0

Using MatPlotLib in Python, i have tried this:

labels = range(len(df["Investigations"])) #column vector of strings - hid for the example's sake
normals = df["No signs of pathology"] #column vector of integers
abnormals = df["Abnormal"]  #column vector of integers
width = 0.5  

fig, ax = plt.subplots()

#set size
fig.set_figheight(7.5)

#labels
ax.barh(labels, normals, width, label='No signs of pathology', color='blue')
ax.barh(labels, abnormals, width, label='Abnormal', color='red')

# grid lines
ax.set_axisbelow(True)
ax.xaxis.grid(color='gray', linestyle='solid', alpha=0.2)

#read top-to-bottom


#Title and legend
ax.set_title('Investigations')
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),
          fancybox=True, shadow=True, ncol=5)

plt.show()

This gives me a chart that looks like the attachement, but i can see that the red, and the blue bars does not stack but overlaps. They should be stacking. Can anyone help me solve this issue? :)

Example of stacked bar chart

Vuptiman
  • 1
  • 2
  • You need to change the starting position of the second list of bars: `ax.barh(labels, abnormals, width, left=normals, label='Abnormal', color='red')`. – JohanC Dec 04 '22 at 22:12

0 Answers0