【问题标题】:How to avoid the overlapping between "suptitle" and "subplots" when using python matplotlib?使用python matplotlib时如何避免“suptitle”和“subplots”之间的重叠?
【发布时间】:2019-11-22 14:53:56
【问题描述】:

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

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

我现在得到的是:

Overlapping titles

【问题讨论】:

标签: python matplotlib


【解决方案1】:

截至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()

【讨论】:

    【解决方案2】:

    我遇到了类似的问题,使用 GridSpec 作为这个答案https://stackoverflow.com/a/19627237/8079057 为我修复了它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      相关资源
      最近更新 更多