按数据框分组的两个不同的折线图

时间:2019-04-09 01:25:24

标签: pandas matplotlib jupyter-notebook

我将数据分为2个单独的数据帧,并希望将它们与2条单独的线一起绘制在一个图中。

我已经根据需要对数据进行了分组,并根据分组后的数据绘制了单独的图形。

grouped_men = df_men.groupby('age').mean()[['oldpeak']]
grouped_women = df_women.groupby('age').mean()[['oldpeak']]
grouped_men.plot(kind='line',title='Mens age vs oldpeak')
grouped_women.plot(kind='line',title='Womens age vs oldpeak')

但是现在,我需要绘制一个由两行男女组成的图形来代替2个单独的图形。

当前图看起来像这样: plots

1 个答案:

答案 0 :(得分:1)

您需要指定熊猫在其上放置绘图的轴。请尝试以下操作:

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
grouped_men.plot(kind='line', ax=ax, label='Mens age vs oldpeak')
grouped_women.plot(kind='line', ax=ax, label='Womens age vs oldpeak')
plt.gca().legend(title="Legend title")  # Changes
plt.show()