Set the Matplotlib color matrix size to fit the graph.

I can’t get the colorbar on imshow graphs like this to be the same height as the graph without using Photoshop after the fact. How to get height to fit? Example of the colorbar size mismatch

+120
python matplotlib image
Aug 12 '13 at 20:06 on
source share
6 answers

You can do this easily with matplotlib AxisDivider .

The example from the linked page also works without using subheadings:

import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1 import make_axes_locatable import numpy as np plt.figure() ax = plt.gca() im = ax.imshow(np.arange(100).reshape((10,10))) # create an axes on the right side of ax. The width of cax will be 5% # of ax and the padding between cax and ax will be fixed at 0.05 inch. divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) plt.colorbar(im, cax=cax) 

enter image description here

+151
Aug 12 '13 at 20:15
source share

This combination (and values ​​close to these) seems to “magically” work for me to preserve a color bar scaled to the graph, regardless of the size of the display.

 plt.colorbar(im,fraction=0.046, pad=0.04) 

It also does not require sharing an axis, which can get a graph from a square.

+167
Nov 03 '14 at 18:18
source share

@bogatron already gave the answer suggested by matplotlib docs , which creates the correct height, but presents another problem. Now the width of the color panel (as well as the space between color and plot) changes with the width of the graph. In other words, the aspect ratio of the color bar is no longer fixed.

To get both the correct height and the specified aspect ratio, you need to delve into the mysterious module axes_grid1 .

 import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1 import make_axes_locatable, axes_size import numpy as np aspect = 20 pad_fraction = 0.5 ax = plt.gca() im = ax.imshow(np.arange(200).reshape((20, 10))) divider = make_axes_locatable(ax) width = axes_size.AxesY(ax, aspect=1./aspect) pad = axes_size.Fraction(pad_fraction, width) cax = divider.append_axes("right", size=width, pad=pad) plt.colorbar(im, cax=cax) 

Please note that this indicates the width of the color panel wrt the height of the graph (as opposed to the width of the shape, as it was before).

The interval between the color panel and graphics can now be set as part of the width of the color panel, which is IMHO a much more significant number than the proportion of the width of the figure.

color plot graphic plot

UPDATE:

I created an IPython laptop on a topic where I packed the above code into an easily reusable function:

 import matplotlib.pyplot as plt from mpl_toolkits import axes_grid1 def add_colorbar(im, aspect=20, pad_fraction=0.5, **kwargs): """Add a vertical color bar to an image plot.""" divider = axes_grid1.make_axes_locatable(im.axes) width = axes_grid1.axes_size.AxesY(im.axes, aspect=1./aspect) pad = axes_grid1.axes_size.Fraction(pad_fraction, width) current_ax = plt.gca() cax = divider.append_axes("right", size=width, pad=pad) plt.sca(current_ax) return im.axes.figure.colorbar(im, cax=cax, **kwargs) 

It can be used as follows:

 im = plt.imshow(np.arange(200).reshape((20, 10))) add_colorbar(im) 
+23
Nov 03 '15 at 17:30
source share

When creating a colorbar try using the fraction and / or compression options.

From the docs:

share 0.15; share of source axes for use in colorbar

reduce 1.0; fraction with which you can reduce the color gamut

+8
Aug 12 '13 at 20:23
source share

All of the above solutions are good, but I like @Steve and @bejota the best, as they are not related to fancy calls and are universal.

By universal, I mean that it works with any type of axis, including GeoAxes . For example, you have projected axes to display:

 projection = cartopy.crs.UTM(zone='17N') ax = plt.axes(projection=projection) im = ax.imshow(np.arange(200).reshape((20, 10))) 

call

 cax = divider.append_axes("right", size=width, pad=pad) 

crash: KeyException: map_projection

Thus, the only universal way to determine the size of a color panel with all types of axes is:

 ax.colorbar(im, fraction=0.046, pad=0.04) 

Work with a fraction from 0.035 to 0.046 to get the maximum size. However, the values ​​for the fraction and the paddig should be adjusted to best suit your plot and will differ depending on whether the orientation of the color bar is vertical or horizontal.

+7
Oct 09 '16 at 20:39
source share

I appreciate all the answers above. However, as some answers and comments indicated, the axes_grid1 module cannot solve GeoAxes, while adjusting fraction , pad , shrink , and other similar parameters may not necessarily give a very precise order, which really bothers me. I believe that providing a colorbar your own axes might be the best solution to solve all the problems that have been mentioned.

The code

 import matplotlib.pyplot as plt import numpy as np fig=plt.figure() ax = plt.gca() im = ax.imshow(np.arange(100).reshape((10,10))) # Create an axes for colorbar. The position of the axes is calculated based on the position of ax. # You can change 0.01 to adjust the distance between the main image and the colorbar. # You can change 0.02 to adjust the width of the colorbar. # This practice is universal for both subplots and GeoAxes. cax = fig.add_axes([ax.get_position().x1+0.01,ax.get_position().y0,0.02,ax.get_position().height]) plt.colorbar(im, cax=cax) 

Result

enter image description here

0
Jul 05 '19 at 10:01
source share



All Articles