【问题标题】:matplotlib plot multiple plots using subplots like grid, in row or in columnmatplotlib 使用网格、行或列等子图绘制多个图
【发布时间】:2021-09-15 21:11:35
【问题描述】:

以下是一些必需的示例。

网格图

行图

柱状图

【问题讨论】:

    标签: python image matplotlib machine-learning subplot


    【解决方案1】:

    使用this 小包,MatplotlibDashboard 为您的目的提供易于使用的界面。请参阅链接中的文档和示例。

    pip install matplotlib-dashboard

    网格图:

    from matplotlib_dashboard import MatplotlibDashboard
    dashboard = MatplotlibDashboard([
        ['A','B','C'],
        ['D','E','F'],
        ['G','H','I'],
    ])
    
    dashboard['A'].plot(list(range(100)), color='red')
    dashboard['A'].set_title('A plot')
    
    dashboard['I'].plot(list(range(100)), color='red')
    dashboard['I'].set_title('I plot')
    
    # more plots ...
    
    plt.show()
    

    行图:

    from matplotlib_dashboard import MatplotlibDashboard
    dashboard = MatplotlibDashboard([
        ['A','B','C'],
    ])
    
    dashboard['A'].plot(list(range(100)), color='red')
    dashboard['A'].set_title('A plot')
    # more plots ...
    plt.show()
    

    柱状图:

    from matplotlib_dashboard import MatplotlibDashboard
    dashboard = MatplotlibDashboard([
        ['A'],
        ['D'],
        ['G'],
    ])
    
    dashboard['A'].plot(list(range(100)), color='red')
    dashboard['A'].set_title('A plot')
    # more plots ...
    plt.show()
    

    复杂情节:

    from matplotlib_dashboard import MatplotlibDashboard
    dashboard = MatplotlibDashboard([
        ['top' ,'top' ,'top' ,'top'  ],
        ['left','left', None ,'right'],
        ['left','left','down','right'],
    ], as3D=['left'], wspace=0.5, hspace=0.5)
    
    # drawing plots ...
    
    plt.show()
    

    【讨论】:

      【解决方案2】:
      import cv2
      import matplotlib.pyplot as plt
      plt.style.use('fivethirtyeight')
      plt.rcParams['axes.grid'] = False
          
      image_filepath="Resources/Lenna.png"
      img = cv2.imread(image_filepath)
      img_gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
      
      # blur images with different kernel size
      img_blur3 = cv2.GaussianBlur(img_gray,(3,3),0) #src, ksize, sigma
      img_blur7 = cv2.GaussianBlur(img_gray,(7,7),0)
      img_blur15 = cv2.GaussianBlur(img_gray,(15,15),0)
      

      网格图

      这里 axs[x,y] 中的 x,y 表示网格中的坐标。

      fig,axs = plt.subplots(2,2,figsize=(10,10))
      axs[0,0].imshow(img_gray,cmap='gray')
      axs[0,0].set_title("Original Image")
      axs[1,0].imshow(img_blur3,cmap='gray')
      axs[1,0].set_title("3x3")
      axs[0,1].imshow(img_blur7,cmap='gray')
      axs[0,1].set_title("7x7")
      axs[1,1].imshow(img_blur15,cmap='gray')
      axs[1,1].set_title("15x15")
      plt.show()
      

      行图

      这里axs[x]中的x代表行号或列号。

      fig,axs = plt.subplots(3,1,figsize=(5,15))
      axs[0].imshow(img_blur3,cmap='gray')
      axs[0].set_title("3x3")
      axs[1].imshow(img_blur7,cmap='gray')
      axs[1].set_title("7x7")
      axs[2].imshow(img_blur15,cmap='gray')
      axs[2].set_title("15x15")
      plt.show()
      

      柱状图

      fig,axs = plt.subplots(1,3,figsize=(15,5))
      axs[0].imshow(img_blur3,cmap='gray')
      axs[0].set_title("3x3")
      axs[1].imshow(img_blur7,cmap='gray')
      axs[1].set_title("7x7")
      axs[2].imshow(img_blur15,cmap='gray')
      axs[2].set_title("15x15")
      plt.show()
      

      【讨论】:

        【解决方案3】:

        垂直子图:

        fig, axs = plt.subplots(2)
        fig.suptitle('Vertically stacked subplots')
        axs[0].plot(x, y)
        axs[1].plot(x, -y)
        

        水平子图:

        python3
        fig, (ax1, ax2) = plt.subplots(1, 2)
        fig.suptitle('Horizontally stacked subplots')
        ax1.plot(x, y)
        ax2.plot(x, -y)
        

        网格子图:

        fig, axs = plt.subplots(2, 2)
        axs[0, 0].plot(x, y)
        axs[0, 0].set_title('Axis [0, 0]')
        axs[0, 1].plot(x, y, 'tab:orange')
        axs[0, 1].set_title('Axis [0, 1]')
        axs[1, 0].plot(x, -y, 'tab:green')
        axs[1, 0].set_title('Axis [1, 0]')
        axs[1, 1].plot(x, -y, 'tab:red')
        axs[1, 1].set_title('Axis [1, 1]')
        
        for ax in axs.flat:
            ax.set(xlabel='x-label', ylabel='y-label')
        
        # Hide x labels and tick labels for top plots and y ticks for right plots.
        for ax in axs.flat:
            ax.label_outer()
        

        子图的语法:subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw) 第一个参数 => nrows => 你想要的行数, 第二个参数 => ncols => 你想要的列数,

        Docs Docs

        【讨论】:

          猜你喜欢
          • 2019-02-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-14
          • 1970-01-01
          • 2017-05-09
          • 2019-11-28
          • 1970-01-01
          相关资源
          最近更新 更多