aminnj / yahist

1D and 2D histogram objects
10 stars 3 forks source link

issue with first bin in plot_stack #1

Closed aminnj closed 4 years ago

aminnj commented 4 years ago

utils.plot_stack is essentially

    bottom = 0.0
    for h in hists:
        h.plot(bottom=bottom, **kwargs)
        bottom += h.counts

which gives a weird result for the first bin (Hist1D.plot() uses histtype="stepfilled" by default): image however, when specifying histtype="bar", the issue is not present: image Essentially it boils down to the difference in behavior between pairs of lines:

fig, ax = plt.subplots()
# looks fine
ax.hist(h1.bin_centers, h1.edges, weights=h1.counts, histtype="bar")
ax.hist(h2.bin_centers, h2.edges, weights=h2.counts, bottom=h1.counts, histtype="bar")
# first bin starts at (0,0)
ax.hist(h1.bin_centers, h1.edges, weights=h1.counts, histtype="stepfilled")
ax.hist(h2.bin_centers, h2.edges, weights=h2.counts, bottom=h1.counts, histtype="stepfilled")
# also fine (but not filled, of course)
ax.hist(h1.bin_centers, h1.edges, weights=h1.counts, histtype="step")
ax.hist(h2.bin_centers, h2.edges, weights=h2.counts, bottom=h1.counts, histtype="step")
aminnj commented 4 years ago

Minimal reproducer

import matplotlib.pyplot as plt

centers = [0.5, 1.5, 2.5, 3.5]
edges = [0., 1., 2., 3., 4.]
counts1 = counts2 = [5., 4., 3., 2.]

fig, axs = plt.subplots(3, sharex=True)
for ax, histtype in zip(axs.flat, ["bar", "stepfilled", "step"]):
    ax.hist(centers, edges, weights=counts1, histtype=histtype)
    ax.hist(centers, edges, weights=counts2, bottom=counts1, histtype=histtype)

image

aminnj commented 4 years ago

I updated to matplotlib 3.3.1 and this issue is gone. Looks like it was fixed upstream!