|
11 | 11 | import matplotlib.patches as mpatches
|
12 | 12 | import matplotlib.path as mpath
|
13 | 13 | import matplotlib.cbook as cbook
|
| 14 | +import numpy as np |
14 | 15 | import warnings
|
15 | 16 |
|
16 | 17 | class Spine(mpatches.Patch):
|
@@ -57,6 +58,8 @@ def __init__(self,axes,spine_type,path,**kwargs):
|
57 | 58 | self.set_zorder(2.5)
|
58 | 59 | self.set_transform(self.axes.transData) # default transform
|
59 | 60 |
|
| 61 | + self._bounds = None # default bounds |
| 62 | + |
60 | 63 | # Defer initial position determination. (Not much support for
|
61 | 64 | # non-rectangular axes is currently implemented, and this lets
|
62 | 65 | # them pass through the spines machinery without errors.)
|
@@ -138,6 +141,39 @@ def cla(self):
|
138 | 141 | if self.axis is not None:
|
139 | 142 | self.axis.cla()
|
140 | 143 |
|
| 144 | + def _adjust_location(self): |
| 145 | + """automatically set spine bounds to the view interval""" |
| 146 | + |
| 147 | + if self.spine_type == 'circle': |
| 148 | + return |
| 149 | + |
| 150 | + if self._bounds is None: |
| 151 | + if self.spine_type in ('left','right'): |
| 152 | + low,high = self.axes.viewLim.intervaly |
| 153 | + elif self.spine_type in ('top','bottom'): |
| 154 | + low,high = self.axes.viewLim.intervalx |
| 155 | + else: |
| 156 | + raise ValueError('unknown spine spine_type: %s'%self.spine_type) |
| 157 | + else: |
| 158 | + low,high = self._bounds |
| 159 | + |
| 160 | + v1 = self._path.vertices[:] # copy |
| 161 | + assert v1.shape == (2,2), 'unexpected vertices shape' |
| 162 | + if self.spine_type in ['left','right']: |
| 163 | + v1[0,1] = low |
| 164 | + v1[1,1] = high |
| 165 | + elif self.spine_type in ['bottom','top']: |
| 166 | + v1[0,0] = low |
| 167 | + v1[1,0] = high |
| 168 | + else: |
| 169 | + raise ValueError('unable to set bounds for spine "%s"'%spine_type) |
| 170 | + self._path.vertices = v1 # replace |
| 171 | + |
| 172 | + @allow_rasterization |
| 173 | + def draw(self, renderer): |
| 174 | + self._adjust_location() |
| 175 | + return super( Spine, self).draw(renderer) |
| 176 | + |
141 | 177 | def _calc_offset_transform(self):
|
142 | 178 | """calculate the offset transform performed by the spine"""
|
143 | 179 | self._ensure_position_is_set()
|
@@ -280,17 +316,10 @@ def get_spine_transform(self):
|
280 | 316 | raise ValueError("unknown spine_transform type: %s"%what)
|
281 | 317 |
|
282 | 318 | def set_bounds( self, low, high ):
|
283 |
| - v1 = self._path.vertices[:] # copy |
284 |
| - assert v1.shape == (2,2), 'unexpected vertices shape' |
285 |
| - if self.spine_type in ['left','right']: |
286 |
| - v1[0,1] = low |
287 |
| - v1[1,1] = high |
288 |
| - elif self.spine_type in ['bottom','top']: |
289 |
| - v1[0,0] = low |
290 |
| - v1[1,0] = high |
291 |
| - else: |
292 |
| - raise ValueError('unable to set bounds for spine "%s"'%spine_type) |
293 |
| - self._path.vertices = v1 # replace |
| 319 | + if self.spine_type == 'circle': |
| 320 | + raise ValueError( |
| 321 | + 'set_bounds() method incompatible with circular spines') |
| 322 | + self._bounds = (low, high) |
294 | 323 |
|
295 | 324 | @classmethod
|
296 | 325 | def linear_spine(cls, axes, spine_type, **kwargs):
|
|
0 commit comments