Question

I'd like to get two colourbars in one plot, with a map. Unfortunately, the colourbars are as big as the plot themselves. Even using shrink in the colourbar code, it only shrinks the colourbars, but not the size that they take up.

IS there a simple way to use more space for my plot, and less space for the colourbars? And is there an easy way to get the colourbars to appear side by side at the bottom also?

Plot with colourbars Code is below

plt.clf()
my_cmap = cm.get_cmap('YlOrRd')


cs = map.contourf(x,y,bj,levels = Y,cmap=my_cmap,locator=mpl.ticker.LogLocator())


norm = mpl.colors.BoundaryNorm(bounds, my_cmap.N)
    cb1 = plt.colorbar(cmap=my_cmap, 
                norm=norm,
                boundaries=bounds,
                extend='both',
                orientation="horizontal",
                ticks=bounds,
                shrink = 0.35)
    cb1.set_label('Increase in Black Carbon')
    bj = -bj
    ymap = cm.get_cmap('PuBu')


    cs = map.contourf(x,y,bj,levels = Y,cmap=ymap,locator=mpl.ticker.LogLocator())

    # set colourbar with location and size, with labels.
    norm = mpl.colors.BoundaryNorm(bounds,ymap.N)
    cb2 = plt.colorbar(cmap=my_cmap, 
                norm=norm,
                boundaries=bounds,
                extend='both',
                orientation="horizontal",
                ticks=bounds,
                shrink=0.35)


    cb2.set_label('Decrease in Black Carbon')

    font = {'family' : 'serif',
        'color'  : 'black',
        'weight' : 'bold',
        'size'   : 21,
        }

    #add plot details
    plt.title(r'Black Carbon surface concentrations changes in %s 2006 compared with %s 2006 ($\mu$gm$\^3$)'%(g,d) ,fontdict=font)
    map.drawcoastlines(linewidth=0.75)
    map.drawcountries(linewidth=0.25)
    #show plot
    plt.show()
Was it helpful?

Solution

A minimal working example* of your problem and some more options for plt.colorbar that achieve what you are looking for:

import pylab as plt

plt.imshow([[1,2,3],[4,5,6]])

cbar_options = {'extend':'both',
                'orientation':"horizontal",
                'shrink':0.75,
                'fraction':.10,
                'pad':.07}

cb1 = plt.colorbar(**cbar_options)
cb1.set_label('Increase in Black Carbon')

cb2 = plt.colorbar(**cbar_options)
cb2.set_label('Decrease in Black Carbon')

plt.show()

enter image description here

  • You should always try to post minimal pieces of code that work out of the box. Your example required a ton of fiddling and was missing imports and variables!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top