Reputation: 229
I have seen many posts about plotting using the bar chart side by side. I have some arrays and the X coordinates are not necessarily the same. If I try to add and subtract w
with bins1
, bins2
, and/or bins3
I get an error. I also tried to use arrange(bins1)
from numpy
and get an error.
import matplotlib.pyplot as plt
#first data set
bins1= [1,2,3,4,5,6,7]
frequency1= [2,27,70,91,45,11,7]
#second data set
bins2= [5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
frequency2= [2,4,6,7,6,13,4,3,13,4,5,7,6,1,1,1]
#third data set
bins3= [2,3,4,5,6,7,8,9,10,11,12,14]
frequency3= [1,8,21,27,33,14,17,23,4,2,1,1]
#width
w= 0.3
#plot
plt.figure()
#modify to print side by side
plt.bar(bins1, frequency1, width=w, color='r', align='center', label='QD128')
plt.bar(bins3, frequency3, width=w, color='g', align='center', label='QD16')
plt.bar(bins2, frequency2, width=w, color='b', align='center', label='QD1')
plt.show()
This is an image of what the current code looks like:
And this is more what I am going for, but it is from a histogram plot and not a bar graph plot:
Upvotes: 1
Views: 917
Reputation: 229
I was able to get what I was looking for using a histogram instead of a bar graph:
import matplotlib.pyplot as plt
bins1= [1,2,3,4,5,6,7]
frequency1= [2,27,70,91,45,11,7]
bins2= [5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
frequency2= [2,4,6,7,6,13,4,3,13,4,5,7,6,1,1,1]
bins3= [2,3,4,5,6,7,8,9,10,11,12,14]
frequency3= [1,8,21,27,33,14,17,23,4,2,1,1]
plt.figure()
plt.hist([bins1, bins2, bins3], 20, weights=[frequency1, frequency2, frequency3],
histtype='bar', label=['QD128','QD1','QD16'])
plt.legend()
plt.show()
The result looks like:
Upvotes: 1