Skip to content

Commit c0ee100

Browse files
committed
Merge pull request #1090 from pelson/external_transform_api
External transform api
2 parents dc535b4 + 456a723 commit c0ee100

File tree

11 files changed

+7825
-20
lines changed

11 files changed

+7825
-20
lines changed

lib/matplotlib/artist.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import matplotlib
44
import matplotlib.cbook as cbook
55
from matplotlib import docstring, rcParams
6-
from transforms import Bbox, IdentityTransform, TransformedBbox, TransformedPath
6+
from transforms import Bbox, IdentityTransform, TransformedBbox, \
7+
TransformedPath, Transform
78
from path import Path
89

910
## Note, matplotlib artists use the doc strings for set and get
@@ -223,7 +224,7 @@ def set_transform(self, t):
223224
ACCEPTS: :class:`~matplotlib.transforms.Transform` instance
224225
"""
225226
self._transform = t
226-
self._transformSet = True
227+
self._transformSet = t is not None
227228
self.pchanged()
228229

229230
def get_transform(self):
@@ -233,6 +234,9 @@ def get_transform(self):
233234
"""
234235
if self._transform is None:
235236
self._transform = IdentityTransform()
237+
elif (not isinstance(self._transform, Transform)
238+
and hasattr(self._transform, '_as_mpl_transform')):
239+
self._transform = self._transform._as_mpl_transform(self.axes)
236240
return self._transform
237241

238242
def hitlist(self, event):

lib/matplotlib/axes.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6022,7 +6022,7 @@ def scatter(self, x, y, s=20, c='b', marker='o', cmap=None, norm=None,
60226022
edgecolors = edgecolors,
60236023
linewidths = linewidths,
60246024
offsets = zip(x,y),
6025-
transOffset = self.transData,
6025+
transOffset = kwargs.pop('transform', self.transData),
60266026
)
60276027
collection.set_transform(mtransforms.IdentityTransform())
60286028
collection.set_alpha(alpha)
@@ -6550,7 +6550,7 @@ def stackplot(self, x, *args, **kwargs):
65506550

65516551
def streamplot(self, x, y, u, v, density=1, linewidth=None, color=None,
65526552
cmap=None, norm=None, arrowsize=1, arrowstyle='-|>',
6553-
minlength=0.1):
6553+
minlength=0.1, transform=None):
65546554
if not self._hold: self.cla()
65556555
lines = mstream.streamplot(self, x, y, u, v,
65566556
density=density,
@@ -6560,7 +6560,8 @@ def streamplot(self, x, y, u, v, density=1, linewidth=None, color=None,
65606560
norm=norm,
65616561
arrowsize=arrowsize,
65626562
arrowstyle=arrowstyle,
6563-
minlength=minlength)
6563+
minlength=minlength,
6564+
transform=transform)
65646565
return lines
65656566
streamplot.__doc__ = mstream.streamplot.__doc__
65666567

lib/matplotlib/collections.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,16 @@ def set_paths(self):
156156
def get_transforms(self):
157157
return self._transforms
158158

159+
def get_offset_transform(self):
160+
t = self._transOffset
161+
if (not isinstance(t, transforms.Transform)
162+
and hasattr(t, '_as_mpl_transform')):
163+
t = t._as_mpl_transform(self.axes)
164+
return t
165+
159166
def get_datalim(self, transData):
160167
transform = self.get_transform()
161-
transOffset = self._transOffset
168+
transOffset = self.get_offset_transform()
162169
offsets = self._offsets
163170
paths = self.get_paths()
164171

@@ -192,7 +199,7 @@ def _prepare_points(self):
192199
"""Point prep for drawing and hit testing"""
193200

194201
transform = self.get_transform()
195-
transOffset = self._transOffset
202+
transOffset = self.get_offset_transform()
196203
offsets = self._offsets
197204
paths = self.get_paths()
198205

@@ -1407,7 +1414,7 @@ def draw(self, renderer):
14071414
if not self.get_visible(): return
14081415
renderer.open_group(self.__class__.__name__, self.get_gid())
14091416
transform = self.get_transform()
1410-
transOffset = self._transOffset
1417+
transOffset = self.get_offset_transform()
14111418
offsets = self._offsets
14121419

14131420
if self.have_units():

lib/matplotlib/contour.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,8 @@ def __init__(self, ax, *args, **kwargs):
774774
raise ValueError('Either colors or cmap must be None')
775775
if self.origin == 'image': self.origin = mpl.rcParams['image.origin']
776776

777+
self.transform = kwargs.get('transform', None)
778+
777779
self._process_args(*args, **kwargs)
778780
self._process_levels()
779781

@@ -822,6 +824,7 @@ def __init__(self, ax, *args, **kwargs):
822824
antialiaseds = (self.antialiased,),
823825
edgecolors= 'none',
824826
alpha=self.alpha,
827+
transform=self.transform,
825828
zorder=zorder)
826829
self.ax.add_collection(col)
827830
self.collections.append(col)
@@ -841,6 +844,7 @@ def __init__(self, ax, *args, **kwargs):
841844
linewidths = width,
842845
linestyle = lstyle,
843846
alpha=self.alpha,
847+
transform=self.transform,
844848
zorder=zorder)
845849
col.set_label('_nolegend_')
846850
self.ax.add_collection(col, False)

