如何在Matplotlib中添加粗体的注释文字?

如何在Matplotlib中添加粗体的注释文字?

在Matplotlib中添加粗体的注释文字,我们可以使用LaTeX表示法表示标签。

步骤

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

  • 使用numpy创建 xy 数据点。

  • 要为每个散点设置标签,请创建标签列表。

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

  • 遍历压缩的 labels, xpointsypoints

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

输出

如何在Matplotlib中添加粗体的注释文字?

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程