df = pd.DataFrame(raw_data)
raw_data = {'X':[x1,x2,x3], 'Y':[y1,y2,y3]}
r =np.arange(len(df))
totals = [n+m for n,m in zip(df['X'], df['Y'])]
X = [n / m * 100 for n,m in zip(df['X'], totals)]
Y = [n / m * 100 for n,m in zip(df['Y'], totals)]
barWidth = 0.8
names = ("R 1","R 2","R 3")
plt.bar(r, X, color='r', edgecolor='black', width=barWidth,label="X")
plt.bar(r, Y, bottom=X, color='c', edgecolor='black', width=barWidth,label="Y")
plt.title("TEST - Stacked bar graph (%)",y=1.05,fontsize=20)
plt.xticks(r, names)
plt.xlabel("Vrsta obrazovanja",fontsize=15)
plt.ylabel("Precentage %",fontsize=15)
plt.legend(loc="lower center")
plt.show()
$\begingroup$
$\endgroup$
Add a comment
|
1 Answer
$\begingroup$
$\endgroup$
1
I think the way you are setting the DataFrame might be the problem. It seems that x1, x2, x3
and y1, y2, y3
are all lists of 1 element, so your code will use these lists assuming them as numbers and trigger the exception you see.
Try,
raw_data = {'X':[x1[0],x2[0],x3[0]], 'Y':[y1[0],y2[0],y3[0]]}
-
$\begingroup$ do you maybe know how to put percentage (%) into bars? $\endgroup$– pf_manFeb 28, 2019 at 21:43