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.

Zoom into specific region of cartopy projection

See original GitHub issue

I’m getting an error with proplot when trying to zoom in to a specific subregion of my cartopy projection. I just want to map the California coastline, but it seems to break due to changes in the axis width. However, this works just fine with matplotlib.

Any tips here? Should I be using something other than set_extent that I can call in plot.subplots()? Any thoughts on maintaining the right width/aspect for a zoomed in cartopy subplot? I’ve had trouble in the past with matplotlib to get differing coastal regions to have the same width/height aspect ratio due to being in different latitudes.

proplot

f, ax = plot.subplots(width='12cm', proj='cyl')
ax.set_extent([-140, -107, 20, 50], crs=ccrs.PlateCarree())

Huge error chain ending in:

ValueError: Not enough room for axes (would have width -33.5696). Increase width, or reduce spacings 'left', 'right', or 'wspace'.

matplotlib

f, ax = plt.subplots(subplot_kw=dict(projection=ccrs.PlateCarree()))
ax.set_extent([-140, -107, 20, 50])
ax.coastlines()

Screen Shot 2019-03-28 at 2 06 38 PM

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
lukelbdcommented, May 14, 2019

P.S. In a recent commit (e78c43d), I fixed the issues with “smart tight layout” and zoomed-in cartopy projections. There is actually a cartopy github issue with the solution posted, it just hasn’t been implemented yet. ProPlot will now also fix issues associated with “zoomed in” cartopy projections having different aspect ratios than the intial aspect ratios, so you will no longer end up with a bunch of unnecessary empty space in your plot.

Anyway, now the only case when “smart tight layout” has to be disabled is when you use parallel/meridian labels with cartopy.

0reactions
bradyrxcommented, May 20, 2019

Thanks for all the tips, @lukelbd. I thought I had tried the proj_kw but maybe not. This is a pretty simple solution!

Good example you added to the docs. Maybe you should also add one to polar-specific projections. I’m just getting into Southern Ocean stuff so getting used to these keywords. Note that of course it doesn’t use rectangular coordinates. You just pass boundinglat to set the extent.

f, ax = plot.subplots(proj='spstere', basemap=True,
                      axwidth='10cm', 
                      proj_kw={'boundinglat': -30, 'lon_0': 120})
ax.pcolormesh(ds.XC, ds.YC, ds, vmin=-10, vmax=10,
              cmap='RdBu')
ax.format(land=True)

Screen Shot 2019-05-20 at 9 17 14 AM

Read more comments on GitHub >

github_iconTop Results From Across the Web

cartopy: zoom in to a region - Stack Overflow
I would like to use Cartopy to plot only a region (in my case, ... longitude and latitude and line width ax =...
Read more >
Zoom into specific region of cartopy projection #14 - GitHub
I'm getting an error with proplot when trying to zoom in to a specific subregion of my cartopy projection. I just want to...
Read more >
Geographic and polar axes — ProPlot documentation
To zoom into cartopy projections, use set_extent or pass lonlim , latlim , or boundinglat to format . The boundinglat keyword controls the...
Read more >
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.
Read more >
Maps with Cartopy - Research Computing in Earth Sciences
Most classes of projection can be configured in projection-specific ways, although Cartopy takes an opinionated stance on sensible defaults.
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