Plotting a Pandas DataSeries.GroupBy

57,930

Solution 1

You can make the plots by looping over the groups from groupby:

import matplotlib.pyplot as plt

for title, group in df.groupby('ModelID'):
    group.plot(x='saleDate', y='MeanToDate', title=title)

See for more information on plotting with pandas dataframes:
http://pandas.pydata.org/pandas-docs/stable/visualization.html
and for looping over a groupby-object:
http://pandas.pydata.org/pandas-docs/stable/groupby.html#iterating-through-groups

Solution 2

Example with aggregation:

I wanted to do something like the following, if pandas had a colour aesthetic like ggplot:

aggregated = df.groupby(['model', 'training_examples']).aggregate(np.mean)
aggregated.plot(x='training_examples', y='accuracy', label='model')

(columns: model is a string, training_examples is an integer, accuracy is a decimal)

But that just produces a mess.

Thanks to joris's answer, I ended up with:

for index, group in df.groupby(['model']):
    group_agg = group.groupby(['training_examples']).aggregate(np.mean)
    group_agg.plot(y='accuracy', label=index)

I found that title= was just replacing the single title of the plot on each loop iteration, but label= does what you'd expect -- after running plt.legend(), of course.

Share:
57,930
Nyxynyx
Author by

Nyxynyx

Hello :) I have no formal education in programming :( And I need your help! :D These days its web development: Node.js Meteor.js Python PHP Laravel Javascript / jQuery d3.js MySQL PostgreSQL MongoDB PostGIS

Updated on July 09, 2022

Comments

  • Nyxynyx
    Nyxynyx almost 2 years

    I am new to python and pandas, and have the following DataFrame.

    How can I plot the DataFrame where each ModelID is a separate plot, saledate is the x-axis and MeanToDate is the y-axis?

    Attempt

    data[40:76].groupby('ModelID').plot()
    

    enter image description here

    DataFrame

    enter image description here

  • gented
    gented about 7 years
    The plots will be displayed on independent figures even without the plt.figure() first.
  • glS
    glS over 6 years
    another nice tutorial on using groupby is here: chrisalbon.com/python/pandas_apply_operations_to_groups.html