user2050187
user2050187

Reputation: 175

Plot 3 figures with common colorbar by using Basemap

I have been using this code to plot 3 figures with common colorbar:

grid_top = ImageGrid(fig, 211, nrows_ncols = (1, 3),
                     cbar_location = "right",     
                     cbar_mode="single",
                     cbar_pad=.2) 

for n in xrange(3):
     im1 = grid_top[n].pcolor(data_top[n], 
                        interpolation='nearest', vmin=0, vmax=5)

plt.show()

I want to use Basemap to plot orthographic projection, which I define as:

m=Basemap(projection='ortho',lon_0=lon_0,lat_0=lat_0,resolution='l',
          llcrnrx=0.,‌​llcrnry=0.,urcrnrx=m1.urcrnrx/2.,urcrnry=m1.urcrnry/2.)

When, I change the code above as follow:

im1 = grid_top[n].m.pcolor(data_top[n], interpolation='nearest', vmin=0, vmax=5)

I get an error..

What do I have to change to make it works with Basemap ?

Thank you

Upvotes: 0

Views: 1416

Answers (1)

Deditos
Deditos

Reputation: 1415

Well, it's a bit difficult to diagnose the specific error without seeing the error message you're getting, but I'll give it a go.

When you call Basemap() it draws the map to the currently active Axes instance, but when the axes are defined using ImageGrid() and then looped through none of them is ever active. You can activate an existing Axes instance at any time using plt.sca() (set current axes).

To finalise the colorbar you need to call a colorbar() instance after you've actually plotted some data using pcolor() or imshow().

Here's a working example of what I think you're trying to achieve:

data_top = []
for i in range(3):
    data_top.append(np.random.random((5,5)))


fig = plt.figure(1,(8.,8.))

grid_top = ImageGrid(fig, 211, nrows_ncols = (1, 3),
                     cbar_location = "right",
                     cbar_mode="single",
                     cbar_pad=.2)

for g, d in zip(grid_top,data_top):
    plt.sca(g)
    M = Basemap(projection='ortho',lon_0=10.0,lat_0=50.0,resolution='l')
    M.drawcoastlines()
    M.drawparallels(np.arange(-90.,120.,30.))
    M.drawmeridians(np.arange(0.,360.,60.))
    I = M.imshow(d,vmin=0,vmax=1,interpolation="nearest")
grid_top.cbar_axes[0].colorbar(I)

Some ugly maps, but you get the idea.

Upvotes: 2

Related Questions