question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve plot aspect handling when using cartopy

See original GitHub issue

This applies to single plots and FacetGrids.

The current plotting behavior when using a projection that changes the plot aspect is as follows:

from xray.tutorial import load_dataset

ds = load_dataset('air_temperature')

ax = plt.subplot(projection=ccrs.LambertConformal())
ds.air.isel(time=0).plot(transform=ccrs.PlateCarree())
ax.coastlines()
ax.gridlines()

single

fg = ds.air.isel(time=slice(0, 9)).plot(col='time', col_wrap=3, transform=ccrs.PlateCarree(),
                                   subplot_kws=dict(projection=ccrs.LambertConformal()))
for ax in fg.axes.flat:
    ax.coastlines()
    ax.gridlines()

facet

There are two problems here, I think both are related to the aspect of the subplot:

  1. In the single case, the subplot aspect is correct but the colorbar is not scaled appropriately
  2. In the FacetGrid case, the subplot aspects are not correct but the colorbar is.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dcheriancommented, Feb 1, 2019

Found a solution for 1. This function works really well even when resizing a figure on matplotlib 3.0.2:

def make_colorbar(ax, mappable, **kwargs):
    from mpl_toolkits.axes_grid1 import make_axes_locatable
    import matplotlib as mpl

    divider = make_axes_locatable(ax)
    orientation = kwargs.pop('orientation', 'vertical')
    if orientation == 'vertical':
        loc = 'right'
    elif orientation == 'horizontal':
        loc = 'bottom'
        
    cax = divider.append_axes(loc, '5%', pad='3%', axes_class=mpl.pyplot.Axes)
    ax.get_figure().colorbar(mappable, cax=cax, orientation=orientation)
import cartopy.crs as ccrs
import xarray as xr

ds = xr.tutorial.open_dataset('air_temperature').load()

ax = plt.subplot(projection=ccrs.LambertConformal())
mappable = ds.air.isel(time=0).plot(transform=ccrs.PlateCarree(), add_colorbar=False)
ax.coastlines()
ax.gridlines()

make_colorbar(ax, mappable, orientation='vertical')

image

ds = xr.tutorial.open_dataset('air_temperature').load()

ax = plt.subplot(projection=ccrs.LambertConformal())
mappable = ds.air.isel(time=0).plot(transform=ccrs.PlateCarree(), add_colorbar=False)
ax.coastlines()
ax.gridlines()

make_colorbar(ax, mappable, orientation='horizontal')

image

0reactions
stale[bot]commented, Jan 3, 2021

In order to maintain a list of currently relevant issues, we mark issues as stale after a period of inactivity

If this issue remains relevant, please comment here or remove the stale label; otherwise it will be marked as closed automatically

Read more comments on GitHub >

github_iconTop Results From Across the Web

More advanced mapping with cartopy and matplotlib - SciTools
From the outset, cartopy's purpose has been to simplify and improve the quality of mapping visualisations available for scientific data. Thanks to the ......
Read more >
How can I modify the margins when I plot map with cartopy?
This will create an axes which sits tight at one side of the figure. The reason is that the map has a fixed...
Read more >
Making Maps with Cartopy — COSIMA Recipes documentation
This tutorial focusses on mapping with Cartopy . ... Instead, let's plot the same data with cartopy. ... You can see that this...
Read more >
Interpolations for imshow — Matplotlib 3.6.2 documentation
For the Agg, ps and pdf backends, interpolation='none' works well when a big image is scaled down, while interpolation='nearest' works well when a...
Read more >
Working with Maps - Python Numerical Methods
cartopy has very nice API to interact with matplotlib, to plot a map, we only need to tell the matplotlib to use a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found