Home > OS >  ValueError: shape mismatch: objects cannot be broadcast to a single shape when plotting a bar
ValueError: shape mismatch: objects cannot be broadcast to a single shape when plotting a bar

Time:01-12

I tried to make a bar plot.

purchase_value

Buy_Coffee Buy_ColdDrinks Buy_Juices Buy_Pastries Buy_Sandwiches
0 0 1 0 1 0
1 1 0 0 0 0
2 1 0 0 0 1
3 1 0 0 0 0
4 1 0 0 0 1
5 1 0 0 0 0
plt.bar(purchase_value.index,
    purchase_value.value_counts(), 
    width=0.5, 
    bottom=None, 
    align='center', 
    color=['lightsteelblue', 
           'cornflowerblue', 
           'royalblue', 
           'midnightblue', 
           'darkblue'])
plt.xticks(rotation='vertical')
plt.show()

But it turned out

ValueError: shape mismatch: objects cannot be broadcast to a single shape

CodePudding user response:

When you call enter image description here


If you would to draw the sum of each item for each column, you should use:

fig, ax = plt.subplots()

purchase_value.sum(axis = 0).plot(kind = 'bar',
                                  ax = ax,
                                  stacked = True, 
                                  width=0.5, 
                                  bottom=0, 
                                  align='center', 
                                  color=['lightsteelblue', 
                                         'cornflowerblue', 
                                         'royalblue', 
                                         'midnightblue', 
                                         'darkblue'])

plt.tight_layout()

plt.show()

enter image description here

  •  Tags:  
  • Related