Reputation: 515
def plotbars(hists, bins, labels, filename):
plt.figure()
center = (bins[:-1] + bins[1:]) / 2
width = 0.7 * (bins[1] - bins[0])
for hist in hists:
plt.bar(center, hist, align='center', width=width)
plt.legend(labels)
plt.savefig(filename)
If I plot histograms like this plotbars([hist1, hist2], bins, ['hist1', 'hist2'], 'histo.png')
, this plots the histograms on top of each other. I want to the bars of the histogram to be adjacent. The bin width for each are equal.
Upvotes: 0
Views: 3574
Reputation: 2471
try using plt.hist instead of plt.bar.
hist1=[x for x in range(0,10)]*10
hist2= [x for x in range(0,10)]*2
plt.hist([hist1,hist2],bins = 10)
Yields this image:
Is this how you would like to see your graph?
Update:
x axis can be properly formatted using the following.
bin_list = [x for x in range(0,11) ]
plt.hist([hist1,hist2], bins=bin_list ,align='left')
Resulting in a proper spaced x axis. For some reason it appears bins must be set as a list for proper spacing of x values.
https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.xticks
For further tweaking you can try to play around with plt.xticks().
Upvotes: 1