Hướng dẫn how do you plot two graphs simultaneously in python? - làm thế nào để bạn vẽ hai đồ thị đồng thời trong python?
Ghi chú Nhấn vào đây để tải xuống mã ví dụ đầy đủhere to download the full example code Một con số chỉ có một subplot#
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. Xếp các ô con theo một hướng#Hai đối số tùy chọn đầu tiên của 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. 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. Để 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. 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() 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() 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. Bạn có thể sử dụng chia sẻ hoặc chia sẻ để căn chỉnh trục ngang hoặc dọc. Đặ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. Đố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 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. 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() 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() Trục cực#Tham số subplot_kw của 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 Làm thế nào để bạn kết hợp đồ thị trong Python?Làm thế nào để làm nó.... Cài đặt matplotlib bằng cách mở dấu nhắc lệnh Python và bắn pip cài đặt matplotlib. .... Chuẩn bị dữ liệu được hiển thị. .... Chia dữ liệu thành các mảng cho các đơn vị di động của mỗi công ty. .... Tạo các mẫu phụ đầu tiên. .... Tạo biểu đồ thanh với thông tin về iPhone_sales .. Làm thế nào để bạn vẽ nhiều biểu đồ trên các hình khác nhau trong Python?Quản lý nhiều hình trong Pyplot.. Nhập matplotlib.pyplot như PLT Nhập Numpy như NP T = NP.Arange (0,0, 2.0, 0,01) S1 = NP.tội lỗi (2*np. pi*t) s2 = np.Tội lỗi (4*np. .... Hình (1) PLT.Subplot (211) plt.Lô đất (T, S1) plt.Subplot (212) plt.Lô đất (t, 2*S1). Hình (1) PLT.Subplot (211) plt.Biểu đồ (t, s2, 's') ax = plt.GCA () AX .. Làm thế nào để bạn vẽ hai điều trong Python?Bạn có thể vẽ nhiều dòng từ dữ liệu được cung cấp bởi DataFrame trong Python bằng matplotlib.Bạn có thể làm điều đó bằng cách chỉ định các cột khác nhau của DataFrame là tham số trục x và y trong hàm matplotlib.pyplot.plot ().specifying different columns of the dataframe as the x and y-axis parameters in the matplotlib. pyplot. plot() function.
Làm thế nào để tôi vẽ hai lô trong matplotlib?Để tạo nhiều lô, sử dụng matplotlib.pyplot.Phương thức Subplots trả về hình cùng với đối tượng trục hoặc mảng của đối tượng trục.Các thuộc tính NCOLS của phương thức Subplots () Xác định số lượng hàng và cột của lưới Subplot. |