Skip to content

Commit

Permalink
Merge pull request #1090 from pelson/external_transform_api
Browse files Browse the repository at this point in the history
External transform api
  • Loading branch information
efiring committed Aug 20, 2012
2 parents dc535b4 + 456a723 commit c0ee100
Show file tree
Hide file tree
Showing 11 changed files with 7,825 additions and 20 deletions.
8 changes: 6 additions & 2 deletions lib/matplotlib/artist.py
Expand Up @@ -3,7 +3,8 @@
import matplotlib
import matplotlib.cbook as cbook
from matplotlib import docstring, rcParams
from transforms import Bbox, IdentityTransform, TransformedBbox, TransformedPath
from transforms import Bbox, IdentityTransform, TransformedBbox, \
TransformedPath, Transform
from path import Path

## Note, matplotlib artists use the doc strings for set and get
Expand Down Expand Up @@ -223,7 +224,7 @@ def set_transform(self, t):
ACCEPTS: :class:`~matplotlib.transforms.Transform` instance
"""
self._transform = t
self._transformSet = True
self._transformSet = t is not None
self.pchanged()

def get_transform(self):
Expand All @@ -233,6 +234,9 @@ def get_transform(self):
"""
if self._transform is None:
self._transform = IdentityTransform()
elif (not isinstance(self._transform, Transform)
and hasattr(self._transform, '_as_mpl_transform')):
self._transform = self._transform._as_mpl_transform(self.axes)
return self._transform

def hitlist(self, event):
Expand Down
7 changes: 4 additions & 3 deletions lib/matplotlib/axes.py
Expand Up @@ -6022,7 +6022,7 @@ def scatter(self, x, y, s=20, c='b', marker='o', cmap=None, norm=None,
edgecolors = edgecolors,
linewidths = linewidths,
offsets = zip(x,y),
transOffset = self.transData,
transOffset = kwargs.pop('transform', self.transData),
)
collection.set_transform(mtransforms.IdentityTransform())
collection.set_alpha(alpha)
Expand Down Expand Up @@ -6550,7 +6550,7 @@ def stackplot(self, x, *args, **kwargs):

def streamplot(self, x, y, u, v, density=1, linewidth=None, color=None,
cmap=None, norm=None, arrowsize=1, arrowstyle='-|>',
minlength=0.1):
minlength=0.1, transform=None):
if not self._hold: self.cla()
lines = mstream.streamplot(self, x, y, u, v,
density=density,
Expand All @@ -6560,7 +6560,8 @@ def streamplot(self, x, y, u, v, density=1, linewidth=None, color=None,
norm=norm,
arrowsize=arrowsize,
arrowstyle=arrowstyle,
minlength=minlength)
minlength=minlength,
transform=transform)
return lines
streamplot.__doc__ = mstream.streamplot.__doc__

Expand Down
13 changes: 10 additions & 3 deletions lib/matplotlib/collections.py
Expand Up @@ -156,9 +156,16 @@ def set_paths(self):
def get_transforms(self):
return self._transforms

def get_offset_transform(self):
t = self._transOffset
if (not isinstance(t, transforms.Transform)
and hasattr(t, '_as_mpl_transform')):
t = t._as_mpl_transform(self.axes)
return t

def get_datalim(self, transData):
transform = self.get_transform()
transOffset = self._transOffset
transOffset = self.get_offset_transform()
offsets = self._offsets
paths = self.get_paths()

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

transform = self.get_transform()
transOffset = self._transOffset
transOffset = self.get_offset_transform()
offsets = self._offsets
paths = self.get_paths()

Expand Down Expand Up @@ -1407,7 +1414,7 @@ def draw(self, renderer):
if not self.get_visible(): return
renderer.open_group(self.__class__.__name__, self.get_gid())
transform = self.get_transform()
transOffset = self._transOffset
transOffset = self.get_offset_transform()
offsets = self._offsets

