python - 使用python matplotlib时如何避免 "suptitle"和 "subplots"之间的重叠?

标签 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

我现在得到的是:

enter image description here

最佳答案

截至v3.3 ,matplotlib 的 tight_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()

enter image description here

关于python - 使用python matplotlib时如何避免 "suptitle"和 "subplots"之间的重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57013731/

相关文章:

python - 除了词干还有什么其他选择?

python - 当需要高精度时,Matplotlib 无法正确绘图

python - 在 matplotlib 图中自定义 xticks

python - 使用 Matplotlib 绘制二维数组

python - Numpy 直方图数据 : Why is the length of bins vector longer than the histogram values vector?

python - 计算 python/pandas 中组的平均值

python - 如何打印已经返回的函数调用的堆栈跟踪?

python - 从 Python 在控制台中运行 WinSCP 命令

python - QListWidgetItem 保存列表之间的数据

python - 使用 PyPlot 更改直方图的轴