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

Python xticks trong subplots


Subplot chia figure thành nrow*ncols phần và plt.xticks giúp vẽ xticks cho subplots.

Các bước

  • Tạo hai list cho line 1 và line 2.

  • Thêm subplot vào figure hiện tại, nrow=1, ncols=2, index=1.

  • Vẽ line 1 style dashed.

  • Đặt auto scaling margins(0.2).

  • Đặt xticks ở vị trí chẵn.

  • Đặt tiêu đề trục X.

  • Thêm subplot index=2.

  • Vẽ line 2.

  • Đặt auto scaling margins(0.2).

  • Đặt xticks ở vị trí lẻ.

  • Đặt tiêu đề trục X.

  • Hiển thị figure bằng plt.show().

Ví dụ

import matplotlib.pyplot as plt
line1 = [21, 14, 81]
line2 = [31, 6, 12]
plt.subplot(121)
plt.plot(line1, linestyle='dashed')
plt.margins(0.2)
plt.xticks([2, 4, 6, 8, 10])
plt.title("x-ticks at even places")
plt.subplot(122)
plt.plot(line2)
plt.margins(0.2)
plt.xticks([1, 3, 5, 7, 9])
plt.title("x-ticks at odd places")
plt.show()

Kết quả

Python xticks trong subplots