【问题标题】:Python pandas plot more columns but shows only one legendPython pandas 绘制更多列,但仅显示一个图例
【发布时间】:2016-08-02 12:56:36
【问题描述】:

我想在同一个 x 轴和 y 轴上绘制两列。但是pandas-plot 只显示第二列的图例(所以第一列没有点)。当然,两个标签(列名)都显示在图例框中。

我的数据框是:

df = pd.DataFrame({'datetime':[dt.datetime(2016,1,1,0,0,0), dt.datetime(2016,1,4,0,0,0),
    dt.datetime(2016,1,9,0,0,0)], 'value':[10,7,8], 'value2':[12,4,9]})

而我的情节是:

ax = df.plot(x='datetime', y='value', marker='o', linewidth=0)
df.plot(ax=ax, x='datetime', y='value2', marker='o', linewidth=0)

如果我绘制线条以及显示第一列的“图例”,但它只是一条没有点的蓝线:

ax = df.plot(x='datetime', y='value', marker='o')
df.plot(ax=ax, x='datetime', y='value2', marker='o')

是否可以仅在图例框中(和图上)显示两列的点?

谢谢!

【问题讨论】:

    标签: python pandas plot legend


    【解决方案1】:

    你应该在调用第二个情节后尝试回忆传说:

    ax.legend()
    

    这是我得到的:

    【讨论】:

      猜你喜欢
      • 2018-01-28
      • 2014-03-26
      • 1970-01-01
      • 1970-01-01
      • 2015-12-31
      • 2018-05-13
      • 2016-11-07
      • 1970-01-01
      • 2018-06-07
      相关资源
      最近更新 更多