Skip to content

Instantly share code, notes, and snippets.

@j08lue
Created April 4, 2016 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j08lue/2a2207c73cb068a1c2fa95bc2c0367cb to your computer and use it in GitHub Desktop.
Save j08lue/2a2207c73cb068a1c2fa95bc2c0367cb to your computer and use it in GitHub Desktop.
Cartopy resize colorbar
def get_resize_event_function(ax, cbar_ax):
"""
Returns a function to automatically resize the colorbar
for cartopy plots
Parameters
----------
ax : axis
cbar_ax : colorbar axis
Example
-------
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(10,5), subplot_kw={'projection': ccrs.PlateCarree()})
cbar_ax = fig.add_axes([0, 0, 0.1, 0.1])
[... your code generating a scalar mappable ...]
resize_colorbar = get_resize_event_function(ax, cbar_ax)
fig.canvas.mpl_connect('resize_event', resize_colorbar)
Credits
-------
Solution by pelson at http://stackoverflow.com/a/30077745/512111
"""
def resize_colorbar(event):
plt.draw()
posn = ax.get_position()
cbar_ax.set_position([posn.x0 + posn.width + 0.01, posn.y0,
0.04, posn.height])
return resize_colorbar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment