本文介绍了matplotlib.mplot3d多维数据集的表面上的底图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

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

这篇关于matplotlib.mplot3d多维数据集的表面上的底图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:46