【问题标题】:How can I draw an area and plot a line from columns of df?如何绘制一个区域并从 df 列绘制一条线?
【发布时间】:2021-12-14 00:03:54
【问题描述】:

我有以下 df

index Alive Past & Future Alive Past %
1/1/2020 800 1000 0.8
1/2/2020 200 400 0.5
1/3/2020 300 300 1.0
1/4/2020 400 800 0.5
1/5/2020 500 2000 0.25

这是我用来绘制面积图的代码 df.plot.area(stacked=False)

有没有办法让前两列成为面积图,第三列画成一条线?区域和线是否可能具有不同的 y 轴(左右),因为它们具有不同的大小?

【问题讨论】:

    标签: python pandas matplotlib plot


    【解决方案1】:

    试试:

    # plot area with first two columns
    ax = df.iloc[:,:2].plot.area()
    
    # line plot with 3rd column
    df.plot(y='%', secondary_y=True, ax=ax)
    

    输出:

    【讨论】:

      猜你喜欢
      • 2020-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-29
      • 2018-12-19
      • 1970-01-01
      相关资源
      最近更新 更多