【问题标题】:How to make two plots side-by-side using Python?如何使用 Python 并排制作两个图?
【发布时间】:2017-08-06 16:47:29
【问题描述】:

我在 matplotlib 上找到了以下示例:

import numpy as np
import matplotlib.pyplot as plt


x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)

y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)

plt.subplot(2, 1, 1)
plt.plot(x1, y1, 'ko-')
plt.title('A tale of 2 subplots')
plt.ylabel('Damped oscillation')


plt.subplot(2, 1, 2)
plt.plot(x2, y2, 'r.-')
plt.xlabel('time (s)')
plt.ylabel('Undamped')

plt.show()

我的问题是:我需要改变什么,才能让情节并排?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    在一个方向堆叠子图时,matplotlib documentation 主张如果您只是创建几个轴,请立即解包。

    fig, (ax1, ax2) = plt.subplots(1,2, figsize=(20,8))
    sns.histplot(df['Price'], ax=ax1)
    sns.histplot(np.log(df['Price']),ax=ax2)
    plt.show()
    

    【讨论】:

      【解决方案2】:

      你可以使用-matplotlib.gridspec.GridSpec

      检查 - https://matplotlib.org/stable/api/_as_gen/matplotlib.gridspec.GridSpec.html

      以下代码在右侧显示热图,在左侧显示图像。

      #Creating 1 row and 2 columns grid
      gs = gridspec.GridSpec(1, 2) 
      fig = plt.figure(figsize=(25,3))
      
      #Using the 1st row and 1st column for plotting heatmap
      ax=plt.subplot(gs[0,0])
      ax=sns.heatmap([[1,23,5,8,5]],annot=True)
      
      #Using the 1st row and 2nd column to show the image
      ax1=plt.subplot(gs[0,1])
      ax1.grid(False)
      ax1.set_yticklabels([])
      ax1.set_xticklabels([])
      
      #The below lines are used to display the image on ax1
      image = io.imread("https://images-na.ssl-images- amazon.com/images/I/51MvhqY1qdL._SL160_.jpg")
      
      plt.imshow(image)
      plt.show()
      

      Output image

      【讨论】:

        【解决方案3】:

        查看此页面:http://matplotlib.org/examples/pylab_examples/subplots_demo.html

        plt.subplots 类似。我认为这更好,因为它更容易设置图形的参数。前两个参数定义布局(在您的情况下为 1 行 2 列),其他参数更改特征,例如图形大小:

        import numpy as np
        import matplotlib.pyplot as plt
        
        x1 = np.linspace(0.0, 5.0)
        x2 = np.linspace(0.0, 2.0)
        y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
        y2 = np.cos(2 * np.pi * x2)
        
        fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(5, 3))
        axes[0].plot(x1, y1)
        axes[1].plot(x2, y2)
        fig.tight_layout()
        

        【讨论】:

        • 有没有办法让剧情更宽更高?
        • 我不是说推荐plt.xlim/ylim
        • 我建议使用plt.subplots。您可以指定参数figsize 来定义图形的大小。如plt.subplots(1, 2, figsize=(20, 4))。否则,您可以先定义图形和图形大小:plt.figure(figsize=(20, 4))
        • 由于某种原因,当我尝试保存“plt.savefig('....png')”子图时,我得到一个空白数据。我在开头使用了完全相同的代码(我只添加了 plt.figure(figsize=(7, 3)) )。
        • 你是在plt.show之后打电话给plt.savefig吗?如果是这样,那很可能是您的问题。
        【解决方案4】:

        将子图设置更改为:

        plt.subplot(1, 2, 1)
        
        ...
        
        plt.subplot(1, 2, 2)
        

        subplot 的参数是:行数、列数以及您当前所在的子图。所以1, 2, 1 的意思是“一个 1 行 2 列的图形:转到第一个子图。”然后1, 2, 2 表示“1 行 2 列图:转到第二个子图。”

        您当前要求的是 2 行 1 列(即一个在另一个之上)布局。您需要改为要求 1 行 2 列布局。当你这样做时,结果将是:

        为了尽量减少子图的重叠,您可能需要加入:

        plt.tight_layout()
        

        演出前。产量:

        【讨论】:

          猜你喜欢
          • 2023-01-03
          • 1970-01-01
          • 2020-10-07
          • 1970-01-01
          • 2020-05-30
          • 1970-01-01
          • 2017-10-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多