if self.have_units():
Expand Down
4 changes: 4 additions & 0 deletions lib/matplotlib/contour.py
Expand Up @@ -774,6 +774,8 @@ def __init__(self, ax, *args, **kwargs):
raise ValueError('Either colors or cmap must be None')
if self.origin == 'image': self.origin = mpl.rcParams['image.origin']

self.transform = kwargs.get('transform', None)

self._process_args(*args, **kwargs)
self._process_levels()

Expand Down Expand Up @@ -822,6 +824,7 @@ def __init__(self, ax, *args, **kwargs):
antialiaseds = (self.antialiased,),
edgecolors= 'none',
alpha=self.alpha,
transform=self.transform,
zorder=zorder)
self.ax.add_collection(col)
self.collections.append(col)
Expand All @@ -841,6 +844,7 @@ def __init__(self, ax, *args, **kwargs):
linewidths = width,
linestyle = lstyle,
alpha=self.alpha,
transform=self.transform,
zorder=zorder)
col.set_label('_nolegend_')
self.ax.add_collection(col, False)
Expand Down
6 changes: 3 additions & 3 deletions lib/matplotlib/pyplot.py
Expand Up @@ -3001,7 +3001,7 @@ def stackplot(x, *args, **kwargs):
draw_if_interactive()
finally:
ax.hold(washold)

return ret

# This function was autogenerated by boilerplate.py. Do not edit as
Expand Down Expand Up @@ -3047,7 +3047,7 @@ def step(x, y, *args, **kwargs):
@_autogen_docstring(Axes.streamplot)
def streamplot(x, y, u, v, density=1, linewidth=None, color=None, cmap=None,
norm=None, arrowsize=1, arrowstyle='-|>', minlength=0.1,
hold=None):
transform=None, hold=None):
ax = gca()
# allow callers to override the hold state by passing hold=True|False
washold = ax.ishold()
Expand All @@ -3058,7 +3058,7 @@ def streamplot(x, y, u, v, density=1, linewidth=None, color=None, cmap=None,
ret = ax.streamplot(x, y, u, v, density=density, linewidth=linewidth,
color=color, cmap=cmap, norm=norm,
arrowsize=arrowsize, arrowstyle=arrowstyle,
minlength=minlength)
minlength=minlength, transform=transform)
draw_if_interactive()
finally:
ax.hold(washold)
Expand Down
9 changes: 4 additions & 5 deletions lib/matplotlib/quiver.py
Expand Up @@ -408,10 +408,11 @@ def __init__(self, ax, *args, **kw):
self.width = kw.pop('width', None)
self.color = kw.pop('color', 'k')
self.pivot = kw.pop('pivot', 'tail')
self.transform = kw.pop('transform', ax.transData)
kw.setdefault('facecolors', self.color)
kw.setdefault('linewidths', (0,))
collections.PolyCollection.__init__(self, [], offsets=self.XY,
transOffset=ax.transData,
transOffset=self.transform,
closed=False,
**kw)
self.polykw = kw
Expand Down Expand Up @@ -529,8 +530,6 @@ def _angles_lengths(self, U, V, eps=1):
lengths = np.absolute(dxy[:,0] + dxy[:,1]*1j) / eps
return angles, lengths



def _make_verts(self, U, V):
uv = (U+V*1j)
if self.angles == 'xy' and self.scale_units == 'xy':
Expand Down Expand Up @@ -592,7 +591,6 @@ def _make_verts(self, U, V):

return XY


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

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

self.set_UVC(u, v, c)
Expand Down
11 changes: 8 additions & 3 deletions lib/matplotlib/streamplot.py
Expand Up @@ -14,7 +14,7 @@

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

p = patches.FancyArrowPatch(arrow_tail, arrow_head, **arrow_kw)
p = patches.FancyArrowPatch(arrow_tail,
arrow_head,
transform=transform,
**arrow_kw)
axes.add_patch(p)

lc = mcollections.LineCollection(streamlines, **line_kw)
lc = mcollections.LineCollection(streamlines,
transform=transform,
**line_kw)
if use_multicolor_lines:
lc.set_array(np.asarray(line_colors))
lc.set_cmap(cmap)
Expand Down
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c0ee100

Please sign in to comment.