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

Vẽ đường ngang trên nhiều subplots trong Python dùng pyplot

Dùng subplots tạo nhiều axes, và axhline() để vẽ đường ngang trên từng axes.

  • fig, (ax1, ax2, ax3) = plt.subplots(3).
  • ax1.axhline(y=0.5, xmin=0, xmax=3, c="black", linewidth=2, zorder=0).
  • ax2.axhline(y=0.5, xmin=0, xmax=3, c="red", linewidth=3, zorder=0).
  • ax3.axhline(y=0.5, xmin=0, xmax=3, c="yellow", linewidth=4, zorder=0).
  • show().

Ví dụ

from matplotlib import pyplot as plt
fig, (ax1, ax2, ax3) = plt.subplots(3)
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
ax1.axhline(y=0.5, xmin=0, xmax=3, c="black", linewidth=2, zorder=0)
ax2.axhline(y=0.5, xmin=0, xmax=3, c="red", linewidth=3, zorder=0)
ax3.axhline(y=0.5, xmin=0, xmax=3, c="yellow", linewidth=4, zorder=0)
plt.show()

Kết quả

Ba subplot chồng nhau, mỗi cái có một đường ngang y=0.5 với màu và độ dày khác nhau.