熊猫绘制多个类别线

Cra*_*aig 3 matplotlib pandas

假设我有以下数据...

date      Score  category                 
2017-01-01   50.0         1
2017-01-01  590.0         2
2017-01-02   30.0         1
2017-01-02  210.4         2
2017-01-03   11.0         1
2017-01-03   50.3         2
Run Code Online (Sandbox Code Playgroud)

所以每天,我有多个类别,每个类别都有一个分数。到目前为止,这是我的代码...

vals = [{'date': '2017-01-01', 'category': 1, 'Score': 50},
         {'date': '2017-01-01', 'category': 2, 'Score': 590},
         {'date': '2017-01-02', 'category': 1, 'Score': 30},
         {'date': '2017-01-02', 'category': 2, 'Score': 210.4},
         {'date': '2017-01-03', 'category': 1, 'Score': 11},
         {'date': '2017-01-03', 'category': 2, 'Score': 50.3}]
df = pd.DataFrame(vals)
df.date = pd.to_datetime(df['date'], format='%Y-%m-%d')
df.set_index(['date'],inplace=True)
Run Code Online (Sandbox Code Playgroud)

这导致了一个奇怪的情节如下。 在此处输入图片说明

我想要多行,每个类别一行,以及 X 轴上的日期 - 我该怎么做?

Vai*_*ali 5

您可以使用 groupby 和 plot

fig, ax = plt.subplots()
for label, grp in df.groupby('category'):
    grp.plot(x = grp.index, y = 'Score',ax = ax, label = label)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明