【问题标题】:python bar graph and line graph in same chart with pandas & matplotlibpython条形图和折线图与熊猫和matplotlib在同一图表中
【发布时间】:2015-10-20 14:53:27
【问题描述】:

在 pandas 数据框中,我有每日库存量 [bar] 和同一条 [line] 的 65 天移动平均线。我希望它们相互叠加,但不知道该怎么做。谁能给我看看?

谢谢

【问题讨论】:

    标签: python-3.x pandas matplotlib


    【解决方案1】:

    这是一种方法

    随机数据

    rng = pd.date_range('1/1/2011', periods=100, freq='D')
    ts = pd.Series(np.random.randn(len(rng)), index=rng).cumsum()
    

    带有 10 天移动平均线的条形图。

    fig = plt.figure()
    ax = ts.plot(kind="bar")   # barchart
    ax2 = ax.twinx()
    ax2.plot(ax.get_xticks(), pd.rolling_mean(ts, 10)) #linechart
    

    【讨论】:

    • 谢谢。但是如果是竖线,移动平均线的峰值怎么可能超过峰值呢?
    • 此代码无效。 ax2.plot(ax.get_xticks(), ts.rolling(10).mean()) 工作。
    猜你喜欢
    • 1970-01-01
    • 2016-12-13
    • 2020-05-06
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多