在Python中,如何基于两个类别的groupby()创建线条图,其中一个类别是图例?

在Python中,如何基于两个类别的groupby()创建线条图,其中一个类别是图例?,python,matplotlib,Python,Matplotlib,我使用此代码按年份和大陆对平均预期寿命进行分组: avg_lifeExp_by_cont_yr = df.groupby(['year','continent'])['lifeExp'].mean() 结果如下所示: 我想创建一个折线图,其中x轴上有年份,y轴上有平均预期寿命,大陆用作图例(因此每个大陆一条线)。您可以使用df.unstack('大陆')将大陆作为列,然后此数据框成为一个2D表格,其中第一列是x,其他列为Y。您可以直接调用plot函数或通过原始matplotlib操作自己控制

我使用此代码按年份和大陆对平均预期寿命进行分组:

avg_lifeExp_by_cont_yr = df.groupby(['year','continent'])['lifeExp'].mean()
结果如下所示:


我想创建一个折线图,其中x轴上有年份,y轴上有平均预期寿命,大陆用作图例(因此每个大陆一条线)。

您可以使用
df.unstack('大陆')
将大陆作为列,然后此数据框成为一个2D表格,其中第一列是x,其他列为Y。您可以直接调用
plot
函数或通过原始matplotlib操作自己控制绘图

感谢您提供的数据,以下是您请求的完整代码示例:

# imports
import pandas as pd
import matplotlib.pyplot as plt
# prepare dataframe
df = pd.read_csv('gapminder.tsv', sep='\t')
df = df.groupby(['year','continent']).lifeExp.mean()

# unstack the `continent` index, to place it as columns
df = df.unstack(level='continent')

# The name of columns would become the name of legend
# when using dataframe plot
df.columns.name = 'Life Expectation'

# Now, we have a 2d talbe, 1st column become to X
# and other columns become to Y
# In [14]: df.head()
# Out[14]:
# Life Expectation     Africa  Americas       Asia     Europe  Oceania
# year
# 1952              39.135500  53.27984  46.314394  64.408500   69.255
# 1957              41.266346  55.96028  49.318544  66.703067   70.295
# 1962              43.319442  58.39876  51.563223  68.539233   71.085
# 1967              45.334538  60.41092  54.663640  69.737600   71.310
# 1972              47.450942  62.39492  57.319269  70.775033   71.910

# matplotlib operations
# Here we use dataframe plot function
# You could also use raw matplotlib plot one column each to do fine control
# Please polish the figure with more configurations
fig, ax = plt.subplots(figsize=(6, 4.5))
df.plot()
数据处理中有几个技巧,请检查代码中的注释。粗略的情节看起来像

请使用更多matplotlib操作来润色您的图形。例如:

  • 设置y标签
  • 两个大的高度,将图例设置为两列以减小它
  • 线条的颜色或线条的形状
  • 用记号笔划线
这里有一些调整

# set axis labels
ax.set_xlabel('Year')
ax.set_ylabel('Life Expection')

# set markers
markers = ['o', 's', 'd', '^', 'v']
for i, line in enumerate(ax.get_lines()):
    line.set_marker(markers[i])

# update legend
ax.legend(ax.get_lines(), df.columns, loc='best', ncol=2)

plt.tight_layout()
现在的图形如下所示:

使用
透视表
():


数据可以在GitHub上找到:非常感谢!这就成功了。当我意识到您基本上是使用unstack()来旋转数据时,我还尝试使用Pandas pivot_table(),这同样有效。
data = pd.read_csv("https://raw.githubusercontent.com/chendaniely/pandas_for_everyone/master/data/gapminder.tsv", sep="\t")
data.pivot_table(values="lifeExp", index="year", columns="continent").plot()