如何在 Matplotlib 中添加加粗注释文本?


要添加加粗注释文本,我们可以在 matplotlib 中使用 LaTeX 表示法作为标记。

步骤

  • 设置图形大小并调整子图之间和周围的填充。

  • 使用 numpy 创建xy数据点。

  • 要设置每个分散点的标签,请列出标签。

  • 使用scatter()方法绘制xpoints、ypoints。对于颜色,请使用 xpoints。

  • 迭代拉链化的标签、xpointsypoints。

  • 在 for 循环中使用带加粗 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()

输出

更新于: 03-06-2021

1K+ 浏览

助力您的 职业

完成课程,获得认证

开始
广告
© . All rights reserved.