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

Căn chỉnh nhãn trục phải/trên trong Matplotlib


Các bước

  1. Thiết lập kích thước hình và padding.
  2. Tạo figure và subplots.
  3. Khởi tạo biến N cho số mẫu.
  4. Vẽ dữ liệu x, y bằng plot().
  5. Đặt xlabel ở vị trí right, ylabel ở top.
  6. Gọi show().

Ví dụ

import numpy as np
from matplotlib import pyplot as plt

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

fig, ax = plt.subplots()
N = 10
x = np.random.rand(N)
y = np.random.rand(N)
ax.plot(x, y)
ax.set_xlabel("X-axis", loc="right")
ax.set_ylabel("Y-axis", loc="top")
plt.show()

Kết quả

Căn chỉnh nhãn trục phải/trên trong Matplotlib