【问题标题】:Pandas DataFrame step plot: where="post"Pandas DataFrame 阶梯图:where="post"
【发布时间】:2016-05-06 04:07:38
【问题描述】:

我想知道如何将 matplotlibs where="post" 传递给熊猫图。

import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.randn(36, 3))
df.plot(drawstyle="steps", linewidth=2)

# this doesn't work
df.plot(drawstyle="steps", where='post')

有谁知道如何实现这一点?

提前致谢!

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    你只需要指定drawstyle="steps-post":

    df = pd.DataFrame(np.random.randn(36, 3))
    df.plot(drawstyle="steps", linewidth=2)
    df.plot(drawstyle="steps-post", linewidth=2)
    

    比较结果:

    【讨论】:

    • 完美。谢谢!!顺便说一句:这是否记录在某处?在熊猫文档中找不到它..
    • 好吧,我承认我一直在猜测我的方式......但是这些关键词最终都传递给了line对象,并且文档确实提到了它:matplotlib.org/api/lines_api.html
    • 哦,我错过了。谢谢!
    • 我参加聚会有点晚了,但是:有没有办法在顶部(底部)图中的第一个(最后)点添加一条水平线?即,当这些是时间间隔时,显示的数量在每个时间间隔内都是恒定的。
    • 回应@ElRudi 提出的问题——我不知道(在配置情节时)。在这种情况下,我通常会创建一个正在绘制的数据的副本,并根据需要添加前导/尾随观察以模拟效果(保持原始数据不变。)
    【解决方案2】:

    为什么不直接使用 matplotlib 绘图? Click here for an example.

    from matplotlib import pyplot as plt
    plt.step(range(len(df.index)),df[0],where='post')
    plt.step(range(len(df.index)),df[1],where='post')
    plt.step(range(len(df.index)),df[2],where='post')
    

    【讨论】:

    • 谢谢!我更喜欢 pandas 绘图来进行基本绘图,因为它需要更少的代码。
    猜你喜欢
    • 1970-01-01
    • 2021-10-11
    • 2014-12-04
    • 1970-01-01
    • 2020-06-14
    • 2019-01-30
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多