小编jul*_*les的帖子

如何将字母放置在子图的角落,包括轴标签(不是轴的角落)

我想用左上角的字母(A,B,C,...)来标记子图,这些字母要么与 ylabels 对齐,要么与子图的实际角(而不是轴)对齐。

如果子图的宽度不同,我不能再使用轴变换坐标中带有偏移的文本,因为这会导致每个子图的距离不同。所以我通常使用偏移变换。但如果 ylabel 具有不同的宽度,这将不再起作用。我目前还关注每个图的偏移量,但这还不够好。

这是我到目前为止所拥有的:

import matplotlib.pyplot as plt
from matplotlib import transforms
import numpy as np

fig = plt.figure(figsize=(5, 2), constrained_layout=True)
gs = fig.add_gridspec(1, 3, width_ratios=[1, 2, 3])
axes = [fig.add_subplot(gs[i]) for i in range(3)]

ylabels = ["flat label", "bigger\nlabel", "even\nbigger\nlabel"]
labels = ["A", "B", "C"]

scaledtrans = transforms.ScaledTranslation(-0.4, 0, fig.dpi_scale_trans)

for ax, ylabel, label in zip(axes, ylabels, labels):
    ax.set_ylabel(ylabel)
    ax.text(0, 1, label, fontsize=12, fontweight="bold", va="bottom", ha="left",
           transform=ax.transAxes + scaledtrans)
Run Code Online (Sandbox Code Playgroud)

如下图所示,标签与轴脊的距离相同,但我希望它们与 ylabel 的左边缘或子图的角对齐,在这种情况下,它们与 ylabel 水平重合。但没有 transSubplot,只有 transAxes …

python text transform matplotlib subplot

5
推荐指数
1
解决办法
2690
查看次数

标签 统计

matplotlib ×1

python ×1

subplot ×1

text ×1

transform ×1