Skip to content
This repository

External transform api #1090

Merged
merged 4 commits into from over 1 year ago

3 participants

Phil Elson Benjamin Root Eric Firing
Phil Elson
Collaborator

This PR adds an easy interface for matplotlib transform creation based on a custom object.

The syntax looks like:

class MyObject(object):
    def _as_mpl_transform(self, axes):
        return ... # a matplotlib transform instance

This interface is comprable to the _as_mpl_axes method to easily create axes from custom classes.

My primary usecase for both of these cases is to provide an interface which could do something like:

plate_carree = MyProjectionLibrary.PlateCarree()
robinson = MyProjectionLibrary.Robinson()

ax = plt.axes(projection=robinson)
plt.plot(lons, lats, transform=plate_carree)
Benjamin Root
Collaborator

Could this be useful for basemap? @jswhit, this PR might be of interest to you.

lib/matplotlib/artist.py
@@ -233,6 +234,8 @@ def get_transform(self):
233 234
         """
234 235
         if self._transform is None:
235 236
             self._transform = IdentityTransform()
  237
+        elif not isinstance(self._transform, Transform) and hasattr(self._transform, '_as_mpl_transform'):
1
Benjamin Root Collaborator

As per PEP8, please keep lines under 80 chars long.

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

Seems like there isn't much complaint about this PR. Anyone willing to merge?

Eric Firing efiring merged commit c0ee100 into from August 20, 2012
Eric Firing efiring closed this August 20, 2012
Eric Firing
Owner
Eric Firing efiring commented on the diff August 20, 2012
lib/matplotlib/artist.py
@@ -3,7 +3,8 @@
3 3
 import matplotlib
4 4
 import matplotlib.cbook as cbook
5 5
 from matplotlib import docstring, rcParams
6  
-from transforms import Bbox, IdentityTransform, TransformedBbox, TransformedPath
  6
+from transforms import Bbox, IdentityTransform, TransformedBbox, \
1
Eric Firing Owner
efiring added a note August 20, 2012

For later cleanup, at leisure: use parentheses instead of backslash; I'm pretty sure this is supported for 2.6 and later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Eric Firing efiring commented on the diff August 20, 2012
lib/matplotlib/artist.py
@@ -233,6 +234,9 @@ def get_transform(self):
233 234
         """
234 235
         if self._transform is None:
235 236
             self._transform = IdentityTransform()
  237
+        elif (not isinstance(self._transform, Transform) 
  238
+              and hasattr(self._transform, '_as_mpl_transform')):
2
Eric Firing Owner
efiring added a note August 20, 2012

Is there any point in checking for the _as_mpl_transform attribute? If it is not None, and it is not a Transform, and it can't be made into one, shouldn't it just raise an exception? Or is the idea that something not inheriting from Transform can still function as if it did?

Phil Elson Collaborator
pelson added a note August 20, 2012

The idea was simply to not break anything else that was already passing something fruity. I think it might just get down to the point where the transform_path etc. methods are called and blow up if they don't exist on the thing in self._transform, though I am not entirely sure.

I'm not too fussed either way, but I am happy to be less generous and just try calling the _as_mpl_transform method, and if it doesn't exist, oh well... what is your preference?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Phil Elson pelson commented on the diff August 30, 2012
lib/matplotlib/artist.py
@@ -223,7 +224,7 @@ def set_transform(self, t):
223 224
         ACCEPTS: :class:`~matplotlib.transforms.Transform` instance
224 225
         """
225 226
         self._transform = t
226  
-        self._transformSet = True
  227
+        self._transformSet = t is not None
1
Phil Elson Collaborator
pelson added a note August 30, 2012

Turns out this is bad. Reverted in #1176 (along with a suitable test).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.