FunnyChef
FunnyChef

Reputation: 1946

How to chart groupby counts in pyplot?

How do I generate a pyplot line chart from the following groupby where the y values represent the counts for each 'enabled_flag' value?

df = pd.DataFrame({'date': ['2019-10-06', '2019-10-06','2019-10-06', '2019-10-07', '2019-10-07'], 
                   'enabled_flag': ['y', 'y','n', 'y', 'n']})

df.groupby(['date','enabled_flag'])['enabled_flag'].count()

So, the x-axis is 'date' and y has two axis, one for the 'Y' counts and the other for the 'N' counts.

Upvotes: 0

Views: 31

Answers (2)

Quang Hoang
Quang Hoang

Reputation: 150735

You could do:

(df.groupby(['date','enabled_flag'])['enabled_flag'].count()
   .unstack()
   .plot()
)

Or

(df.groupby('date')['enabled_flag'].value_counts()
   .unstack()
   .plot()
)

Output:

enter image description here

Upvotes: 1

Raj
Raj

Reputation: 86

plt.gca().set_color_cycle(['green', 'yellow'])
for dtype in ['Y', 'N']:
  plt.plot(
     'Datetime',
     dtype,
     data=df,
     label=dtype,
     alpha=0.8
     )
plt.legend()
plt.grid() 
plt.show() 

Try this. Does this work ?

Upvotes: 0

Related Questions