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

Skip to content

Commit c8af84a

Browse files
committed
Axes.hist: rework autoscaling. Closes 2971357.
svn path=/trunk/matplotlib/; revision=8353
1 parent b780c89 commit c8af84a

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

lib/matplotlib/axes.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7396,9 +7396,21 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
73967396
w = [None]*nx
73977397

73987398
# Check whether bins or range are given explicitly. In that
7399-
# case do not autoscale axes.
7399+
# case use those values for autoscaling.
74007400
binsgiven = (cbook.iterable(bins) or range != None)
74017401

7402+
# Save autoscale state for later restoration; turn autoscaling
7403+
# off so we can do it all a single time at the end, instead
7404+
# of having it done by bar or fill and then having to be redone.
7405+
_saved_autoscalex = self.get_autoscalex_on()
7406+
_saved_autoscaley = self.get_autoscaley_on()
7407+
self.set_autoscalex_on(False)
7408+
self.set_autoscaley_on(False)
7409+
7410+
# Save the datalimits for the same reason:
7411+
_saved_bounds = self.dataLim.bounds
7412+
7413+
74027414
hist_kwargs = dict(range=range, normed=bool(normed))
74037415
if np.__version__ < "1.3": # version 1.1 and 1.2
74047416
hist_kwargs['new'] = True
@@ -7503,18 +7515,21 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
75037515

75047516
# adopted from adjust_x/ylim part of the bar method
75057517
if orientation == 'horizontal':
7506-
xmin, xmax = 0, self.dataLim.intervalx[1]
7518+
xmin0 = max(_saved_bounds[0]*0.9, 1e-100)
7519+
xmax = self.dataLim.intervalx[1]
75077520
for m in n:
75087521
xmin = np.amin(m[m!=0]) # filter out the 0 height bins
75097522
xmin = max(xmin*0.9, 1e-100)
7523+
xmin = min(xmin0, xmin)
75107524
self.dataLim.intervalx = (xmin, xmax)
75117525
elif orientation == 'vertical':
7512-
ymin, ymax = 0, self.dataLim.intervaly[1]
7526+
ymin0 = max(_saved_bounds[1]*0.9, 1e-100)
7527+
ymax = self.dataLim.intervaly[1]
75137528
for m in n:
75147529
ymin = np.amin(m[m!=0]) # filter out the 0 height bins
75157530
ymin = max(ymin*0.9, 1e-100)
7531+
ymin = min(ymin0, ymin)
75167532
self.dataLim.intervaly = (ymin, ymax)
7517-
self.autoscale_view()
75187533

75197534
if label is None:
75207535
labels = ['_nolegend_']
@@ -7535,17 +7550,14 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
75357550
lbl = '_nolegend_'
75367551

75377552
if binsgiven:
7538-
self.set_autoscale_on(False)
75397553
if orientation == 'vertical':
7540-
self.autoscale_view(scalex=False, scaley=True)
7541-
XL = self.xaxis.get_major_locator().view_limits(
7542-
bins[0], bins[-1])
7543-
self.set_xbound(XL)
7554+
self.update_datalim([(bins[0],0), (bins[-1],0)], updatey=False)
75447555
else:
7545-
self.autoscale_view(scalex=True, scaley=False)
7546-
YL = self.yaxis.get_major_locator().view_limits(
7547-
bins[0], bins[-1])
7548-
self.set_ybound(YL)
7556+
self.update_datalim([(0,bins[0]), (0,bins[-1])], updatex=False)
7557+
7558+
self.set_autoscalex_on(_saved_autoscalex)
7559+
self.set_autoscaley_on(_saved_autoscaley)
7560+
self.autoscale_view()
75497561

75507562
if nx == 1:
75517563
return n[0], bins, cbook.silent_list('Patch', patches[0])

0 commit comments

Comments
 (0)