如何在 Matplotlib 中水平居中一个注释,使其对齐一个点?
若要水平居中对齐注释,以便对其一个点,我们可以采取以下步骤:
- 使用 numpy 创建 x 和 y 的点。
- 使用 xpoints 创建标签。
- 使用 scatter() 方法来分散这些点。
- 迭代标签、xpoints 和 ypoints,并用标签、x 和 y 注释该图,使用不同的属性,使水平对齐 ha=center。
- 若要显示该图,请使用 show() 方法。
示例
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True xpoints = np.linspace(1, 10, 10) ypoints = np.random.rand(10) labels = ["%.2f" % i for i in xpoints] plt.scatter(xpoints, ypoints, c=xpoints) for label, x, y in zip(labels, xpoints, ypoints): plt.annotate( label, xy=(x, y), xytext=(-20, 20), textcoords='offset points', ha='center', va='bottom', bbox=dict(boxstyle='round,pad=0.5', fc='green', alpha=0.5), arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0')) plt.show()
输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP