Skip to content

Make logscale bar/hist autolimits more consistents. #14581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions doc/api/next_api_changes/2019-06-19-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
log-scale bar() / hist() autolimits change
``````````````````````````````````````````

The autolimits computation in `~.Axes.bar` and `~.Axes.hist` when the axes
already uses log-scale has changed to match the computation when the axes is
switched to log-scale after the call to `~.Axes.bar` and `~.Axes.hist`, and
when calling ``bar(..., log=True)`` / ``hist(..., log=True)``: if there are
at least two different bar heights, add the normal axes margins to them (in
log-scale); if there is only a single bar height, expand the axes limits by one
order of magnitude around it and then apply axes margins.
53 changes: 3 additions & 50 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2303,20 +2303,12 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
label = kwargs.pop('label', '')
tick_labels = kwargs.pop('tick_label', None)

adjust_ylim = False
adjust_xlim = False

y = bottom # Matches barh call signature.
if orientation == 'vertical':
if bottom is None:
if self.get_yscale() == 'log':
adjust_ylim = True
if y is None:
y = 0

elif orientation == 'horizontal':
if x is None:
if self.get_xscale() == 'log':
adjust_xlim = True
x = 0

if orientation == 'vertical':
Expand Down Expand Up @@ -2429,21 +2421,6 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
else:
errorbar = None

if adjust_xlim:
xmin, xmax = self.dataLim.intervalx
xmin = min(w for w in width if w > 0)
if xerr is not None:
xmin = xmin - np.max(xerr)
xmin = max(xmin * 0.9, 1e-100)
self.dataLim.intervalx = (xmin, xmax)

if adjust_ylim:
ymin, ymax = self.dataLim.intervaly
ymin = min(h for h in height if h > 0)
if yerr is not None:
ymin = ymin - np.max(yerr)
ymin = max(ymin * 0.9, 1e-100)
self.dataLim.intervaly = (ymin, ymax)
self._request_autoscale_view()

bar_container = BarContainer(patches, errorbar, label=label)
Expand Down Expand Up @@ -6741,30 +6718,8 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
if log:
if orientation == 'horizontal':
self.set_xscale('log', nonposx='clip')
logbase = self.xaxis._scale.base
else: # orientation == 'vertical'
self.set_yscale('log', nonposy='clip')
logbase = self.yaxis._scale.base

# Setting a minimum of 0 results in problems for log plots
if np.min(bottom) > 0:
minimum = np.min(bottom)
elif density or weights is not None:
# For data that is normed to form a probability density,
# set to minimum data value / logbase
# (gives 1 full tick-label unit for the lowest filled bin)
ndata = np.array(tops)
minimum = (np.min(ndata[ndata > 0])) / logbase
else:
# For non-normed (density = False) data,
# set the min to 1 / log base,
# again so that there is 1 full tick-label unit
# for the lowest bin
minimum = 1.0 / logbase

y[0], y[-1] = minimum, minimum
else:
minimum = 0

if align == 'left':
x -= 0.5*(bins[1]-bins[0])
Expand All @@ -6785,8 +6740,6 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
# set the top of this polygon
y[1:2*len(bins)-1:2], y[2:2*len(bins):2] = (m + bottom,
m + bottom)
if log:
y[y < minimum] = minimum
if orientation == 'horizontal':
xvals.append(y.copy())
yvals.append(x.copy())
Expand All @@ -6809,9 +6762,9 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
for patch_list in patches:
for patch in patch_list:
if orientation == 'vertical':
patch.sticky_edges.y.append(minimum)
patch.sticky_edges.y.append(0)
elif orientation == 'horizontal':
patch.sticky_edges.x.append(minimum)
patch.sticky_edges.x.append(0)

# we return patches, so put it back in the expected order
patches.reverse()
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading