Hướng dẫn how do you plot two plots in python? - làm thế nào để bạn vẽ hai ô trong python?

Ghi chú

Nhấn vào đây để tải xuống mã ví dụ đầy đủhere to download the full example code

pyplot.subplots tạo ra một con số và một lưới các ô con với một cuộc gọi duy nhất, trong khi cung cấp kiểm soát hợp lý về cách các lô riêng lẻ được tạo ra. Đối với các trường hợp sử dụng nâng cao hơn, bạn có thể sử dụng GridSpec cho bố cục subplot tổng quát hơn hoặc Figure.add_subplot để thêm các ô phụ tại các vị trí tùy ý trong hình.

import matplotlib.pyplot as plt
import numpy as np

# Some example data to display
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

Một con số chỉ có một subplot#

subplots() mà không có đối số trả về Figure và một

fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[0, 0].set_title('Axis [0, 0]')
axs[0, 1].plot(x, y, 'tab:orange')
axs[0, 1].set_title('Axis [0, 1]')
axs[1, 0].plot(x, -y, 'tab:green')
axs[1, 0].set_title('Axis [1, 0]')
axs[1, 1].plot(x, -y, 'tab:red')
axs[1, 1].set_title('Axis [1, 1]')

for ax in axs.flat:
    ax.set(xlabel='x-label', ylabel='y-label')

# Hide x labels and tick labels for top plots and y ticks for right plots.
for ax in axs.flat:
    ax.label_outer()
0.

Đây thực sự là cách đơn giản nhất và được khuyến nghị để tạo một hình và trục duy nhất.

Hướng dẫn how do you plot two plots in python? - làm thế nào để bạn vẽ hai ô trong python?

Xếp các ô con theo một hướng#

Hai đối số tùy chọn đầu tiên của pyplot.subplots xác định số lượng hàng và cột của lưới phụ.

Khi chỉ xếp theo một hướng,

fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[0, 0].set_title('Axis [0, 0]')
axs[0, 1].plot(x, y, 'tab:orange')
axs[0, 1].set_title('Axis [0, 1]')
axs[1, 0].plot(x, -y, 'tab:green')
axs[1, 0].set_title('Axis [1, 0]')
axs[1, 1].plot(x, -y, 'tab:red')
axs[1, 1].set_title('Axis [1, 1]')

for ax in axs.flat:
    ax.set(xlabel='x-label', ylabel='y-label')

# Hide x labels and tick labels for top plots and y ticks for right plots.
for ax in axs.flat:
    ax.label_outer()
2 được trả về là một mảng numpy 1D chứa danh sách các trục được tạo.

Hướng dẫn how do you plot two plots in python? - làm thế nào để bạn vẽ hai ô trong python?

Nếu bạn đang tạo chỉ một vài trục, thật tiện lợi khi giải nén chúng ngay lập tức cho các biến chuyên dụng cho mỗi trục. Bằng cách đó, chúng ta có thể sử dụng

fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[0, 0].set_title('Axis [0, 0]')
axs[0, 1].plot(x, y, 'tab:orange')
axs[0, 1].set_title('Axis [0, 1]')
axs[1, 0].plot(x, -y, 'tab:green')
axs[1, 0].set_title('Axis [1, 0]')
axs[1, 1].plot(x, -y, 'tab:red')
axs[1, 1].set_title('Axis [1, 1]')

for ax in axs.flat:
    ax.set(xlabel='x-label', ylabel='y-label')

# Hide x labels and tick labels for top plots and y ticks for right plots.
for ax in axs.flat:
    ax.label_outer()
3 thay vì nhiều hơn
fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[0, 0].set_title('Axis [0, 0]')
axs[0, 1].plot(x, y, 'tab:orange')
axs[0, 1].set_title('Axis [0, 1]')
axs[1, 0].plot(x, -y, 'tab:green')
axs[1, 0].set_title('Axis [1, 0]')
axs[1, 1].plot(x, -y, 'tab:red')
axs[1, 1].set_title('Axis [1, 1]')

for ax in axs.flat:
    ax.set(xlabel='x-label', ylabel='y-label')

# Hide x labels and tick labels for top plots and y ticks for right plots.
for ax in axs.flat:
    ax.label_outer()
4.

Hướng dẫn how do you plot two plots in python? - làm thế nào để bạn vẽ hai ô trong python?

Để có được các ô phụ bên cạnh, hãy truyền các tham số

fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[0, 0].set_title('Axis [0, 0]')
axs[0, 1].plot(x, y, 'tab:orange')
axs[0, 1].set_title('Axis [0, 1]')
axs[1, 0].plot(x, -y, 'tab:green')
axs[1, 0].set_title('Axis [1, 0]')
axs[1, 1].plot(x, -y, 'tab:red')
axs[1, 1].set_title('Axis [1, 1]')

for ax in axs.flat:
    ax.set(xlabel='x-label', ylabel='y-label')

# Hide x labels and tick labels for top plots and y ticks for right plots.
for ax in axs.flat:
    ax.label_outer()
5 cho một hàng và hai cột.

Hướng dẫn how do you plot two plots in python? - làm thế nào để bạn vẽ hai ô trong python?

Xếp các ô con theo hai hướng#

Khi xếp chồng theo hai hướng,

fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[0, 0].set_title('Axis [0, 0]')
axs[0, 1].plot(x, y, 'tab:orange')
axs[0, 1].set_title('Axis [0, 1]')
axs[1, 0].plot(x, -y, 'tab:green')
axs[1, 0].set_title('Axis [1, 0]')
axs[1, 1].plot(x, -y, 'tab:red')
axs[1, 1].set_title('Axis [1, 1]')

for ax in axs.flat:
    ax.set(xlabel='x-label', ylabel='y-label')

# Hide x labels and tick labels for top plots and y ticks for right plots.
for ax in axs.flat:
    ax.label_outer()
2 được trả về là một mảng 2D.

Nếu bạn phải đặt các tham số cho mỗi Subplot, nó tiện dụng để lặp lại trên tất cả các ô con trong lưới 2D bằng cách sử dụng

fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[0, 0].set_title('Axis [0, 0]')
axs[0, 1].plot(x, y, 'tab:orange')
axs[0, 1].set_title('Axis [0, 1]')
axs[1, 0].plot(x, -y, 'tab:green')
axs[1, 0].set_title('Axis [1, 0]')
axs[1, 1].plot(x, -y, 'tab:red')
axs[1, 1].set_title('Axis [1, 1]')

for ax in axs.flat:
    ax.set(xlabel='x-label', ylabel='y-label')

# Hide x labels and tick labels for top plots and y ticks for right plots.
for ax in axs.flat:
    ax.label_outer()
7.

fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[0, 0].set_title('Axis [0, 0]')
axs[0, 1].plot(x, y, 'tab:orange')
axs[0, 1].set_title('Axis [0, 1]')
axs[1, 0].plot(x, -y, 'tab:green')
axs[1, 0].set_title('Axis [1, 0]')
axs[1, 1].plot(x, -y, 'tab:red')
axs[1, 1].set_title('Axis [1, 1]')

for ax in axs.flat:
    ax.set(xlabel='x-label', ylabel='y-label')

# Hide x labels and tick labels for top plots and y ticks for right plots.
for ax in axs.flat:
    ax.label_outer()

Hướng dẫn how do you plot two plots in python? - làm thế nào để bạn vẽ hai ô trong python?

Bạn cũng có thể sử dụng Tuple-Unpacking trong 2D để gán tất cả các ô con cho các biến chuyên dụng:

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
fig.suptitle('Sharing x per column, y per row')
ax1.plot(x, y)
ax2.plot(x, y**2, 'tab:orange')
ax3.plot(x, -y, 'tab:green')
ax4.plot(x, -y**2, 'tab:red')

for ax in fig.get_axes():
    ax.label_outer()

Hướng dẫn how do you plot two plots in python? - làm thế nào để bạn vẽ hai ô trong python?

Chia sẻ trục#

Theo mặc định, mỗi trục được chia tỷ lệ riêng lẻ. Do đó, nếu các phạm vi khác nhau, các giá trị đánh dấu của các ô con không căn chỉnh.

Hướng dẫn how do you plot two plots in python? - làm thế nào để bạn vẽ hai ô trong python?

Bạn có thể sử dụng chia sẻ hoặc chia sẻ để căn chỉnh trục ngang hoặc dọc.

Hướng dẫn how do you plot two plots in python? - làm thế nào để bạn vẽ hai ô trong python?

Đặt chia sẻ hoặc chia sẻ thành

fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[0, 0].set_title('Axis [0, 0]')
axs[0, 1].plot(x, y, 'tab:orange')
axs[0, 1].set_title('Axis [0, 1]')
axs[1, 0].plot(x, -y, 'tab:green')
axs[1, 0].set_title('Axis [1, 0]')
axs[1, 1].plot(x, -y, 'tab:red')
axs[1, 1].set_title('Axis [1, 1]')

for ax in axs.flat:
    ax.set(xlabel='x-label', ylabel='y-label')

# Hide x labels and tick labels for top plots and y ticks for right plots.
for ax in axs.flat:
    ax.label_outer()
8 cho phép chia sẻ toàn cầu trên toàn bộ lưới, tức là các trục y của các ô phụ xếp chồng lên theo chiều dọc có cùng thang điểm khi sử dụng
fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[0, 0].set_title('Axis [0, 0]')
axs[0, 1].plot(x, y, 'tab:orange')
axs[0, 1].set_title('Axis [0, 1]')
axs[1, 0].plot(x, -y, 'tab:green')
axs[1, 0].set_title('Axis [1, 0]')
axs[1, 1].plot(x, -y, 'tab:red')
axs[1, 1].set_title('Axis [1, 1]')

for ax in axs.flat:
    ax.set(xlabel='x-label', ylabel='y-label')

# Hide x labels and tick labels for top plots and y ticks for right plots.
for ax in axs.flat:
    ax.label_outer()
9.

Hướng dẫn how do you plot two plots in python? - làm thế nào để bạn vẽ hai ô trong python?

Đối với các ô phụ đang chia sẻ trục, một bộ nhãn Tick là đủ. Nhãn đánh dấu của các trục bên trong được tự động xóa bởi Sharex và Sharey. Vẫn còn một khoảng trống không sử dụng giữa các ô con.

Để kiểm soát chính xác vị trí của các ô con, người ta có thể tạo rõ ràng GridSpec với

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
fig.suptitle('Sharing x per column, y per row')
ax1.plot(x, y)
ax2.plot(x, y**2, 'tab:orange')
ax3.plot(x, -y, 'tab:green')
ax4.plot(x, -y**2, 'tab:red')

for ax in fig.get_axes():
    ax.label_outer()
1, sau đó gọi phương thức
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
fig.suptitle('Sharing x per column, y per row')
ax1.plot(x, y)
ax2.plot(x, y**2, 'tab:orange')
ax3.plot(x, -y, 'tab:green')
ax4.plot(x, -y**2, 'tab:red')

for ax in fig.get_axes():
    ax.label_outer()
2 của nó. Ví dụ, chúng ta có thể giảm chiều cao giữa các ô phụ dọc bằng cách sử dụng
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
fig.suptitle('Sharing x per column, y per row')
ax1.plot(x, y)
ax2.plot(x, y**2, 'tab:orange')
ax3.plot(x, -y, 'tab:green')
ax4.plot(x, -y**2, 'tab:red')

for ax in fig.get_axes():
    ax.label_outer()
3.

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
fig.suptitle('Sharing x per column, y per row')
ax1.plot(x, y)
ax2.plot(x, y**2, 'tab:orange')
ax3.plot(x, -y, 'tab:green')
ax4.plot(x, -y**2, 'tab:red')

