【问题标题】:matplotlib how to correctly plot text in subplot [duplicate]matplotlib如何在子图中正确绘制文本[重复]
【发布时间】:2018-01-04 05:34:29
【问题描述】:

这张图片是使用:

subplot(1,2,1)
hist(...)
subplot(1,2,2)
text(...)

但我不想要第二个情节的边框,我想要情节左上角的文字。我怎样才能做到?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    试试这个:

    subplot(1,2,1)
    hist(...)
    ax2 = subplot(1,2,2)
    ax2.set_xticks([])
    ax2.set_yticks([])
    for spine in ax2.spines.values():
        spine.set_visible(False)
    ax2.invert_yaxis()
    ax2.text(..., verticalalignment="top")
    

    更新:

    正如 cmets 中指出的,您也可以这样做:

    subplot(1,2,1)
    hist(...)
    ax2 = subplot(1,2,2)
    ax2.axis("off")
    ax2.invert_yaxis()
    ax2.text(..., verticalalignment="top")
    

    虽然这也会删除默认轴背景(取决于您的设置和 Matplotlib 版本,这可能会给您留下灰色背景)。

    【讨论】:

    • 谢谢,这太完美了。
    • 为此,您可以在第二个图上使用plt.axis('off')
    • 是的,我检查了 Axes API,并将其替换为 ax2.set_axis_off()
    • 你为什么invert_yaxis()
    • @tom 因此,根据 OP 的要求,文本将出现在绘图的上部(似乎原点或靠近它的点被用作文本的坐标) .
    猜你喜欢
    • 2021-10-30
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 2023-02-01
    • 1970-01-01
    • 2019-05-31
    • 1970-01-01
    • 2020-01-18
    相关资源
    最近更新 更多