So I want to plot a 3d
map using matplotlib basemap
. But an error message comes popping up.
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()
I got an error message on line 21{i.e ax.add_collection3d(map.drawcoastlines(linewidth=0.25))
} saying:-
'It is not currently possible to manually set the aspect '
NotImplementedError: It is not currently possible to manually set the aspect on 3D axes'
I found this question because I had the exact question.
I later chanced upon some documentation that revealed the workaround - if setting of aspect is not implemented, then let's not set it by setting fix_aspect to false:
map = Basemap(fix_aspect=False)
EDIT:
I suppose I should add a little more to my previous answer to make it a little easier to understand what to do.
The NotImplementedError
is a deliberate addition by the matplotlib team, as can be seen here. What the error is saying is that we are trying to fix the aspect ratio of the plot, but this is not implemented in 3d plots.
This error occurs when using mpl_toolkits.basemap()
with 3d plots as it sets fix_aspect=True
by default.
Therefore, to do away with the NotImplementedError
, one can consider adding fix_aspect=False
when calling mpl_toolkits.basemap()
. For example:
map = Basemap(llcrnrlon=-20,llcrnrlat=0,urcrnrlon=15,urcrnrlat=50,fix_aspect=False)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With