Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Raise exception when user tries to use set_xlim or set_ylim on a geographic projection #1856

Closed
wants to merge 3 commits into from

5 participants

@adrn

This is a pretty simple, atomic change but may require some documentation if accepted. I've found that when teaching users about projections, many people expect to be able to change the projection limits (rotate the projection) by using set_xlim() and set_ylim(). The functions work on projection axes:

>>> fig,axes = plt.subplots(1,1,subplots_kw=dict(projection="aitoff"))
>>> axes.set_xlim(0,360)

but don't actually do anything. I propose (with this PR) that these functions explicitly throw a NotImplementedError so it is obvious to the user that they are not actually changing anything by using the limit functions:

>>> axes.set_xlim(0,360)
NotImplementedError: set_xlim and set_ylim are not implemented for geographic projections
@dmcdougall
Collaborator

I'm happy with this approach. We should be clear when the user does something wrong, and this achieves that. I would rather the error message be a little more self-explanatory. Perhaps something like,

NotImplementedError: It is not possible to change axes limits for geographic projections

Also, maybe it makes more sense to raise a TypeError to really get across that it's the type of projection that doesn't support this behaviour.

@adrn

Sure, that seems reasonable to me! Will make changes and commit.

@WeatherGod
Collaborator
@mdboom
Owner

:+1:

lib/matplotlib/projections/geo.py
@@ -158,8 +158,8 @@ def set_yscale(self, *args, **kwargs):
set_xscale = set_yscale
def set_xlim(self, *args, **kwargs):
- Axes.set_xlim(self, -np.pi, np.pi)
- Axes.set_ylim(self, -np.pi / 2.0, np.pi / 2.0)
+ raise TypeError("It is not possible to change axes limits "
+ "for geographic projections.")
@pelson Collaborator
pelson added a note

If you add "Please consider using Basemap or Cartopy." to the exception, I think this is good to go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@pelson
Collaborator

Merged with e528dad.

Thanks @adrn.

@pelson pelson closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 3 additions and 2 deletions.
  1. +3 −2 lib/matplotlib/projections/geo.py
View
5 lib/matplotlib/projections/geo.py
@@ -158,8 +158,9 @@ def set_yscale(self, *args, **kwargs):
set_xscale = set_yscale
def set_xlim(self, *args, **kwargs):
- Axes.set_xlim(self, -np.pi, np.pi)
- Axes.set_ylim(self, -np.pi / 2.0, np.pi / 2.0)
+ raise TypeError("It is not possible to change axes limits "
+ "for geographic projections. Please consider "
+ "using Basemap or Cartopy.")
set_ylim = set_xlim
Something went wrong with that request. Please try again.