【问题标题】:Matplotlib different size subplotsMatplotlib 不同大小的子图
【发布时间】:2021-08-05 12:40:25
【问题描述】:

我需要在一个图中添加两个子图。一个子图需要大约是第二个子图的三倍(相同高度)。我使用GridSpeccolspan 参数完成了此操作,但我想使用figure 执行此操作,因此我可以保存为PDF。我可以使用构造函数中的figsize 参数调整第一个图形,但是如何更改第二个图形的大小?

【问题讨论】:

    标签: python plot matplotlib figure


    【解决方案1】:
    import numpy as np
    import matplotlib.pyplot as plt 
    
    # generate some data
    x = np.arange(0, 10, 0.2)
    y = np.sin(x)
    
    # plot it
    f, (a0, a1) = plt.subplots(1, 2, gridspec_kw={'width_ratios': [3, 1]})
    a0.plot(x, y)
    a1.plot(y, x)
    
    f.tight_layout()
    f.savefig('grid_figure.pdf')
    

    • 因为这个问题是规范的,所以这里是一个带有垂直子图的示例。
    # plot it
    f, (a0, a1, a2) = plt.subplots(3, 1, gridspec_kw={'height_ratios': [1, 1, 3]})
    
    a0.plot(x, y)
    a1.plot(x, y)
    a2.plot(x, y)
    
    f.tight_layout()
    

    【讨论】:

      【解决方案2】:

      简单的说,不用gridspec也可以做不同大小的子图:

      plt.figure(figsize=(12, 6))
      ax1 = plt.subplot(2,3,1)
      ax2 = plt.subplot(2,3,2)
      ax3 = plt.subplot(2,3,3)
      ax4 = plt.subplot(2,1,2)
      axes = [ax1, ax2, ax3, ax4]
      

      【讨论】:

      • plt.subplot 解释:subplot(nrows, ncols, index, **kwargs)
      • 这不仅是一个很好的答案,而且恰好是我需要的格式!让我... ctrl+c, ctrl+v
      【解决方案3】:

      您可以使用gridspecfigure

      import numpy as np
      import matplotlib.pyplot as plt 
      from matplotlib import gridspec
      
      # generate some data
      x = np.arange(0, 10, 0.2)
      y = np.sin(x)
      
      # plot it
      fig = plt.figure(figsize=(8, 6)) 
      gs = gridspec.GridSpec(1, 2, width_ratios=[3, 1]) 
      ax0 = plt.subplot(gs[0])
      ax0.plot(x, y)
      ax1 = plt.subplot(gs[1])
      ax1.plot(y, x)
      
      plt.tight_layout()
      plt.savefig('grid_figure.pdf')
      

      【讨论】:

        【解决方案4】:

        可能最简单的方法是使用subplot2grid,在Customizing Location of Subplot Using GridSpec 中进行了描述。

        ax = plt.subplot2grid((2, 2), (0, 0))
        

        等于

        import matplotlib.gridspec as gridspec
        gs = gridspec.GridSpec(2, 2)
        ax = plt.subplot(gs[0, 0])
        

        所以 bmu 的例子变成了:

        import numpy as np
        import matplotlib.pyplot as plt
        
        # generate some data
        x = np.arange(0, 10, 0.2)
        y = np.sin(x)
        
        # plot it
        fig = plt.figure(figsize=(8, 6))
        ax0 = plt.subplot2grid((1, 3), (0, 0), colspan=2)
        ax0.plot(x, y)
        ax1 = plt.subplot2grid((1, 3), (0, 2))
        ax1.plot(y, x)
        
        plt.tight_layout()
        plt.savefig('grid_figure.pdf')
        

        【讨论】:

          【解决方案5】:

          我使用pyplotaxes对象手动调整大小而不使用GridSpec

          import matplotlib.pyplot as plt
          import numpy as np
          x = np.arange(0, 10, 0.2)
          y = np.sin(x)
          
          # definitions for the axes
          left, width = 0.07, 0.65
          bottom, height = 0.1, .8
          bottom_h = left_h = left+width+0.02
          
          rect_cones = [left, bottom, width, height]
          rect_box = [left_h, bottom, 0.17, height]
          
          fig = plt.figure()
          
          cones = plt.axes(rect_cones)
          box = plt.axes(rect_box)
          
          cones.plot(x, y)
          
          box.plot(y, x)
          
          plt.show()
          

          【讨论】:

            猜你喜欢
            • 2017-03-01
            • 2015-10-18
            • 2023-04-06
            • 2015-07-15
            • 2017-07-25
            • 1970-01-01
            • 2021-11-21
            相关资源
            最近更新 更多