Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f5899e1

Browse files
committed
spines: attempt to fix initial placement bug
svn path=/trunk/matplotlib/; revision=8047
1 parent c3693f6 commit f5899e1

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

lib/matplotlib/axes.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,9 +2065,6 @@ def set_xlim(self, xmin=None, xmax=None, emit=True, **kwargs):
20652065
other.figure.canvas is not None):
20662066
other.figure.canvas.draw_idle()
20672067

2068-
for loc in ('bottom','top'):
2069-
self.spines[loc].set_bounds(xmin,xmax)
2070-
20712068
return xmin, xmax
20722069

20732070
def get_xscale(self):
@@ -2242,9 +2239,6 @@ def set_ylim(self, ymin=None, ymax=None, emit=True, **kwargs):
22422239
other.figure.canvas is not None):
22432240
other.figure.canvas.draw_idle()
22442241

2245-
for loc in ('left','right'):
2246-
self.spines[loc].set_bounds(ymin,ymax)
2247-
22482242
return ymin, ymax
22492243

22502244
def get_yscale(self):

lib/matplotlib/spines.py

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import matplotlib.patches as mpatches
1212
import matplotlib.path as mpath
1313
import matplotlib.cbook as cbook
14+
import numpy as np
1415
import warnings
1516

1617
class Spine(mpatches.Patch):
@@ -57,6 +58,8 @@ def __init__(self,axes,spine_type,path,**kwargs):
5758
self.set_zorder(2.5)
5859
self.set_transform(self.axes.transData) # default transform
5960

61+
self._bounds = None # default bounds
62+
6063
# Defer initial position determination. (Not much support for
6164
# non-rectangular axes is currently implemented, and this lets
6265
# them pass through the spines machinery without errors.)
@@ -138,6 +141,39 @@ def cla(self):
138141
if self.axis is not None:
139142
self.axis.cla()
140143

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+
141177
def _calc_offset_transform(self):
142178
"""calculate the offset transform performed by the spine"""
143179
self._ensure_position_is_set()
@@ -280,17 +316,10 @@ def get_spine_transform(self):
280316
raise ValueError("unknown spine_transform type: %s"%what)
281317

282318
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)
294323

295324
@classmethod
296325
def linear_spine(cls, axes, spine_type, **kwargs):

0 commit comments

Comments
 (0)