Kaleend
Kaleend

Reputation: 58

How would I plot a bar graph with this data

I had a dataframe and i filtered out the columns to get x and did y = vl.groupby(['Model Year','Manufacturer']).size() to get value of y. Now, when I try to plot it I got an error. This is my first time trying to might be missing something. Could you please help me.

#ax = plt.subplot(111)
#ax.bar(x, y,width=0.1,color='b',align='center')

#plt.show()

x: array(['1991', '1992', '1993', '1994'], dtype=object)

y: 1991        Chevrolet             1
            Ford                  1
1992        Chevrolet             2
            Dodge                 2
            Ford                  1
1993        Chevrolet             3
            Dodge                 2
            Ford                  2
1994        Dodge                 3
            Ford                  2
dtype: int64

What I am trying to plot is year vs number of cars by manufacturer.

Upvotes: 2

Views: 860

Answers (1)

jezrael
jezrael

Reputation: 863541

I think you need add unstack for creating columns with last level of MultiIndex and then DataFrame.plot.bar:

print (y.unstack(fill_value=0))
      Chevrolet  Dodge  Ford
1991          1      0     1
1992          2      2     1
1993          3      2     2
1994          0      3     2

y = vl.groupby(['Model Year','Manufacturer']).size()
y.unstack(fill_value=0).plot.bar(width=0.1, align='center')

plt.show()

graph1

Another possible solution with crosstab:

pd.crosstab(vl['Model Year'], vl['Manufacturer']).plot.bar(width=0.1, align='center')
plt.show()

Upvotes: 2

Related Questions