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


enter image description here


closed as off-topic by oW_, Mark.F, Siong Thye Goh, Toros91, OmG Mar 3 at 15:41

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about data science, within the scope defined in the help center." – oW_, Mark.F, Siong Thye Goh, Toros91, OmG
If this question can be reworded to fit the rules in the help center, please edit the question.


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.


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$ – Filip Feb 28 at 21:43

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