Repository navigation
Closed
Milestone
Description
For histograms, Axes.dataLim is supposed to use the limits of the actual bins, not the limits of their representation in the plot (which might be different due to a smaller bin width), right? In this case, the last hist call in the code below is setting them incorrectly:
from matplotlib import pyplot as plt
x1 = [ 0.1, 0.2, 0.3, 0.5, 0.75, 1.1, 1.5, 1.9, 2.5, 2.9 ]
x2 = list(x1)
x2.extend([ 1.75, 1.8, 2.2, 2.4, 2.7, 2.8 ])
x1.extend([ -x for x in x1 ])
x2.extend([ -x for x in x2 ])
print("One dataset, explicit bin sequence:")
plt.subplot(221)
plt.minorticks_on()
n, bins, patches = plt.hist(x1, bins=[-3,-2,-1,0,1,2,3])
print("bins: ({}, {})".format(min(bins), max(bins)))
print("datalim: {}".format(plt.gca().dataLim))
print()
print("One dataset, automatic bins:")
plt.subplot(222)
plt.minorticks_on()
n, bins, patches = plt.hist(x1)
print("bins: ({}, {})".format(min(bins), max(bins)))
print("datalim: {}".format(plt.gca().dataLim))
print()
print("Two datasets, explicit bin sequence:")
plt.subplot(223)
plt.minorticks_on()
n, bins, patches = plt.hist([x1,x2], bins=[-3,-2,-1,0,1,2,3], stacked=True)
print("bins: ({}, {})".format(min(bins), max(bins)))
print("datalim: {}".format(plt.gca().dataLim))
print()
print("Two datasets, automatic bins:")
plt.subplot(224)
plt.minorticks_on()
n, bins, patches = plt.hist([x1,x2], stacked=True)
print("bins: ({}, {})".format(min(bins), max(bins)))
print("datalim: {}".format(plt.gca().dataLim))
plt.savefig("datalim_inconsistency.png")Output is:
One dataset, explicit bin sequence:
bins: (-3, 3)
datalim: Bbox(x0=-3.0, y0=0.0, x1=3.0, y1=5.0)
One dataset, automatic bins:
bins: (-2.9, 2.9)
datalim: Bbox(x0=-2.9, y0=0.0, x1=2.9, y1=4.0)
Two datasets, explicit bin sequence:
bins: (-3, 3)
datalim: Bbox(x0=-3.0, y0=0.0, x1=3.0, y1=10.0)
Two datasets, automatic bins:
bins: (-2.9, 2.9)
datalim: Bbox(x0=-2.842, y0=0.0, x1=2.8419999999999996, y1=8.0)
As you can see, it works as I would've expected in the first three calls, but after the fourth call, dataLim is set to (presumably - see image below) the outer bin edges' positions in the plot, as opposed to their actual data values.

This came up as I closed #6872 because it would make the second and third hist calls in the code behave the same way as the fourth (setting dataLim to representation rather than data edges).
matplotlib version: 0cfc7a8
Python 3.5.2
Metadata
Metadata
Assignees
Labels
No labels
Activity
tacaswell commentedon Aug 3, 2016
Why do the bars get narrower for multiple data sets? That makes sense if they are not going to be stacked. It looks like the logic to make the bars narrower than the bins (around line 6000 in _axes.py) pre-dates the logic. I will put in a PR to address this for 2.0.
API: only shrink hist bars if not stacked
API: only shrink hist bars if not stacked
anntzer commentedon Aug 31, 2017
Closed by #6887 unless I am mistaken (then feel free to (request a) reopen).