如何在 Matplotlib 的数据坐标中将注释写在绘图之外?
matplotlibpythondata visualization更新于 2025/6/1 7:08:17
我们可以使用 annotate() 方法将注释放在绘图之外。
步骤
设置图形大小并调整子图之间和周围的填充。
使用 numpy 创建 x 和 y 数据点。
使用 subplots() 方法创建一个图形和一组子图。
使用 scatter() 方法使用星号标记和铜色图绘制 x 和 y 数据点。
要将注释放在绘图之外,请使用 xy 坐标元组相应地。
要显示图形,请使用 show() 方法。
示例
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.random.rand(100)
y = np.random.rand(100)
fig, ax = plt.subplots()
ax.scatter(x, y, c=y, marker="*", cmap="copper")
ax.annotate('Scatter points(outside the drawing)', xy=(0.30, 1.05), xycoords=ax.get_xaxis_transform())
plt.show()
输出

相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

