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

Cách viết chỉ số dưới (subscript) trong nhãn trục và legend Matplotlib

Dùng cú pháp LaTeX $X_{axis}$ trong xlabel, ylabel, label để tạo subscript.

  • x = linspace(1,10,1000), y = exp(x).
  • plt.plot(x, y, label=r'$e^x$', c="red", lw=2).
  • plt.xlabel("$X_{axis}$"), plt.ylabel("$Y_{axis}$").
  • plt.legend(loc='upper left').
  • 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.linspace(1, 10, 1000)
y = np.exp(x)
plt.plot(x, y, label=r'$e^x$', c="red", lw=2)
plt.xlabel("$X_{axis}$")
plt.ylabel("$Y_{axis}$")
plt.legend(loc='upper left')
plt.show()

Kết quả

Đồ thị e^x với nhãn trục X_axis, Y_axis (chữ axis ở dưới), legend e^x (x trên).