3

正如标题所示,我正在尝试在 matplotlib.mplot3d 线图的 z=0 表面上绘制底图。我知道 Axes3D 对象能够在 z=0 表面上绘图(通过 Axes3D.plot、Axes3D.scatter 等),但我不知道如何使用 Basemap 对象进行绘图。希望下面的代码足够清楚地显示我需要什么。任何想法将不胜感激!

import matplotlib.pyplot as pp
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.basemap import Basemap

# make sample data for 3D lineplot
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)

# make the 3D line plot
FIG = ct.pp.figure()
AX = Axes3D(FIG)
AX.plot(x, y, z, '-b')

# make the 2D basemap
### NEEDS TO SOMEHOW BE AT z=0 IN FIG
M = ct.Basemap(projection='stere', width=3700e3, height=2440e3,
               lon_0=-5.0, lat_0=71.0, lat_ts=71.0,
               area_thresh=100, resolution='c')
PATCHES = M.fillcontinents(lake_color='#888888', color='#282828')
4

2 回答 2

1

只需将您的地图作为 3d 集合添加到 Axes3D 实例:

import numpy as np
import matplotlib.pyplot as pp
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.basemap import Basemap

theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-500, 500, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)

FIG = pp.figure()
AX = Axes3D(FIG)
AX.plot(x, y, z, '-b')

M = Basemap(projection='stere', width=3700e3, height=2440e3,
               lon_0=-5.0, lat_0=71.0, lat_ts=71.0,
               area_thresh=100, resolution='c')
AX.add_collection3d(M.drawcoastlines())
AX.grid(True)

pp.draw()
pp.show()
于 2012-02-20T18:36:05.740 回答
-2

AX.add_collection3d(M.drawcoastlines())

有效,但

PATCHES = M.fillcontinents(lake_color='#888888', color='#282828')

不起作用。

添加颜色填充后,您会收到类似于以下内容的错误:“AttributeError: 'Polygon' object has no attribute 'do_3d_projection'”

M.fillcontinents(lake_color='#888888', color='#282828')`

返回一个多边形数组,而不是 add_collection() 所需的输入之一。collect.PatchCollection()似乎也不起作用。

那么你用什么将 `M.fillcontinents(lake_color='#888888', color='#282828') 添加到 3D 绘图中?

于 2014-05-16T12:27:33.343 回答