0
votes

I have an array called gender, which stores the number of sales made by males and females. I am trying to create a stacked bar chart of these items but the plot shows the stacked bar chart at 7 on the y axis changing to the next stack which goes up to 14 on the y axis, but should instead go up to 28.

    gender = [[7],[14]]
    attributes = ("Male & Female")
    y_pos = np.arange(len(attributes))

    plt.bar(y_pos, gender[0], align='center', alpha=0.5)
    plt.bar(y_pos, gender[1], align='center', alpha=0.5)
    plt.xticks(y_pos, attributes)
    plt.ylabel('Volume')
    plt.xlabel('Customer Attributes')
    plt.title('Number of Customer Types')
    plt.show()
1
I do not understand what you are trying to obtain with a bar chart made via 2d array. Can you upload a sketch of it? ps. gender is a list of lists, not a 2d-array, moreover I guess you want to define attributes this way attributes = ("Male", "Female") - Andrea
I corrected the attribute typo - that was a mistake on my part. I was going to use a 2-dimensional array to stack the charts? - tankarzAscension

1 Answers

0
votes

I am not entirely sure if I understand your question correctly. But if you want the second bar to sit on top of the first bar, you can achieve that by setting the bottom parameter of the second bar to the height of the first, like this:

gender = [(7),(14)]
attributes = ["Male & Female"]
y_pos = np.arange(len(attributes))

plt.bar(y_pos, gender[0], align='center', alpha=0.5)
plt.bar(y_pos, gender[1], align='center', alpha=0.5, bottom=gender[0])
plt.xticks(y_pos, attributes)
plt.ylabel('Volume')
plt.xlabel('Customer Attributes')
plt.title('Number of Customer Types')
plt.show()

enter image description here