如何在Matplotlib中添加粗体的注释文字?
在Matplotlib中添加粗体的注释文字,我们可以使用LaTeX表示法表示标签。
步骤
- 设置图形大小并调整子图之间和周围的填充。
-
使用numpy创建 x 和 y 数据点。
-
要为每个散点设置标签,请创建标签列表。
-
使用 scatter() 方法绘制 xpoints, ypoints 。对于颜色,请使用xpoints。
-
遍历压缩的 labels, xpoints 和 ypoints 。
-
在循环中使用LaTeX显示粗体注释。 annotate() 方法。
-
使用 show() 方法显示图形。
示例
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 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(
f"\bf{label}",
xy=(x, y), xytext=(-20, 20),
textcoords='offset points', ha='center', va='bottom',
arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0'))
plt.show()