Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

geometry of colorbars in matplotlib

Plotting a figure with a colorbar, like for example the ellipse collection of the matplotlib gallery, I'm trying to understand the geometry of the figure. If I add the following code in the source code (instead of plt.show()):

cc=plt.gcf().get_children()
print(cc[1].get_geometry())
print(cc[2].get_geometry())

I get

(1, 2, 1)
(3, 1, 2)

I understand the first one - 1 row, two columns, plot first (and presumably the second is the colorbar), but I don't understand the second one, which I would expect to be (1,2,2). What do these values correspond to?

Edit: It seems that the elements in cc do not have the same axes,which would explain the discrepancies. Somehow, I'm still confused with the geometries that are reported.

like image 493
bzklrm Avatar asked Nov 03 '22 10:11

bzklrm


1 Answers

What's happening is when you call colorbar, use_gridspec defaults to True which then makes a call to matplotlib.colorbar.make_axes_gridspec which then creates a 1 by 2 grid to hold the plot and cbar axes then then cbar axis itself is actually a 3 by 1 grid that has its aspect ratio adjusted

the key line in matplotlib.colorbar.make_axes_gridspec which makes this happen is

gs2 = gs_from_sp_spec(3, 1, subplot_spec=gs[1], hspace=0.,
                      height_ratios=wh_ratios)

because wh_ratios == [0.0, 1.0, 0.0] by default so the other two subplots above and below are 0 times the size of the middle plot.

I've put what I did to figure this out into an IPython notebook

like image 197
Phillip Cloud Avatar answered Nov 13 '22 22:11

Phillip Cloud