Reputation:
I am plotting a bar graph using matplotlib. Before I has only two sets of bars in two locations (4 bars in total). So my_list
was a 4x2 matrix and my script worked just fine.
I added another set of data (so now my array is 6x2) and added these new bars to my y1_means
. However, I'm getting the following error:
ValueError: incompatible sizes: argument 'height' must be length 2 or scalar
* UPDATE *
I added another bar (rects3
) and reduced the y_means
to two values. It works, but the last two bars are stacked.
I looked at the other posts related to this error, but couldn't find my solution. Can anybody help? Here's my script:
#!/usr/bin/python
import numpy as np
import matplotlib.pyplot as plt
mylist = [[1061.8065108865587, 242.42977414163315],
[0.17619757021099161, 0.090235256942903783],
[0.12829524580453561, 0.083100278521638746],
[580.78192382619045, 367.56899594357981],
[0.76475391750505795, 0.42257691834032352],
[0.50712851564326855, 0.14217924391430981]]
###### Plot Script ######
N = 2
y1_means = (mylist[1][0], mylist[5][0])
y1_std = (mylist[1][1], mylist[5][1])
ind = np.arange(N) # the x locations for the groups
width = 0.3 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(ind, y1_means, width, color='r', alpha=0.3, yerr=y1_std)
y2_means = (mylist[1][0], mylist[4][0])
y2_std = (mylist[1][1], mylist[4][1])
rects2 = ax.bar(ind + width, y2_means, width, color='b', alpha=0.3, yerr=y2_std)
y3_means = (mylist[2][0], mylist[5][0])
y3_std = (mylist[2][1], mylist[5][1])
rects3 = ax.bar(ind + width, y3_means, width, color='b', alpha=0.3, yerr=y3_std)
# add some text for labels, title and axes ticks
ax.set_ylabel('RTT')
ax.set_xticks(ind + width / 2)
#ax.grid(True)
ax.yaxis.grid('on')
ax.set_xticklabels(('Label-1', 'Label-2'))
fig.tight_layout(rect=[0, 0.05, 1, 0.95])
plt.figlegend( (rects1[0], rects2[0], rects3[0]), ('Scen-1', 'Scen-2', 'Scen-3'),
loc = 'lower center', ncol=5, labelspacing=0. )
plt.show()
Upvotes: 1
Views: 1746
Reputation:
I ended up solving the issue. I need to add another bar plot. So each bar y_means
takes two values and updated the location of the bars! Here's my updated code:
###### Plot Script ######
N = 2
y1_means = (mylist[0][0], mylist[3][0])
y1_std = (mylist[0][1], mylist[3][1])
ind = np.arange(N) # the x locations for the groups
width = 0.3 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(ind - width, y1_means, width, color='r', alpha=0.3, yerr=y1_std)
y2_means = (mylist[1][0], mylist[4][0])
y2_std = (mylist[1][1], mylist[4][1])
rects2 = ax.bar(ind, y2_means, width, color='b', alpha=0.3, yerr=y2_std)
y3_means = (mylist[2][0], mylist[5][0])
y3_std = (mylist[2][1], mylist[5][1])
rects3 = ax.bar(ind + width, y2_means, width, color='b', alpha=0.3, yerr=y2_std)
# add some text for labels, title and axes ticks
ax.set_ylabel('RTT')
ax.set_xticks(ind + width / 2)
#ax.grid(True)
ax.yaxis.grid('on')
ax.set_xticklabels(('Label-1', 'Label-2'))
fig.tight_layout(rect=[0, 0.05, 1, 0.95])
plt.figlegend( (rects1[0], rects2[0], rects3[0]), ('Scen-1', 'Scen-2', 'Scen-3'),
loc = 'lower center', ncol=5, labelspacing=0. )
plt.show()
Upvotes: 1