How to plot several line charts in one figure (overlay/groupby)

醉酒当歌 提交于 2019-12-24 03:08:35

问题


I would like to illustrate the change in one variable for several persons in my data over time. I have several issues with basic commands here.

Here is my data:

import pandas as pd
df = pd.DataFrame({'year': ['1988', '1989', '1990', '1988', '1989', '1990', '1988', '1989', '1990'],
                   'id': ['1', '1', '1', '2', '2', '2', '3', '3', '3'],
                   'money': ['5', '7', '8', '8', '3', '3', '7', '8', '10']}).astype(int)

df.info()
df

I tried to make use of matplotlib and started to loop for each of my unique IDs. I'm new to this package. First, how can I specify for each plot that only 3 points are connected for a line, not all? Second, how can I overlay those plots in one figure?

import matplotlib.pyplot as plt

for i in df.id.unique():
        df.plot.line(x='year', y='money')

回答1:


Since you have tagged matplotlib, one solution is to check for the id while looping through the DataFrame before plotting using df[df['id']==i].

To overlay those plots in one figure, create a figure object and pass the axis ax to the df.plot() function.

import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({'year': ['1988', '1989', '1990', '1988', '1989', '1990', '1988', '1989', '1990'],
                   'id': ['1', '1', '1', '2', '2', '2', '3', '3', '3'],
                   'money': ['5', '7', '8', '8', '3', '3', '7', '8', '10']}).astype(int)

fig, ax = plt.subplots()

for i in df.id.unique():
    df[df['id']==i].plot.line(x='year', y='money', ax=ax, label='id = %s'%i)
plt.xticks(np.unique(df.year),rotation=45)    

Pandas solution using groupby would look like following. Here you will have to modify the legends later.

df.groupby('id').plot(x='year', y='money',legend=True, ax=ax)

h,l = ax.get_legend_handles_labels()
ax.legend(h, df.id.unique(), fontsize=12)
plt.xticks(np.unique(df.year), rotation=45)



回答2:


Can also be done with a simple pivot

df.pivot(index='year', columns='id', values='money').plot(rot=45)

If some entries are missing years, then this will not plot perfectly, so add an interpolation:

(df.pivot(index='year', columns='id', values='money')
   .apply(pd.Series.interpolate, limit_area='inside')
   .plot())



回答3:


You can also use either groupby:

df.set_index('year').groupby('id').money.plot()

which gives:

or, use seaborn with hue

sns.lineplot(x='year',y='money', hue='id', data=df)

which gives:



来源:https://stackoverflow.com/questions/56632446/how-to-plot-several-line-charts-in-one-figure-overlay-groupby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!