-1
$\begingroup$

enter image description here

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()

enter image description here

$\endgroup$

1 Answer 1

0
$\begingroup$

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]]}
$\endgroup$
1
  • $\begingroup$ do you maybe know how to put percentage (%) into bars? $\endgroup$
    – pf_man
    Feb 28, 2019 at 21:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.