简体   繁体   English

下图文字为matplotlib

[英]Text below a figure with matplotlib

I managed to add text on the figure with我设法在图中添加了文字

        fg_ax.text(
            0.05,
            0.1,
            f"n = {len(df)}",
        )

fg_ax being my matplotlib.axes object fg_ax是我的matplotlib.axes object

But I want it below the figure.但我想要它低于这个数字。 Because my y coordinates go from 0 to 1, I figured after reading the documentation that doing something like this would put it below:因为我的 y 坐标 go 从 0 到 1,所以我在阅读文档后认为做这样的事情会把它放在下面:

        fg_ax.text(
            0.05,
            -0.5,
            f"n = {len(df)}",
        )

But there is nothing that appears anymore, as if I was writing "outside" what is displayed.但是什么都没有出现,就好像我在写“外面”显示的东西一样。

I tried plt.show() and fg_ax.figure.savefig .我尝试plt.show()fg_ax.figure.savefig None works.没有工作。

Minimal reproducible example:最小可重现示例:

import numpy as np
import matplotlib.pyplot as plt


N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = (30 * np.random.rand(N)) ** 2  # 0 to 15 point radii

_, fg_ax = plt.subplots()
fg_ax.scatter(x, y, s=area, c=colors, alpha=0.5)
fg_ax.text(
    0.05,
    -0.5,
    f"n = qsdfqsdf",
)
plt.show()

Based on this answer , I've changed your code to the following:基于此答案,我已将您的代码更改为以下内容:

N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = (30 * np.random.rand(N)) ** 2  # 0 to 15 point radii

fig, fg_ax = plt.subplots()
fg_ax.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.figtext(
    0.2, 0.01, f"n = qsdfqsdf", wrap=True, horizontalalignment='center', fontsize=10
)
fig.subplots_adjust(bottom=0.3)
plt.savefig("image.pdf", format="pdf", bbox_inches="tight")
plt.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM