python · matplotlib | 如何绘制子图

发布时间 2023-05-26 10:56:52作者: MoonOut

代码:
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rc("font",family='MicroSoft YaHei',weight="bold")
 
fig, axs = plt.subplots(2, 2, figsize=(15,12))
colors = ['blue', 'orange', 'green', 'red']
for i, value in enumerate(item_indexes.values()):
    temp_p, temp_t = pred[:, value].reshape(-1), test_y[:, value].reshape(-1)
    # 注释: temp_p temp_t 都是 torch.tensor
    axs[i//2, i%2].scatter(temp_p, temp_t, c=colors[i], s=20)
    axs[i//2, i%2].plot([min(temp_t), max(temp_t)], [min(temp_t), max(temp_t)], c='black')
axs[0, 0].set_title('空调送风温度', fontsize=14)
axs[0, 1].set_title('空调回风温度', fontsize=14)
axs[1, 0].set_title('IT 设备进风口温度', fontsize=14)
axs[1, 1].set_title('IT 设备出风口温度', fontsize=14)

for ax in axs.flat:
    ax.set(xlabel='真实值', ylabel='预测值')

# Hide x labels and tick labels for top plots and y ticks for right plots.
for ax in axs.flat:
    ax.label_outer()

效果: