使用 python matplotlib 时如何避免“副标题”和“子图”之间的重叠?

Gab*_*ssi 6 python matplotlib

我正在尝试绘制一个矩阵来比较一些数据。但情节的标题与子情节重叠:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sn
   
def save_graph_cm(CMatrix):
    # CMatrix is a dict with four 3x3 pandas DataFrame
    k = 'Wine'
    id = 0
    cm = 1
    plt.suptitle("#" + str(id) + " Confusion Matrix for " + k + " dataset")
    for c_matrix in CMatrix:
        plt.subplot(2, 2, cm)
        sn.heatmap(CMatrix[c_matrix], annot=True, cmap="YlOrRd")
        plt.title("CV - " + str(cm-1))
        plt.xlabel("Predicted Classes")
        plt.ylabel("Real Classes")
        cm += 1
    plt.tight_layout()
    plt.show
Run Code Online (Sandbox Code Playgroud)

我现在得到的是:

在此输入图像描述

uke*_*emi 4

从v3.3开始,matplotlibtight_layout现在suptitle可以正确显示:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(1, 3)
for i, ax in enumerate(axs):
    ax.plot([1, 2, 3])
    ax.set_title(f'Axes {i}')

fig.suptitle('suptitle')
fig.tight_layout()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述