【问题标题】:matplotlib multiple Y-axis pandas plotmatplotlib 多 Y 轴熊猫图
【发布时间】:2021-11-09 20:38:18
【问题描述】:

有人可以告诉我如何绘制多个 Y 轴图吗?

这是下面一些虚构的数据,我怎么能把Temperature放在自己的Y轴上,Pressure放在自己的Y轴上,然后把Value1Value2放在自己的Y轴上相同 Y 轴。我正在尝试使用相同的外观和感觉of this SO post answer。感谢您提供任何提示,我不明白 ax3 = ax.twinx() 过程,就像我需要为我需要的每个单独的 Y 轴图定义一个 ax.twinx() 吗?

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt



rows,cols = 8760,4
data = np.random.rand(rows,cols) 
tidx = pd.date_range('2019-01-01', periods=rows, freq='H') 
df = pd.DataFrame(data, columns=['Temperature','Value1','Pressure','Value2'], index=tidx)


# using subplots() function
fig, ax = plt.subplots(figsize=(25,8))
plt.title('Multy Y Plot')
 
ax2 = ax.twinx()    
ax3 = ax.twinx()    
ax4 = ax.twinx()

plot1, = ax.plot(df.index, df.Temperature)
plot2, = ax2.plot(df.index, df.Value1, color = 'r')
plot3, = ax3.plot(df.index, df.Pressure, color = 'g')
plot4, = ax4.plot(df.index, df.Value2, color = 'b')

ax.set_xlabel('Date')
ax.set_ylabel('Temperature')
ax2.set_ylabel('Value1')
ax3.set_ylabel('Pressure')
ax4.set_ylabel('Value2')


plt.legend([plot1,plot2,plot3,plot4],list(df.columns))

# defining display layout
plt.tight_layout()

# show plot
plt.show()

这将输出在同一侧混乱的所有内容,没有单独的 Y 轴用于压力、Value1 和 Value2。

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    您将 4 个不同的地块合二为一,这没有帮助。我建议将其分成 2 个带有共享 x 轴“日期”的图:

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    rows,cols = 8760,4
    data = np.random.rand(rows,cols) 
    tidx = pd.date_range('2019-01-01', periods=rows, freq='H') 
    df = pd.DataFrame(data, columns=['Temperature','Value1','Pressure','Value2'], index=tidx)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(25,8))
    plt.title('Multy Y Plot')
     
    ax1b = ax1.twinx()    
    plot1a, = ax1.plot(df.index, df.Temperature)
    plot1b, = ax1b.plot(df.index, df.Pressure, color='r')
    
    ax1.set_ylabel('Temperature')
    ax1b.set_ylabel('Pressure')
    
    ax2b = ax2.twinx() 
    plot2a, = ax2.plot(df.index, df.Value1, color='k')
    plot2b, = ax2b.plot(df.index, df.Value2, color='g')
    
    ax2.set_xlabel('Date')
    ax2.set_ylabel('Value1')
    ax2b.set_ylabel('Value2')
    
    
    plt.legend([plot1a, plot1b, plot2a, plot2b], df.columns)
    
    # defining display layout
    plt.tight_layout()
    
    # show plot
    plt.show()
    

    在这里,我在第一个图(顶部)温度和压力以及第二个图(底部)值 1 和值 2 中添加了值。通常,我们在同一个图中添加了比较有意义的东西相同的 x 轴。压力和温度是一个有效的组合,这就是我将这两者结合在一起的原因。但你可以随心所欲。

    【讨论】:

    • 你能帮我更进一步吗?在value 1 value 2 的底部图上是否可以在同一 Y 轴上获得它们? (不拆分)在我的现实世界场景中,这些具有相同的单位
    • 其实我想我想通了。我也会发布如何执行此操作的答案。让我知道是否有更好的方法!再次感谢您的帮助!!!
    • plt.tight_layout() 是做什么的?
    • @HenryHub 调整子图之间和周围的填充。
    【解决方案2】:

    下面这个答案使用mpatches是如何在同一轴上制作Value1Value2的子图。这篇文章的解决方案在不同的轴上有Value1Value2 的子图。感谢@tzinie 的帮助!

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.patches as mpatches
    
    
    rows,cols = 8760,4
    data = np.random.rand(rows,cols) 
    tidx = pd.date_range('2019-01-01', periods=rows, freq='H') 
    df = pd.DataFrame(data, columns=['Temperature','Value1','Pressure','Value2'], index=tidx)
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(25,8))
    plt.title('Multy Y Plot')
     
    ax1b = ax1.twinx()    
    plot1a, = ax1.plot(df.index, df.Temperature, color='r') # red
    plot1b, = ax1b.plot(df.index, df.Pressure, color='b') # blue
    
    ax1.set_ylabel('Temperature')
    ax1b.set_ylabel('Pressure')
    
    
    ax2.plot(df.index, df.Value1, color='k') # black
    ax2.plot(df.index, df.Value2, color='g') # green
    ax2.set_xlabel('Date')
    ax2.set_ylabel('Value1 & Value2')
    
    
    red_patch = mpatches.Patch(color='red', label='Temperature')
    blue_patch = mpatches.Patch(color='blue', label='Pressure')
    green_patch = mpatches.Patch(color='green', label='Value2')
    black_patch = mpatches.Patch(color='black', label='Value1')
    plt.legend(handles=[red_patch,blue_patch,green_patch,black_patch])
    
    # defining display layout
    #plt.tight_layout()
    
    # show plot
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2015-05-04
      • 1970-01-01
      • 2021-01-14
      • 2022-06-14
      • 1970-01-01
      • 1970-01-01
      • 2020-02-11
      • 2018-09-16
      • 2021-07-11
      相关资源
      最近更新 更多