无法使用 'ax.add_collection3d(map.drawcoastlines())' 添加 'map.drawcoastline' 到 3d 图
Not able to add 'map.drawcoastline' to 3d figure using 'ax.add_collection3d(map.drawcoastlines())'
所以我想使用 matplotlib basemap
绘制 3d
地图。但是弹出一条错误消息。
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.basemap import Basemap
from matplotlib.collections import PolyCollection
import numpy as np
map = Basemap(llcrnrlon=-20,llcrnrlat=0,urcrnrlon=15,urcrnrlat=50,)
fig = plt.figure()
ax = Axes3D(fig)
#ax.set_axis_off()
ax.azim = 270
ax.dist = 7
polys = []
for polygon in map.landpolygons:
polys.append(polygon.get_coords())
lc=PolyCollection(polys,edgecolor='black',facecolor='#DDDDDD',closed=False)
ax.add_collection3d(lc)
ax.add_collection3d(map.drawcoastlines(linewidth=0.25))
ax.add_collection3d(map.drawcountries(linewidth=0.35))
lons = np.array([-13.7, -10.8, -13.2, -96.8, -7.99, 7.5, -17.3, -3.7])
lats = np.array([9.6, 6.3, 8.5, 32.7, 12.5, 8.9, 14.7, 40.39])
cases = np.array([1971, 7069, 6073, 4, 6, 20, 1, 1])
deaths = np.array([1192, 2964, 1250, 1, 5, 8, 0, 0])
places = np.array(['Guinea', 'Liberia', 'Sierra Leone','United States','Mali','Nigeria', 'Senegal', 'Spain'])
x, y = map(lons, lats)
ax.bar3d(x, y, np.zeros(len(x)), 2, 2, deaths, color= 'r', alpha=0.8)
plt.show()
我在第 21 行收到一条错误消息{即 ax.add_collection3d(map.drawcoastlines(linewidth=0.25))
} 说:-
'It is not currently possible to manually set the aspect '
NotImplementedError: It is not currently possible to manually set the aspect on 3D axes'
我找到这个问题是因为我有确切的问题。
我后来偶然发现一些 documentation 揭示了解决方法 - 如果未实现方面的设置,那么我们不要通过将 fix_aspect 设置为 false 来设置它:
map = Basemap(fix_aspect=False)
编辑:
我想我应该在我之前的回答中再补充一点,以便更容易理解该怎么做。
其中NotImplementedError
是matplotlib团队有意添加的,可见here。错误的意思是我们正在尝试修复绘图的纵横比,但这在 3d 绘图中没有实现。
将 mpl_toolkits.basemap()
与 3d 图一起使用时会发生此错误,因为它通过 default 设置 fix_aspect=True
。
因此,为了去掉NotImplementedError
,可以考虑在调用mpl_toolkits.basemap()
时加上fix_aspect=False
。例如:
map = Basemap(llcrnrlon=-20,llcrnrlat=0,urcrnrlon=15,urcrnrlat=50,fix_aspect=False)
所以我想使用 matplotlib basemap
绘制 3d
地图。但是弹出一条错误消息。
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.basemap import Basemap
from matplotlib.collections import PolyCollection
import numpy as np
map = Basemap(llcrnrlon=-20,llcrnrlat=0,urcrnrlon=15,urcrnrlat=50,)
fig = plt.figure()
ax = Axes3D(fig)
#ax.set_axis_off()
ax.azim = 270
ax.dist = 7
polys = []
for polygon in map.landpolygons:
polys.append(polygon.get_coords())
lc=PolyCollection(polys,edgecolor='black',facecolor='#DDDDDD',closed=False)
ax.add_collection3d(lc)
ax.add_collection3d(map.drawcoastlines(linewidth=0.25))
ax.add_collection3d(map.drawcountries(linewidth=0.35))
lons = np.array([-13.7, -10.8, -13.2, -96.8, -7.99, 7.5, -17.3, -3.7])
lats = np.array([9.6, 6.3, 8.5, 32.7, 12.5, 8.9, 14.7, 40.39])
cases = np.array([1971, 7069, 6073, 4, 6, 20, 1, 1])
deaths = np.array([1192, 2964, 1250, 1, 5, 8, 0, 0])
places = np.array(['Guinea', 'Liberia', 'Sierra Leone','United States','Mali','Nigeria', 'Senegal', 'Spain'])
x, y = map(lons, lats)
ax.bar3d(x, y, np.zeros(len(x)), 2, 2, deaths, color= 'r', alpha=0.8)
plt.show()
我在第 21 行收到一条错误消息{即 ax.add_collection3d(map.drawcoastlines(linewidth=0.25))
} 说:-
'It is not currently possible to manually set the aspect '
NotImplementedError: It is not currently possible to manually set the aspect on 3D axes'
我找到这个问题是因为我有确切的问题。
我后来偶然发现一些 documentation 揭示了解决方法 - 如果未实现方面的设置,那么我们不要通过将 fix_aspect 设置为 false 来设置它:
map = Basemap(fix_aspect=False)
编辑:
我想我应该在我之前的回答中再补充一点,以便更容易理解该怎么做。
其中NotImplementedError
是matplotlib团队有意添加的,可见here。错误的意思是我们正在尝试修复绘图的纵横比,但这在 3d 绘图中没有实现。
将 mpl_toolkits.basemap()
与 3d 图一起使用时会发生此错误,因为它通过 default 设置 fix_aspect=True
。
因此,为了去掉NotImplementedError
,可以考虑在调用mpl_toolkits.basemap()
时加上fix_aspect=False
。例如:
map = Basemap(llcrnrlon=-20,llcrnrlat=0,urcrnrlon=15,urcrnrlat=50,fix_aspect=False)