python绘图,控制sub子图的间距

使用 plt.subplots_adjustfig.subplots_adjust 方法来控制两个子图之间的间距: 

# 假设 best_true_target_labels, best_pred, new_target_data_input, new_target_data 是由 trainer.train() 返回的
best_true_target_labels, best_pred, new_target_data_input, new_target_data = trainer.train()

if len(best_true_target_labels) == len(best_pred):
    index_length = len(best_true_target_labels)
    print("打印预测标签和真实标签的长度index_length:", index_length)
    i = index_length - 4

    # 创建两个图,每个图包含两个子图
    fig1, axs1 = plt.subplots(2, 1, figsize=(10, 10))
    fig2, axs2 = plt.subplots(2, 1, figsize=(10, 10))
    
    while i <= index_length - 1:
        # 拿到最后一个batch的数据去画input图像
        input_data = new_target_data_input[i]  # input_data.shape:torch.Size([1, 30])
        target_data = new_target_data[i]

        # input_data对应的数据标签
        input_data_labels_plot = best_true_target_labels[i]
        best_pred_plot = best_pred[i]

        input_data_flattened = input_data.flatten()  # 调整形状为 torch.Size([30])
        target_data_flattened = target_data.flatten()  # 调整形状为 torch.Size([31])

        # 创建从 1 开始的 x 轴序列
        x_values_input = range(1, input_data_flattened.shape[0] + 1)
        x_values_target = range(1, target_data_flattened.shape[0] + 1)

        # 根据i选择当前子图和图
        if i - (index_length - 4) < 2:
            ax = axs1[i - (index_length - 4)]
            fig = fig1
        else:
            ax = axs2[i - (index_length - 4) - 2]
            fig = fig2
        
        # 绘制input_data_flattened和target_data_flattened
        ax.plot(x_values_input, input_data_flattened.numpy(), label='Input Data', linewidth=2)
        ax.plot(x_values_target, target_data_flattened.numpy(), label='Target Data', linewidth=1)
        
        # 绘制 input_data_labels 在 x 轴 31 位置的点
        ax.scatter([31], input_data_labels_plot.numpy(), color='green', label='True Label', zorder=2)
        ax.scatter([31], best_pred_plot.numpy(), color='red', label='Predicted Label', zorder=2)

        ax.set_title(f'Input Data and Forecast Results for index {i}')
        ax.set_xlabel('Time Step')
        ax.set_ylabel('Value')
        ax.legend()
        
        # 扩展 x 轴的范围,以便后续绘图
        ax.set_xlim(0, max(len(input_data_flattened), len(target_data_flattened)) + 4)
        
        i += 1
    
    fig1.tight_layout()
    fig2.tight_layout()
    
    # 调整两个图的子图之间的间距
    fig1.subplots_adjust(hspace=0.4)  # 控制 fig1 子图之间的竖直间距
    fig2.subplots_adjust(hspace=0.4)  # 控制 fig2 子图之间的竖直间距
    
    fig1.suptitle('First Two Subplots')
    fig2.suptitle('Second Two Subplots')
    plt.show()

在这个代码中:

  1. 使用 fig1.tight_layout()fig2.tight_layout() 调整每个图的子图布局,使其不重叠。
  2. 使用 fig1.subplots_adjust(hspace=0.4)fig2.subplots_adjust(hspace=0.4) 来控制每个图中的子图之间的竖直间距。hspace 参数控制子图之间的竖直间距,值越大间距越大,你可以根据需要调整这个值。

通过这种方式,你可以更好地控制两个子图之间的间距,使图表的布局更加清晰和美观。

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 你可以使用 Matplotlib 库的 `subplots_adjust` 函数来设置 subplot 子图间距。其,参数 `wspace` 和 `hspace` 分别控制子图的水平和垂直间距。例如: ``` import matplotlib.pyplot as plt fig, axs = plt.subplots(2, 2) fig.subplots_adjust(wspace=0.4, hspace=0.4) plt.show() ``` ### 回答2: Python subplot 是 Matplotlib 库的一个函数,用于在同一图像上创建多个子图。使用 subplot 时,我们可以指定子图的行数、列数和子图的位置。同时,我们还可以调整 subplot 之间间距subplot 之间间距由下面几个参数控制: 1. wspace:指定子图之间的宽度间距; 2. hspace:指定子图之间的高度间距; 3. left、right、bottom、top 等参数:指定 subplot 区域相对于整个图像区域的位置。在调整 subplot 之间间距时,通常只需要调整 wspace 和 hspace 参数即可。 下面是一个例子: ```python import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2 * np.pi, 100) y = np.sin(x) fig, axs = plt.subplots(2, 2) fig.subplots_adjust(wspace=0.5, hspace=0.5) axs[0, 0].plot(x, y) axs[0, 0].set_title('subplot 1') axs[0, 1].plot(x, y) axs[0, 1].set_title('subplot 2') axs[1, 0].plot(x, y) axs[1, 0].set_title('subplot 3') axs[1, 1].plot(x, y) axs[1, 1].set_title('subplot 4') plt.show() ``` 执行这段代码后,我们会看到输出了一个包含 4 个子图的图像。在这个例子,我们使用 subplots 函数创建了一个 2 行 2 列的图像,然后使用subplot 位置索引从左向右从上到下的顺序为 1-4 命名了 4 个 subplot。接着,我们使用 subplots_adjust 配置函数调整子图之间间距。 当然,除了 wspace 和 hspace 参数,我们还可以使用其他参数来进一步调整 subplot 之间间距和位置。总体来说,通过 subplot 我们可以在单个屏幕上同时将多个图形绘制出来,这对于比较多的图形数据比较方便。 ### 回答3: subplot是在一个大的绘图区域创建多个子图的函数。子图之间间距通常涉及到3个参数:左侧、底部和右侧(分别是left、bottom和right)。这些参数定义了每个子图相对于大绘图区域的位置。如果这些参数设置得不好,那么子图之间间距可能会非常小,从而使图像的可读性降低。 有几种方法可以调整subplot之间间距。以下是一些可能有用的方法: 1. 使用plt.subplots_adjust()函数。使用这个函数,可以通过调整left、bottom、right和top参数来改变子图之间间距。这个函数非常灵活,可以方便地控制子图之间距离。 2. 使用plt.tight_layout()函数。这个函数可以自动调整子图之间间距,以便它们适合于当前图像的大小。使用这个函数时,可以传递pad参数以增加子图之间间距。 3. 使用gridspec。这个库允许您比subplot更细粒度地控制图像子图。在使用gridspec时,可以在大的绘图区域创建一个网格,并将每个子图放置在网格的一个单元格。 4. 使用subplots()函数的hspace和wspace参数。这些参数定义了每个子图之间的水平和垂直间距。 无论你选择使用哪种方法,都应该始终记住,子图之间间距应该足以使图像易于解读。如果子图之间间距太小,那么图像可能会变得混乱、难以理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Top Secret

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值