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

matplotlib数据显示在两个单独的绘图上

  •  0
  • bbartling  · 技术社区  · 4 年前

    我有一个与此代码相结合的线条和条形图。(剧情看起来不错)

    fig, ax1 = plt.subplots(figsize=(25, 10))
    
    
    ax2 = ax1.twinx()
    ax1.bar(daily_summary.index, daily_summary['hour_max_demand'],width=20, alpha=0.2, color='orange')
    ax1.grid(b=False) # turn off grid #2
    
    ax2.plot(daily_summary.kW)
    ax2.set_title('Max Demand per Day and Max Demand Hour of Day')
    ax2.set_ylabel('Electric Demand kW')
    ax1.set_ylabel('Hour of Day')
    
    plt.savefig('./plots/Max_Demand_and_Max_Hour_of_Day.png')
    

    enter image description here

    plot2 = daily_summary.kWH.rolling(7, center=True).mean()
    plt.title(' 7 Day Rolling Average - kWH / Day')
    
    plot2.plot(figsize=(25,10))
    plt.savefig('./plots/kWhRollingAvg.png')
    

    黄色条形图数据不应仅位于底部的一行上: enter image description here

    0 回复  |  直到 4 年前
        1
  •  1
  •   Guimoute    4 年前

    一般来说,最好使用面向对象的方法,因为 plt.something() 当你添加坐标轴和数字时变得越来越难以预测。

    df.plot(...) 默认为不一定是所需的ax。

    # First figure.
    fig1, ax1 = plt.subplots(figsize=(25, 10))    
    ax2 = ax1.twinx()
    
    ax1.bar(daily_summary.index, daily_summary['hour_max_demand'], width=20, alpha=0.2, color='orange')
    ax1.grid(b=False) # turn off grid #2
    
    ax2.plot(daily_summary.kW)
    ax2.set_title('Max Demand per Day and Max Demand Hour of Day')
    ax2.set_ylabel('Electric Demand kW')
    ax1.set_ylabel('Hour of Day')
    
    fig1.savefig('./plots/Max_Demand_and_Max_Hour_of_Day.png')
    
    # Figure 2.
    fig2, ax3 = plt.subplots(figsize=(25, 10))
    ax3.title(' 7 Day Rolling Average - kWh / Day')
    data3 = daily_summary.kWH.rolling(7, center=True).mean()
    ax3.plot(data3)
    fig2.savefig('./plots/kWhRollingAvg.png')