M Azharuddin
M Azharuddin

Reputation: 45

unable to generate multiple basemap plots

I need to generate 5 basemap plots one after other, the code which is tried is:

fig, axes = plt.subplots(nrows=1, ncols=1)
for i in np.arange(0,5,1):
                     map = Basemap(projection='cyl',ax=axes[i],llcrnrlat= 4,urcrnrlat= 50,\
                                                                        resolution='c', llcrnrlon=45,urcrnrlon=125)
                     map.drawparallels(np.arange(4,50,6),labels=[1,0,0,0],linewidth=0,fontsize=8)
                     map.drawmeridians(np.arange(45,125,8),labels=[0,0,0,1],linewidth=0,fontsize=8)
                     map.drawcountries(linewidth=1.5)
                     map.drawcoastlines(linewidth=1.5) 

I get the error

  1. TypeError: 'AxesSubplot' object is not subscriptable

How do I rectify this error to generate 5 basemap plots (Not 5 subplots , but 5 separate plots)

Upvotes: 1

Views: 60

Answers (1)

Jtradfor
Jtradfor

Reputation: 96

The issue is you're specifying the axis in your Basemap call using integers between 0 and 5, but the figure you created only has one axis (1 row x 1 column). Removing ax=axes[i] will resolve the error. However, I think you're going to want to create a new figure inside your loop instead of outside so that you get a new plot every time:

for i in np.arange(0,5,1):
                 fig = plt.figure()
                 map = Basemap(projection='cyl',llcrnrlat= 4,urcrnrlat= 50,\
                                                                    resolution='c', llcrnrlon=45,urcrnrlon=125)
                 map.drawparallels(np.arange(4,50,6),labels=[1,0,0,0],linewidth=0,fontsize=8)
                 map.drawmeridians(np.arange(45,125,8),labels=[0,0,0,1],linewidth=0,fontsize=8)
                 map.drawcountries(linewidth=1.5)
                 map.drawcoastlines(linewidth=1.5)
                 plt.savefig("test%s.png" % (str(i)))
                 plt.close(fig)

Upvotes: 2

Related Questions