Closed adamgarbo closed 3 years ago
Yeah, this is not surprising to me. The legend=True
parameter is called on a single plot, and the code as currently written has awareness of that one plot and one axis and not of its parent figure. So that colorbar is being squeezed onto the axis, forcing the actual plot element to shrink to accommodate it.
To fix this specific issue, I believe that you can use the following code (ref):
fig.colorbar(cm.ScalarMappable(norm=norm, cmap=cmap))
Doing this automatically for you could definitely be part of the geoplot
API, but it would also definitely complicated the implementation (if the code needs to manipulate the colorbar it now needs to know where to look to find it; I'd have to double up on colorbar tests account for this new boolean option; etcetera).
So far my position has been that geoplot
will do everything possible to make overplotting on a single axis work great, but that setting up subplotting to work how you want it to work is up to you. This is because I think the "correct" implementation for subplotting is to do what seaborn
does, which is to have a whole separate API for it.
Does setting the colorbar on the figure do the trick for you?
Hi @ResidentMario,
Thanks for the quick reply. It appears that setting the colorbar to the figure will still change the size of the right-most subplot.
proj = gcrs.PlateCarree()
fig = plt.figure(figsize=(15,5))
ax1 = plt.subplot(131, projection=proj)
ax2 = plt.subplot(132, projection=proj)
ax3 = plt.subplot(133, projection=proj)
fig.colorbar(cm.ScalarMappable(norm=norm, cmap=cmap))
However, if you add the axes as a list to the colorbar, it does appear to plot correctly:
fig.colorbar(cm.ScalarMappable(norm=norm, cmap=cmap), ax=(ax1,ax2,ax3))
I found a good discussion on colorbars and subplots on Stack Overflow that gave me the hint to do so: https://stackoverflow.com/questions/13784201/matplotlib-2-subplots-1-colorbar
Thanks again for your help.
Cheers, Adam
Hi @ResidentMario,
Thanks for a great repository.
A quick question regarding creating a figure with several subplots that all share a single colour bar. This was originally asked about in #163, but the discussion migrated to adding colour bar normalization functionality. Speaking of, normalizing the colour bar works great and can be applied to multiple subplots. However, when the colour bar is enabled for a single axis, it will change the size of the subplot, as shown below:
Is there a preferred method of adding a shared colourbar in Geoplot that won't distort the size of an individual subplot? Perhaps, by adding it to the figure instead of an axis?
Cheers, Adam