Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách thay đổi font chữ nhãn trục khi render bằng LaTeX trong Matplotlib

Để thay đổi font nhãn trục (tick labels) khi dùng LaTeX rendering:

  • Tạo dữ liệu x, y bằng numpy.
  • Thêm subplot, đặt xticks, yticks.
  • Vẽ đồ thị với màu đỏ.
  • Sử dụng biểu diễn LaTeX để đặt font in đậm (\\bf{}).
  • Hiển thị bằng show().

Ví dụ

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()

Kết quả

Hiển thị biểu đồ với nhãn trục được render bằng LaTeX, font in đậm.