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.

pass projection argument to plt.subplot when faceting with cartopy transform

See original GitHub issue

I have a data 3D DataArray with Time, Latitude and Longitude coordinates.

I want to plot maps of this dataset, faceted by Time. The following code

import cartopy.crs as ccrs
proj = ccrs.PlateCarree()
data.plot(transform=proj, col='Time', col_wrap=3, robust=True)

fails with

ValueError: Axes should be an instance of GeoAxes, got <class 'matplotlib.axes._subplots.AxesSubplot'> 

this is because to plot with a transform, the axes must be a GeoAxes, which is done with something like plt.subplot(111, projection=proj). The implicit subplotting done when faceting does not do that. To make the faceting works, I had to do

import cartopy.crs as ccrs
proj = ccrs.PlateCarree()
data.plot(transform=proj, col='Time', col_wrap=3, robust=True, subplot_kws={'projection':proj})

I propose that, when plot faceting is requested with a transform kw, the content of that keyword should be passed to the subplot function as a projection argument automatically by default. If a projection is provided explicitely like in the call above, use that one.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
fmaussioncommented, Jan 13, 2017

However it looks like no xarray code contains the transform keyword. However I definitely can use it in calls to plot(). I don’t understand what is going on. Can someone enlighten me?

Yes, this is what I meant with xarray not being directly “cartopy aware”. The reason is that xarray intends to be a general library, and tries to avoid defining too many keyword arguments in order to avoid confusion. However, xarray passes along any kw argument you want to the underlying mpl plot function (https://github.com/pydata/xarray/blob/master/xarray/plot/plot.py#L379). You’ll have to catch it first, either from the kwargs dict or by adding your own to the signature (the latter being recommended only if necessary, for the reason I mentioned above)

0reactions
mathausecommented, Mar 26, 2020

I think the second example shows this: http://xarray.pydata.org/en/stable/plotting.html#maps

Related: #3169

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I change matplotlib's subplot projection of an existing ...
crs. CRS projections. but I was wondering if there is a proper matplotlib method to change a subplot axis projection after it was...
Read more >
pass projection argument to plt.subplot when faceting with ...
I have a data 3D DataArray with Time , Latitude and Longitude coordinates. I want to plot maps of this dataset, faceted by...
Read more >
Understanding the transform and projection keywords - SciTools
The projection argument is used when creating plots and determines the projection of the resulting plot (i.e. what the plot looks like). The...
Read more >
Plotting — xarray 0.9.6+dev240.g5a28b89 documentation
The easiest way to create faceted plots is to pass in row or col arguments to the xarray plotting methods/functions. This returns a...
Read more >
plotting.rst.txt - Xarray
To plot :py:class:`Dataset` objects simply access the relevant DataArrays, ... the plot to an existing axis pass in the axis as a keyword...
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