Graham Streich Graham Streich -3 years ago 107
Python Question

Creating legend for graph with multiple lines representing a 'group'

From a dataframe in pandas 'g' I have the following data:

index Speaker Date ARI Flesch Kincaid
0 Alan Greenspan 1996 15.234878 34.669383 14.533217
1 Alan Greenspan 1997 16.235605 31.415163 15.335869
11 Alan S. Blinder 2002 14.299481 41.847836 13.681203
12 Alan S. Blinder 2003 NaN NaN NaN
14 Alice M. Rivlin 1996 15.828971 33.394999 15.211662


With the code below I have been able to produce the following graph:

s = data
s['Date'] = pd.to_datetime(s['Date'], format='%Y-%m-%d %H:%M:%S')
s = s.set_index(['Date'])
grouped = s.groupby('Speaker').resample('AS').mean()
grouped = grouped.reset_index()
g = grouped.reset_index()
g["Date"] = g["Date"].dt.year
g.plot(x='Date', y='Flesch', colormap = cm.cubehelix, legend=True,
title="Auto", figsize=(12,10))


My current graph

I would like the graph to include different colors for each line and place a legend that notes which "Speaker" is associated with each line. Any help would be appreciated!

Answer Source

Let's try something like this:

df.pivot_table(values='Flesch',index='Date',columns='Speaker').reset_index().plot(x='Date',colormap= cm.cubehelix)

enter image description here

(I just added that marker to show those single points in your test data.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download