【发布时间】:2020-05-04 21:49:33
【问题描述】:
我希望能够在 matplotlib 中添加类似于以下内容的脚注文本:
以下代码将创建具有相似文本的绘图
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
fig, ax = plt.subplots(figsize = (5, 8))
n = 10
np.random.seed(1)
_ = ax.scatter(np.random.randint(0, 10, n), np.random.randint(0, 10, n), s=500)
x = 0
y = 1
_ = ax.text(
x, y, "hello this is some text at the bottom of the plot", fontsize=15, color="#555"
)
看起来像:
但是,如果数据发生变化,则上述不会调整,例如:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
fig, ax = plt.subplots(figsize=(5, 8))
n = 10
np.random.seed(2)
_ = ax.scatter(np.random.randint(0, 10, n), np.random.randint(0, 10, n), s=500)
x = 0
y = 1
_ = ax.text(
x, y, "hello this is some text at the bottom of the plot", fontsize=15, color="#555"
)
我有seen this question/answer,这只是说明如何在特定的x,y 坐标处绘制文本。我特别希望能够设置一个脚注,而不是在特定的 x,y 处绘制,所以解决方案应该是动态的。
另外,使用 OOP 接口作为mentioned in the docs 是首选。
注意 - 使用fig.tight_layout()时当前的建议似乎有问题
【问题讨论】:
-
附带说明,如果您不需要
ax.scatter和ax.text的返回值,则根本不需要将它们分配给变量。 -
@mapf 我已经为你添加了导入 - 而且 - 我知道我不需要它们,我只是分配它们,所以我不会在 jupyterlab 中得到输出
标签: python numpy matplotlib plot