【问题标题】:How do I put two plots next to each other when having plots with different scales (twinaxes)?当有不同比例(双轴)的地块时,如何将两个地块并排放置?
【发布时间】:2021-03-12 11:22:02
【问题描述】:

我有两个图,但绘制在同一个 x 轴上。我想将两个图彼此相邻(并排)绘制,而不是垂直绘制。 我该怎么做?

从 matplotlib 文档中借用的示例数据。我试过了,我将第一个图放到 plt.subplots 中,但第二个图仍然绘制在下面而不是在第一个图旁边:

import numpy as np
import matplotlib.pyplot as plt

# Create some mock data
t = np.arange(0.01, 10.0, 0.01)
data1 = np.exp(t)
data2 = np.sin(2 * np.pi * t)


## initiating the plots next to each other 

fig,(ax1,ax2) = plt.subplots(1,2)

color = 'tab:red'
ax1.set_xlabel('time (s)')
ax1.set_ylabel('exp', color=color)
ax1.plot(t, data1, color=color)
ax1.tick_params(axis='y', labelcolor=color)

ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis

color = 'tab:blue'
ax2.set_ylabel('sin', color=color)  # we already handled the x-label with ax1
ax2.plot(t, data2, color=color)
ax2.tick_params(axis='y', labelcolor=color)
plt.xlim(0,4)

fig.tight_layout()  # otherwise the right y-label is slightly clipped
plt.show()


fig, ax1 = plt.subplots()

color = 'tab:red'
ax1.set_xlabel('time (s)')
ax1.set_ylabel('exp', color=color)
ax1.plot(t, data1, color=color)
ax1.tick_params(axis='y', labelcolor=color)

ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis

color = 'tab:blue'
ax2.set_ylabel('sin', color=color)  # we already handled the x-label with ax1
ax2.plot(t, data2, color=color)
ax2.tick_params(axis='y', labelcolor=color)
plt.xlim(4,6)

fig.tight_layout()  # otherwise the right y-label is slightly clipped
plt.show()

【问题讨论】:

    标签: python numpy matplotlib jupyter


    【解决方案1】:

    这里实际上需要 4 个地块。所以我用 ax1 和 ax2 作为左边的图,用 ax3 和 ax4 作为右边的图。我不确定这是不是最好的方法,但我认为它可以解决您的问题。

    import numpy as np
    import matplotlib.pyplot as plt
    
    # Create some mock data
    t = np.arange(0.01, 10.0, 0.01)
    data1 = np.exp(t)
    data2 = np.sin(2 * np.pi * t)
    
    
    ## initiating the plots next to each other 
    
    fig,(ax1,ax3) = plt.subplots(1,2)
    
    color = 'tab:red'
    ax1.set_xlabel('time (s)')
    ax1.set_ylabel('exp', color=color)
    ax1.plot(t, data1, color=color)
    ax1.tick_params(axis='y', labelcolor=color)
    
    ax2=ax1.twinx()
    
    color = 'tab:blue'
    ax2.set_ylabel('sin', color=color)  # we already handled the x-label with ax1
    ax2.plot(t, data2, color=color)
    ax2.tick_params(axis='y', labelcolor=color)
    ax2.set_xlim([0,4])
    
    
    
    color = 'tab:red'
    ax3.set_xlabel('time (s)')
    ax3.set_ylabel('exp', color=color)
    ax3.plot(t, data1, color=color)
    ax3.tick_params(axis='y', labelcolor=color)
    
    ax4=ax3.twinx()
    
    color = 'tab:blue'
    ax4.set_ylabel('sin', color=color)  # we already handled the x-label with ax1
    ax4.plot(t, data2, color=color)
    ax4.tick_params(axis='y', labelcolor=color)
    ax4.set_xlim(4,6)
    
    
    
    fig.tight_layout()  # otherwise the right y-label is slightly clipped
    plt.show()
    

    Plots

    【讨论】:

      【解决方案2】:

      在这种情况下,最简单的方法是使用 Gridspec 进行布局。图中的代码直接改编自您的代码。另一方面,我只创建了结构。这种结构将来可以扩展。

      import matplotlib.pyplot as plt
      from matplotlib import gridspec
      import numpy as np
      
      # Create some mock data
      t = np.arange(0.01, 10.0, 0.01)
      data1 = np.exp(t)
      data2 = np.sin(2 * np.pi * t)
      
      fig = plt.figure(figsize=(10, 8))
      gs = gridspec.GridSpec(nrows=1, ncols=2, width_ratios=[1,1], wspace=0.5)
      ax1 = fig.add_subplot(gs[0])
      color = 'tab:red'
      ax1.set_xlabel('time (s)')
      ax1.set_ylabel('exp', color=color)
      ax1.plot(t, data1, color=color)
      ax1.tick_params(axis='y', labelcolor=color)
      
      ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis
      
      color = 'tab:blue'
      ax2.set_ylabel('sin', color=color)  # we already handled the x-label with ax1
      ax2.plot(t, data2, color=color)
      ax2.tick_params(axis='y', labelcolor=color)
      plt.xlim(0,4)
      
      ax3 = fig.add_subplot(gs[1])
      color = 'tab:red'
      ax3.set_xlabel('time (s)')
      ax3.set_ylabel('exp', color=color)
      ax3.plot(t, data1, color=color)
      ax3.tick_params(axis='y', labelcolor=color)
      
      ax4 = ax3.twinx()  # instantiate a second axes that shares the same x-axis
      
      color = 'tab:blue'
      ax4.set_ylabel('sin', color=color)  # we already handled the x-label with ax1
      ax4.plot(t, data2, color=color)
      ax4.tick_params(axis='y', labelcolor=color)
      plt.xlim(4,6)
      
      plt.show()
      

      【讨论】:

      【解决方案3】:

      我想指出这一点,

      • 执行plt.show() 后通常不会进行绘图。

      您可以先创建所有axes,然后再使用它们。参考下面的例子。

      import numpy as np
      import matplotlib.pyplot as plt
      
      # Create some mock data
      t = np.arange(0.01, 10.0, 0.01)
      data1 = np.exp(t)
      data2 = np.sin(2 * np.pi * t)
      
      f = plt.figure(figsize=(10,3))
      
      # create all axes we need
      ax1 = plt.subplot(121)
      ax2 = ax1.twinx()
      ax3 = plt.subplot(122)
      ax4 = ax3.twinx()
      
      # share the secondary axes
      ax1.get_shared_y_axes().join(ax1, ax3)
      
      color = 'tab:red'
      ax1.set_xlabel('time (s)')
      ax1.set_ylabel('exp', color=color)
      ax1.plot(t, data1, color=color)
      ax1.tick_params(axis='y', labelcolor=color)
      ax1.grid()
      
      color = 'tab:blue'
      ax2.set_ylabel('sin', color=color)  # we already handled the x-label with ax1
      ax2.plot(t, data2, color=color)
      ax2.tick_params(axis='y', labelcolor=color)
      plt.xlim(0,4)
      
      color = 'tab:red'
      ax3.set_xlabel('time (s)')
      ax3.set_ylabel('exp', color=color)
      ax3.plot(t, data1, color=color)
      ax3.tick_params(axis='y', labelcolor=color)
      ax3.grid()
      
      color = 'tab:blue'
      ax4.set_ylabel('sin', color=color)  # we already handled the x-label with ax1
      ax4.plot(t, data2, color=color)
      ax4.tick_params(axis='y', labelcolor=color)
      plt.xlim(4,6)
      
      plt.tight_layout()  # otherwise the right y-label is slightly clipped
      plt.show()
      

      输出图像:

      【讨论】:

        猜你喜欢
        • 2020-09-17
        • 2012-09-16
        • 1970-01-01
        • 2023-03-26
        • 1970-01-01
        • 1970-01-01
        • 2021-03-27
        • 2021-11-15
        • 2020-05-18
        相关资源
        最近更新 更多