5

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.

bzklrm
  • 153
  • 4
  • My output is different; I get `(1,1,1)` and the second `print` statement gives an exception, `AttributeError: 'Axes' object has no attribute 'get_geometry'`. – egpbos Jun 24 '13 at 15:25

1 Answers1

2

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

Phillip Cloud
  • 24,919
  • 11
  • 68
  • 88
  • I noticed that if I give the `colorbar()` function call an `ax` argument: `plt.colorbar(sth, ax=ax)`, the geometry of `ax` will be changed, e.g. ax is created `ax=fig.add_subplot(2,3,4)`, then after the colorbar plotting its geometry will be changed to "(2,1,1)" always, I think (2,1,1) is the plot itself, and (2,1,2) is the colorbar. If I create a new `ax2=fig.add_subplot(2,3,4)`, ax2's geometry is again (2,1,1). Could this be regarded as a bug? – Jason Jul 14 '17 at 03:51