7

I need to make a plot with n number of basemap subplots. But when I am doing this the all the values are plotted on the first subplot.

My data is a set of 'n' matrixes, stored in data_all.

f, map = plt.subplots(n,sharex=True, sharey=True, figsize=(20,17))

plt.subplots_adjust(left=None, bottom=None, right=None, top=None,
                    wspace=None, hspace=0.)

for i in range(n):
    map = Basemap(projection='merc', lat_0=0, lon_0=180,
                  resolution='h', area_thresh=0.1,
                  llcrnrlon=0, llcrnrlat=-45,
                  urcrnrlon=360, urcrnrlat=45)
    map.drawcoastlines(linewidth=0.5)
    map.drawmapboundary()
    map.drawmapboundary()
    nx = data_all.shape[0]
    ny = data_all.shape[1]
    lon, lat = map.makegrid(ny[i], nx[i])
    z,y = map(lon, lat)
    cs = map.contourf(z, y, data_all[i])
David Cain
  • 16,484
  • 14
  • 65
  • 75
Nidhi
  • 71
  • 1
  • 2
  • 3
    Just a note: `map` is a [built-in function of Python](http://docs.python.org/2/library/functions.html#map). Setting a variable with that name is bound to cause confusion. – David Cain Jun 24 '13 at 01:44
  • See http://stackoverflow.com/questions/14254379/how-can-i-attach-a-pyplot-function-to-a-figure-instance/14261698#14261698 for difference between OO and state machine interface – tacaswell Jun 24 '13 at 19:00

2 Answers2

16

I can't test it at the moment, but basically, you just need to tell basemap which axes to use.

For example:

import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

fig, axes = plt.subplots(nrows=4, ncols=3)
for ax in axes.flat:
    map_ax = Basemap(ax=ax)
    map_ax.drawcoastlines()
plt.show()

enter image description here

Joe Kington
  • 275,208
  • 71
  • 604
  • 463
  • Hii thanks for the reply.But my real problem comes when I try to use your same code...with figsize increased and subplots only in rows and increased in number say 13. Them the plot shinks in dimension. – Nidhi Jul 16 '13 at 00:48
  • Hii thanks for the reply.But my real problem comes when I try to use your same code...with figsize increased, say 13 or more subplots in rows. Them the plot shinks in dimension. import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap fig, axes = plt.subplots(13,figsize=(10,15)) for ax in axes.flat: map_ax = Basemap(ax=ax,projection='merc', lat_0 = 0, lon_0 =120, resolution = 'h', area_thresh = 10000, llcrnrlon=60, llcrnrlat=-20, urcrnrlon=180, urcrnrlat=20) map_ax.drawcoastlines() plt.show() However it works fine with 1-2 subplots. – Nidhi Jul 16 '13 at 00:53
1

Hope this will help you. https://github.com/matplotlib/basemap/blob/master/examples/panelplot.py It shows how to make multi-panel plots.

mitchelllc
  • 1,607
  • 4
  • 20
  • 24