Created
April 4, 2016 11:22
-
-
Save j08lue/2a2207c73cb068a1c2fa95bc2c0367cb to your computer and use it in GitHub Desktop.
Cartopy resize colorbar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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