Search code examples
pythonmatplotlibhistogram

Histogram bar width


I am using matplotlib histogram histtype='bar' to plot four datasets together. What it automatically does is that it changes the width of the bar of each dataset relative to the size of the dataset. I want to have equal bar width for all four datasets.

[n, bins, patches = plt.hist(
    [np.sort(x2), np.sort(x3), np.sort(x4), np.sort(x5)],
    bins=np.logspace(np.log10(8e-8), np.log10(1), 7),
    histtype='bar', 
    weights=[w2,w3,w4,w5],
    label=[
        '$\Delta {{\chi}^2} <40$',
        '$u_0 < 0.045$',
        '${s}_{fitted} >5$',
        'Multi-peaked'
    ]
)][1]

My current plot


Solution

  • The bins are actually the same size (numeric value) for each set but your x axis is logarithmic so it looks like the width is different. Try to plot a single dataset in a linear x axis (not logarithmic) and you will see that the widths of the bars are the same.