使用 LaTeX 渲染时,我如何更改 Matplotlib 绘图中的坐标轴刻度字体?
使用 LaTeX 渲染时,若要更改 matplotlib 中的坐标轴刻度字体,我们可以采取以下步骤:
使用 numpy 创建 x 和 y 数据点。
使用 subplot() 方法,将子图添加到当前图形中。
使用 set_xticks 和 set_yticks 方法分别使用数据点 x 和 y 设置 x 和 y 刻度。
使用 plot() 方法并设置 color=red 绘制 x 和 y。
若要设置粗体字重,我们可以使用 LaTeX 表示法。
若要显示图形,请使用 show() 方法。
示例
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.array([1, 2, 3, 4])
y = np.exp(x)
ax1 = plt.subplot()
ax1.set_xticks(x)
ax1.set_yticks(y)
ax1.plot(x, y, c="red")
ax1.set_xticklabels(["$\bf{one}$", "$\bf{two}$", "$\bf{three}$", "$\bf{four}$"], rotation=45)
ax1.set_yticklabels(["$\bf{:.2f}$".format(y[0]), "$\bf{:.2f}$".format(y[1]),
"$\bf{:.2f}$".format(y[2]), "$\bf{:.2f}$".format(y[3])], rotation=45)
plt.tight_layout()
plt.show()输出

广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP