【问题标题】:Secondary y axis limit in pandas plot熊猫图中的次要 y 轴限制
【发布时间】:2019-04-09 17:01:36
【问题描述】:

有没有办法在 pandas df.plot 中设置次 Y 轴的限制

我有以下绘图语句。有没有办法简单地为辅助轴添加 ylim ?如“secondary_ylim=(0,1)”

df[["Date","Col1","Col2"]].plot(x="date",y=["Col1","Col2"],secondary_y="Col2",ylim = (0,1))

【问题讨论】:

    标签: pandas plot


    【解决方案1】:

    有趣....我不知道是否还有其他方法可以获取辅助 y_axes 的轴。

    但是,你可以这样做:

    df = pd.DataFrame({'Date':pd.date_range('2019-02-01', periods=10), 'Col1':np.random.randint(0,10,10), 'Col2':np.random.randint(100,500, 10)})
    ax = df[["Date","Col1","Col2"]].plot(x="Date",y=["Col1","Col2"],secondary_y="Col2", ylim = ([0,5]))
    ax.set_ylim(0,5)
    fig = ax.get_figure()
    ax = fig.get_axes()
    ax[1].set_ylim(0,250)
    

    或者正如@Stef 指出的那样,您可以使用right_ax

    ax = df[["Date","Col1","Col2"]].plot(x="Date",y=["Col1","Col2"],secondary_y="Col2", ylim = ([0,5]))
    ax.right_ax.set_ylim(0,250)
    

    输出:

    【讨论】:

    • @ImportanceOfBeingErnest 你有更好的办法吗?
    • 或简单的ax.right_ax.set_ylim(0,250)(使用plot返回的ax
    猜你喜欢
    • 2015-05-04
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多