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

Tùy chỉnh nhãn trục X trong Matplotlib


Các bước

  1. Thiết lập kích thước hình và padding.
  2. Khởi tạo biến N cho số mẫu.
  3. Tạo dữ liệu x, y bằng numpy.
  4. Vẽ đồ thị bằng plot().
  5. Tùy chỉnh nhãn trục X với fontweight, color, fontsize, horizontalalignment.
  6. Gọi show().

Ví dụ

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

N = 100
x = np.random.rand(N)
y = np.random.rand(N)

plt.plot(x, y, 'r*')
plt.xlabel('X-axis Label', fontweight='bold', color='orange', fontsize='17', horizontalalignment='center')
plt.show()

Kết quả

Tùy chỉnh nhãn trục X trong Matplotlib