for ax in fig.get_axes():
    ax.label_outer()
4 là một phương pháp tiện dụng để loại bỏ nhãn và ve từ các ô con không ở rìa của lưới.

Hướng dẫn how do you plot two plots in python? - làm thế nào để bạn vẽ hai ô trong python?

Ngoài

fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[0, 0].set_title('Axis [0, 0]')
axs[0, 1].plot(x, y, 'tab:orange')
axs[0, 1].set_title('Axis [0, 1]')
axs[1, 0].plot(x, -y, 'tab:green')
axs[1, 0].set_title('Axis [1, 0]')
axs[1, 1].plot(x, -y, 'tab:red')
axs[1, 1].set_title('Axis [1, 1]')

for ax in axs.flat:
    ax.set(xlabel='x-label', ylabel='y-label')

# Hide x labels and tick labels for top plots and y ticks for right plots.
for ax in axs.flat:
    ax.label_outer()
8 và
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
fig.suptitle('Sharing x per column, y per row')
ax1.plot(x, y)
ax2.plot(x, y**2, 'tab:orange')
ax3.plot(x, -y, 'tab:green')
ax4.plot(x, -y**2, 'tab:red')

for ax in fig.get_axes():
    ax.label_outer()
6, cả Sharex và Sharey đều chấp nhận các giá trị 'hàng' và 'col' để chia sẻ các giá trị chỉ trên mỗi hàng hoặc cột.

fig = plt.figure()
gs = fig.add_gridspec(2, 2, hspace=0, wspace=0)
(ax1, ax2), (ax3, ax4) = gs.subplots(sharex='col', sharey='row')
fig.suptitle('Sharing x per column, y per row')
ax1.plot(x, y)
ax2.plot(x, y**2, 'tab:orange')
ax3.plot(x + 1, -y, 'tab:green')
ax4.plot(x + 2, -y**2, 'tab:red')

for ax in fig.get_axes():
    ax.label_outer()

Hướng dẫn how do you plot two plots in python? - làm thế nào để bạn vẽ hai ô trong python?

Nếu bạn muốn một cấu trúc chia sẻ phức tạp hơn, trước tiên bạn có thể tạo lưới các trục không chia sẻ, sau đó gọi

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
fig.suptitle('Sharing x per column, y per row')
ax1.plot(x, y)
ax2.plot(x, y**2, 'tab:orange')
ax3.plot(x, -y, 'tab:green')
ax4.plot(x, -y**2, 'tab:red')

for ax in fig.get_axes():
    ax.label_outer()
7 hoặc
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
fig.suptitle('Sharing x per column, y per row')
ax1.plot(x, y)
ax2.plot(x, y**2, 'tab:orange')
ax3.plot(x, -y, 'tab:green')
ax4.plot(x, -y**2, 'tab:red')

for ax in fig.get_axes():
    ax.label_outer()
8 để thêm thông tin chia sẻ một posteriori.

fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[0, 0].set_title("main")
axs[1, 0].plot(x, y**2)
axs[1, 0].set_title("shares x with main")
axs[1, 0].sharex(axs[0, 0])
axs[0, 1].plot(x + 1, y + 1)
axs[0, 1].set_title("unrelated")
axs[1, 1].plot(x + 2, y + 2)
axs[1, 1].set_title("also unrelated")
fig.tight_layout()

Hướng dẫn how do you plot two plots in python? - làm thế nào để bạn vẽ hai ô trong python?

Trục cực#

Tham số subplot_kw của pyplot.subplots kiểm soát các thuộc tính subplot (xem thêm Figure.add_subplot). Cụ thể, điều này có thể được sử dụng để tạo ra một lưới các trục cực.

Hướng dẫn how do you plot two plots in python? - làm thế nào để bạn vẽ hai ô trong python?

Tổng thời gian chạy của tập lệnh: (0 phút 7,949 giây) ( 0 minutes 7.949 seconds)

Phòng trưng bày được tạo ra bởi Sphinx-Gallery