代码之家  ›  专栏  ›  技术社区  ›  edson1991

Matplotlib-变量在错误的子批次中打印[重复]

  •  1
  • edson1991  · 技术社区  · 7 年前

    我有两个变量,我想用errorbars绘制在单独的子图中。然而,它们都在底部的子图中绘制。如何让它们在单独的子地块中绘图?

    from pandas import DataFrame, date_range, Timedelta
    import numpy as np
    from matplotlib import pyplot as plt
    
    rng = date_range(start='2015-01-01', periods=5, freq='24H')
    df = DataFrame({'y':np.random.normal(size=len(rng))}, index=rng)
    y1 = df['y']
    y2 = (y1*3)
    sd1 = (y1*2)
    sd2 = (y1*2)
    
    fig,(ax1,ax2) = plt.subplots(2,1,sharex=True)
    
    ax1 = y1.plot(yerr=sd1)
    ax2 = y2.plot(yerr=sd2)
    

    Example Photo

    1 回复  |  直到 7 年前
        1
  •  1
  •   Scott Boston    7 年前

    使用 ax 熊猫数据框中的参数 plot :

    from pandas import DataFrame, date_range, Timedelta
    import numpy as np
    from matplotlib import pyplot as plt
    
    rng = date_range(start='2015-01-01', periods=5, freq='24H')
    df = DataFrame({'y':np.random.normal(size=len(rng))}, index=rng)
    y1 = df['y']
    y2 = (y1*3)
    sd1 = (y1*2)
    sd2 = (y1*2)
    
    fig,(ax1,ax2) = plt.subplots(2,1,sharex=True)
    
    _ = y1.plot(yerr=sd1, ax=ax1)
    _ = y2.plot(yerr=sd2, ax=ax2)
    

    输出:

    enter image description here