lib/matplotlib/pyplot.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3001,7 +3001,7 @@ def stackplot(x, *args, **kwargs):
30013001
draw_if_interactive()
30023002
finally:
30033003
ax.hold(washold)
3004-
3004+
30053005
return ret
30063006

30073007
# This function was autogenerated by boilerplate.py. Do not edit as
@@ -3047,7 +3047,7 @@ def step(x, y, *args, **kwargs):
30473047
@_autogen_docstring(Axes.streamplot)
30483048
def streamplot(x, y, u, v, density=1, linewidth=None, color=None, cmap=None,
30493049
norm=None, arrowsize=1, arrowstyle='-|>', minlength=0.1,
3050-
hold=None):
3050+
transform=None, hold=None):
30513051
ax = gca()
30523052
# allow callers to override the hold state by passing hold=True|False
30533053
washold = ax.ishold()
@@ -3058,7 +3058,7 @@ def streamplot(x, y, u, v, density=1, linewidth=None, color=None, cmap=None,
30583058
ret = ax.streamplot(x, y, u, v, density=density, linewidth=linewidth,
30593059
color=color, cmap=cmap, norm=norm,
30603060
arrowsize=arrowsize, arrowstyle=arrowstyle,
3061-
minlength=minlength)
3061+
minlength=minlength, transform=transform)
30623062
draw_if_interactive()
30633063
finally:
30643064
ax.hold(washold)

lib/matplotlib/quiver.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,11 @@ def __init__(self, ax, *args, **kw):
408408
self.width = kw.pop('width', None)
409409
self.color = kw.pop('color', 'k')
410410
self.pivot = kw.pop('pivot', 'tail')
411+
self.transform = kw.pop('transform', ax.transData)
411412
kw.setdefault('facecolors', self.color)
412413
kw.setdefault('linewidths', (0,))
413414
collections.PolyCollection.__init__(self, [], offsets=self.XY,
414-
transOffset=ax.transData,
415+
transOffset=self.transform,
415416
closed=False,
416417
**kw)
417418
self.polykw = kw
@@ -529,8 +530,6 @@ def _angles_lengths(self, U, V, eps=1):
529530
lengths = np.absolute(dxy[:,0] + dxy[:,1]*1j) / eps
530531
return angles, lengths
531532

532-
533-
534533
def _make_verts(self, U, V):
535534
uv = (U+V*1j)
536535
if self.angles == 'xy' and self.scale_units == 'xy':
@@ -592,7 +591,6 @@ def _make_verts(self, U, V):
592591

593592
return XY
594593

595-
596594
def _h_arrows(self, length):
597595
""" length is in arrow width units """
598596
# It might be possible to streamline the code
@@ -824,6 +822,7 @@ def __init__(self, ax, *args, **kw):
824822
self.barb_increments = kw.pop('barb_increments', dict())
825823
self.rounding = kw.pop('rounding', True)
826824
self.flip = kw.pop('flip_barb', False)
825+
transform = kw.pop('transform', ax.transData)
827826

828827
#Flagcolor and and barbcolor provide convenience parameters for setting
829828
#the facecolor and edgecolor, respectively, of the barb polygon. We
@@ -851,7 +850,7 @@ def __init__(self, ax, *args, **kw):
851850
#Make a collection
852851
barb_size = self._length**2 / 4 #Empirically determined
853852
collections.PolyCollection.__init__(self, [], (barb_size,), offsets=xy,
854-
transOffset=ax.transData, **kw)
853+
transOffset=transform, **kw)
855854
self.set_transform(transforms.IdentityTransform())
856855

857856
self.set_UVC(u, v, c)

lib/matplotlib/streamplot.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None,
1616
cmap=None, norm=None, arrowsize=1, arrowstyle='-|>',
17-
minlength=0.1):
17+
minlength=0.1, transform=None):
1818
"""Draws streamlines of a vector flow.
1919
2020
*x*, *y* : 1d arrays
@@ -134,10 +134,15 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None,
134134
line_colors.extend(color_values)
135135
arrow_kw['color'] = cmap(norm(color_values[n]))
136136

137-
p = patches.FancyArrowPatch(arrow_tail, arrow_head, **arrow_kw)
137+
p = patches.FancyArrowPatch(arrow_tail,
138+
arrow_head,
139+
transform=transform,
140+
**arrow_kw)
138141
axes.add_patch(p)
139142

140-
lc = mcollections.LineCollection(streamlines, **line_kw)
143+
lc = mcollections.LineCollection(streamlines,
144+
transform=transform,
145+
**line_kw)
141146
if use_multicolor_lines:
142147
lc.set_array(np.asarray(line_colors))
143148
lc.set_cmap(cmap)
Binary file not shown.
74.7 KB
Loading

0 commit comments

Comments
 (0)