matplotlib.mplot3d 立方体表面的底图

发布于 2024-11-10 08:25:36 字数 854 浏览 2 评论 0原文

正如标题所示,我试图在 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')

As the title suggests, I'm trying to plot a Basemap map on the z=0 surface of a matplotlib.mplot3d lineplot. I know the Axes3D object is capable of plotting on the z=0 surface (via Axes3D.plot, Axes3D.scatter, etc.), but I can't figure out how to do so with a Basemap object. Hopefully the code below shows what I need clearly enough. Any ideas would be much appreciated!

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')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如梦亦如幻 2024-11-17 08:25:36

只需将地图作为 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()

Just add your map as a 3d collection to the Axes3D instance:

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()
童话里做英雄 2024-11-17 08:25:36

AX.add_collection3d(M.drawcoastlines())

有效,但

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

无效。

添加颜色填充后,您会收到类似以下内容的错误:“AttributeError:‘Polygon’对象没有属性‘do_3d_projection’”

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

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

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

AX.add_collection3d(M.drawcoastlines())

works but

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

does not work.

As soon as you add color fill you get an error similar to: "AttributeError: 'Polygon' object has no attribute 'do_3d_projection'"

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

returns an array of Polygons, not one of the inputs required by add_collection(). collect.PatchCollection() does not seem to work either.

So what do you use to add `M.fillcontinents(lake_color='#888888', color='#282828') to a 3D plot?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文