milimovement.blogg.se

Pyplot subplot colorbar
Pyplot subplot colorbar





pyplot subplot colorbar

#PYPLOT SUBPLOT COLORBAR CODE#

I created an IPython notebook on the topic, where I packed the above code into an easily re-usable function: import matplotlib.pyplot as pltĭef add_colorbar(im, aspect=20, pad_fraction=0.5, **kwargs): The spacing between colorbar and plot can now be specified as a fraction of the width of the colorbar, which is IMHO a much more meaningful number than a fraction of the figure width. the height of the plot (in contrast to the width of the figure, as it was before). Note that this specifies the width of the colorbar w.r.t. Pad = axes_size.Fraction(pad_fraction, width)Ĭax = divider.append_axes("right", size=width, pad=pad) Width = axes_size.AxesY(ax, aspect=1./aspect) import matplotlib.pyplot as pltįrom mpl_toolkits.axes_grid1 import make_axes_locatable, axes_size

pyplot subplot colorbar

To get both the right height and a given aspect ratio, you have to dig a bit deeper into the mysterious axes_grid1 module. In other words, the aspect ratio of the colorbar is not fixed anymore. Now the width of the colorbar (as well as the space between colorbar and plot) changes with the width of the plot. Code fig, ax = plt.subplots(2,1,figsize=(12,8))Ĭax = fig.add_axes(.get_position().x1-0.25,ax.get_position().y0,0.02,ax.get_position().y1-ax.get_position().y0])įig.colorbar(im1, already gave the answer suggested by the matplotlib docs, which produces the right height, but it introduces a different problem.

pyplot subplot colorbar

Im2 = ax.imshow(np.arange(-100,0).reshape((10,10)), vmin = -100, vmax =100)Īgain, you can also achieve similar effects by specifying cax, a more accurate way from my perspective. Code fig, ax = plt.subplots(2,1,figsize=(12,8)) # Caution, figsize will also influence positions. Therefore, it is useful for multiple subplots, see following. Later on, I find official documentation also gives ax option, which are existing axes that will provide room for the colorbar. Plt.colorbar(im, cax=cax) # Similar to fig.colorbar(im, cax = cax) # This practice is universal for both subplots and GeoAxes.Ĭax = fig.add_axes() # You can change 0.02 to adjust the width of the colorbar. # You can change 0.01 to adjust the distance between the main image and the colorbar. The position of the axes is calculated based on the position of ax. I believe that giving the colorbar its own axes might be a better solution to address all the issues that have been mentioned. However, like some answers and comments pointed out, the axes_grid1 module cannot address GeoAxes, whereas adjusting fraction, pad, shrink, and other similar parameters cannot necessarily give the very precise order, which really bothers me.







Pyplot subplot colorbar