如何在 Matplotlib 中更改图例字体名称?
要在 matplotlib 中更改图例字体,我们可以采取以下步骤 −
设置图形大小并调整子图之间和周围的边距。
使用 numpy 创建 x 数据点。
使用 plot() 方法绘制 x、sin(x) 和 cos(x)。
使用 legend() 方法放置图例。
迭代 legend.get_texts() 并更新图例字体名称。
要显示图形,请使用 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.linspace(-5, 5, 100)
plt.plot(x, np.sin(x), label="$y=sin(x)$")
plt.plot(x, np.cos(x), label="$y=cos(x)$")
legend = plt.legend(loc='upper right')
i = 1
for t in legend.get_texts():
t.set_text("name %d" % i)
i += 1
plt.show()输出

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