Hướng dẫn how do you plot x and y values in python? - làm thế nào để bạn vẽ biểu đồ giá trị x và y trong python?
Ghi chú Nhấn vào đây để tải xuống mã ví dụ đầy đủhere to download the full example code Giới thiệu về giao diện Pyplot. Cũng vui lòng xem Hướng dẫn bắt đầu nhanh để biết tổng quan về cách thức hoạt động của matplotlib và giao diện ứng dụng matplotlib (API) để giải thích về sự đánh đổi giữa API người dùng được hỗ trợ.Quick start guide for an overview of how Matplotlib works and Matplotlib Application Interfaces (APIs) for an explanation of the trade-offs between the supported user APIs. Trong Ghi chú API PYPLOT ngầm thường ít dài dòng hơn nhưng cũng không linh hoạt như API rõ ràng. Hầu hết các chức năng mà bạn thấy ở đây cũng có thể được gọi là phương thức từ đối tượng plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro') plt.axis([0, 6, 0, 20]) plt.show()2. Chúng tôi khuyên bạn nên duyệt các hướng dẫn và ví dụ để xem cách thức hoạt động của nó. Xem Giao diện ứng dụng MatPlotlib (API) để giải thích về việc giao dịch của API người dùng được hỗ trợ.Matplotlib Application Interfaces (APIs) for an explanation of the trade off of the supported user APIs. Tạo hình ảnh trực quan với pyplot rất nhanh: Bạn có thể tự hỏi tại sao trục X dao động từ 0-3 và trục y từ 1-4. Nếu bạn cung cấp một danh sách hoặc mảng duy nhất cho plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro') plt.axis([0, 6, 0, 20]) plt.show()3, matplotlib giả định đó là một chuỗi các giá trị y và tự động tạo các giá trị x cho bạn. Vì phạm vi Python bắt đầu với 0, vectơ X mặc định có cùng độ dài với y nhưng bắt đầu với 0. Do đó dữ liệu X là plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro') plt.axis([0, 6, 0, 20]) plt.show()4. plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro') plt.axis([0, 6, 0, 20]) plt.show()3 là một hàm linh hoạt và sẽ có một số lượng đối số tùy ý. Ví dụ: để vẽ x so với y, bạn có thể viết: [ Định dạng phong cách của cốt truyện của bạn#Đối với mỗi cặp đối số x, y, có một đối số thứ ba tùy chọn là chuỗi định dạng chỉ ra loại màu và dòng của cốt truyện. Các chữ cái và ký hiệu của chuỗi định dạng là từ MATLAB và bạn kết hợp một chuỗi màu với chuỗi kiểu đường. Chuỗi định dạng mặc định là 'B-', là một đường màu xanh rắn. Ví dụ, để vẽ những điều trên với các vòng tròn màu đỏ, bạn sẽ phát hành plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro') plt.axis([0, 6, 0, 20]) plt.show() Xem tài liệu plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro') plt.axis([0, 6, 0, 20]) plt.show()3 để biết danh sách đầy đủ các kiểu và chuỗi định dạng dòng. Hàm plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro') plt.axis([0, 6, 0, 20]) plt.show()7 trong ví dụ trên lấy danh sách plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro') plt.axis([0, 6, 0, 20]) plt.show()8 và chỉ định chế độ xem của các trục. Nếu matplotlib bị giới hạn trong việc làm việc với các danh sách, thì nó sẽ khá vô dụng đối với xử lý số. Nói chung, bạn sẽ sử dụng các mảng numpy. Trong thực tế, tất cả các chuỗi được chuyển đổi thành mảng numpy bên trong. Ví dụ dưới đây minh họa việc vẽ một số dòng với các kiểu định dạng khác nhau trong một cuộc gọi chức năng bằng các mảng. import numpy as np # evenly sampled time at 200ms intervals t = np.arange(0., 5., 0.2) # red dashes, blue squares and green triangles plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^') plt.show() Vẽ với chuỗi từ khóa#Có một số trường hợp bạn có dữ liệu ở định dạng cho phép bạn truy cập các biến cụ thể bằng chuỗi. Ví dụ, với plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro') plt.axis([0, 6, 0, 20]) plt.show()9 hoặc import numpy as np # evenly sampled time at 200ms intervals t = np.arange(0., 5., 0.2) # red dashes, blue squares and green triangles plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^') plt.show()0. Matplotlib cho phép bạn cung cấp một đối tượng như vậy với đối số từ khóa import numpy as np # evenly sampled time at 200ms intervals t = np.arange(0., 5., 0.2) # red dashes, blue squares and green triangles plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^') plt.show()1. Nếu được cung cấp, thì bạn có thể tạo các sơ đồ với các chuỗi tương ứng với các biến này. Vẽ với các biến phân loại#Cũng có thể tạo ra một lô bằng cách sử dụng các biến phân loại. Matplotlib cho phép bạn truyền trực tiếp các biến phân loại cho nhiều chức năng âm mưu. Ví dụ: Kiểm soát các thuộc tính dòng#Các dòng có nhiều thuộc tính mà bạn có thể đặt: linwidth, dash style, antialiased, vv; Xem import numpy as np # evenly sampled time at 200ms intervals t = np.arange(0., 5., 0.2) # red dashes, blue squares and green triangles plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^') plt.show()2. Có một số cách để đặt thuộc tính dòng
Dưới đây là các thuộc tính import numpy as np # evenly sampled time at 200ms intervals t = np.arange(0., 5., 0.2) # red dashes, blue squares and green triangles plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^') plt.show()3 có sẵn.
Zorder In [69]: lines = plt.plot([1, 2, 3]) In [70]: plt.setp(lines) alpha: float animated: [True | False] antialiased or aa: [True | False] ...snip bất kỳ số nàoĐể có được danh sách các thuộc tính dòng có thể giải quyết được, hãy gọi hàm import numpy as np # evenly sampled time at 200ms intervals t = np.arange(0., 5., 0.2) # red dashes, blue squares and green triangles plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^') plt.show()8 với một dòng hoặc dòng làm đối số def f(t): return np.exp(-t) * np.cos(2*np.pi*t) t1 = np.arange(0.0, 5.0, 0.1) t2 = np.arange(0.0, 5.0, 0.02) plt.figure() plt.subplot(211) plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k') plt.subplot(212) plt.plot(t2, np.cos(2*np.pi*t2), 'r--') plt.show() Làm việc với nhiều hình và trục# MATLAB và plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro') plt.axis([0, 6, 0, 20]) plt.show()0, có khái niệm về hình hiện tại và các trục hiện tại. Tất cả các chức năng âm mưu áp dụng cho các trục hiện tại. Hàm def f(t): return np.exp(-t) * np.cos(2*np.pi*t) t1 = np.arange(0.0, 5.0, 0.1) t2 = np.arange(0.0, 5.0, 0.02) plt.figure() plt.subplot(211) plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k') plt.subplot(212) plt.plot(t2, np.cos(2*np.pi*t2), 'r--') plt.show()7 trả về các trục hiện tại (một ví dụ def f(t): return np.exp(-t) * np.cos(2*np.pi*t) t1 = np.arange(0.0, 5.0, 0.1) t2 = np.arange(0.0, 5.0, 0.02) plt.figure() plt.subplot(211) plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k') plt.subplot(212) plt.plot(t2, np.cos(2*np.pi*t2), 'r--') plt.show()8) và def f(t): return np.exp(-t) * np.cos(2*np.pi*t) t1 = np.arange(0.0, 5.0, 0.1) t2 = np.arange(0.0, 5.0, 0.02) plt.figure() plt.subplot(211) plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k') plt.subplot(212) plt.plot(t2, np.cos(2*np.pi*t2), 'r--') plt.show()9 trả về hình hiện tại (một ví dụ mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000) # the histogram of the data n, bins, patches = plt.hist(x, 50, density=True, facecolor='g', alpha=0.75) plt.xlabel('Smarts') plt.ylabel('Probability') plt.title('Histogram of IQ') plt.text(60, .025, r'$\mu=100,\ \sigma=15$') plt.axis([40, 160, 0, 0.03]) plt.grid(True) plt.show()0). Thông thường, bạn không phải lo lắng về điều này, bởi vì tất cả đều được chăm sóc đằng sau hậu trường. Dưới đây là một kịch bản để tạo hai ô con.Axes Demo for an example of placing axes manually and Multiple subplots for an example with lots of subplots. Cuộc gọi mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000) # the histogram of the data n, bins, patches = plt.hist(x, 50, density=True, facecolor='g', alpha=0.75) plt.xlabel('Smarts') plt.ylabel('Probability') plt.title('Histogram of IQ') plt.text(60, .025, r'$\mu=100,\ \sigma=15$') plt.axis([40, 160, 0, 0.03]) plt.grid(True) plt.show()1 ở đây là tùy chọn vì một con số sẽ được tạo nếu không có tồn tại, giống như một trục sẽ được tạo (tương đương với một cuộc gọi mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000) # the histogram of the data n, bins, patches = plt.hist(x, 50, density=True, facecolor='g', alpha=0.75) plt.xlabel('Smarts') plt.ylabel('Probability') plt.title('Histogram of IQ') plt.text(60, .025, r'$\mu=100,\ \sigma=15$') plt.axis([40, 160, 0, 0.03]) plt.grid(True) plt.show()2 rõ ràng) nếu không có tồn tại. Cuộc gọi mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000) # the histogram of the data n, bins, patches = plt.hist(x, 50, density=True, facecolor='g', alpha=0.75) plt.xlabel('Smarts') plt.ylabel('Probability') plt.title('Histogram of IQ') plt.text(60, .025, r'$\mu=100,\ \sigma=15$') plt.axis([40, 160, 0, 0.03]) plt.grid(True) plt.show()3 chỉ định mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000) # the histogram of the data n, bins, patches = plt.hist(x, 50, density=True, facecolor='g', alpha=0.75) plt.xlabel('Smarts') plt.ylabel('Probability') plt.title('Histogram of IQ') plt.text(60, .025, r'$\mu=100,\ \sigma=15$') plt.axis([40, 160, 0, 0.03]) plt.grid(True) plt.show()4 trong đó mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000) # the histogram of the data n, bins, patches = plt.hist(x, 50, density=True, facecolor='g', alpha=0.75) plt.xlabel('Smarts') plt.ylabel('Probability') plt.title('Histogram of IQ') plt.text(60, .025, r'$\mu=100,\ \sigma=15$') plt.axis([40, 160, 0, 0.03]) plt.grid(True) plt.show()5 dao động từ 1 đến mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000) # the histogram of the data n, bins, patches = plt.hist(x, 50, density=True, facecolor='g', alpha=0.75) plt.xlabel('Smarts') plt.ylabel('Probability') plt.title('Histogram of IQ') plt.text(60, .025, r'$\mu=100,\ \sigma=15$') plt.axis([40, 160, 0, 0.03]) plt.grid(True) plt.show()6. Các dấu phẩy trong cuộc gọi mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000) # the histogram of the data n, bins, patches = plt.hist(x, 50, density=True, facecolor='g', alpha=0.75) plt.xlabel('Smarts') plt.ylabel('Probability') plt.title('Histogram of IQ') plt.text(60, .025, r'$\mu=100,\ \sigma=15$') plt.axis([40, 160, 0, 0.03]) plt.grid(True) plt.show()3 là tùy chọn nếu mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000) # the histogram of the data n, bins, patches = plt.hist(x, 50, density=True, facecolor='g', alpha=0.75) plt.xlabel('Smarts') plt.ylabel('Probability') plt.title('Histogram of IQ') plt.text(60, .025, r'$\mu=100,\ \sigma=15$') plt.axis([40, 160, 0, 0.03]) plt.grid(True) plt.show()8. Vì vậy, mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000) # the histogram of the data n, bins, patches = plt.hist(x, 50, density=True, facecolor='g', alpha=0.75) plt.xlabel('Smarts') plt.ylabel('Probability') plt.title('Histogram of IQ') plt.text(60, .025, r'$\mu=100,\ \sigma=15$') plt.axis([40, 160, 0, 0.03]) plt.grid(True) plt.show()9 giống hệt với ax = plt.subplot() t = np.arange(0.0, 5.0, 0.01) s = np.cos(2*np.pi*t) line, = plt.plot(t, s, lw=2) plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05), ) plt.ylim(-2, 2) plt.show()0. Bạn có thể tạo một số lượng tùy ý của các ô con và trục. Nếu bạn muốn đặt một trục theo cách thủ công, tức là, không phải trên lưới hình chữ nhật, hãy sử dụng ax = plt.subplot() t = np.arange(0.0, 5.0, 0.01) s = np.cos(2*np.pi*t) line, = plt.plot(t, s, lw=2) plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05), ) plt.ylim(-2, 2) plt.show()1, cho phép bạn chỉ định vị trí là ax = plt.subplot() t = np.arange(0.0, 5.0, 0.01) s = np.cos(2*np.pi*t) line, = plt.plot(t, s, lw=2) plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05), ) plt.ylim(-2, 2) plt.show()2 trong đó tất cả các giá trị nằm trong tọa độ phân đoạn (0 đến 1). Xem Demo Axes để biết ví dụ về việc đặt trục bằng tay và nhiều ô con cho một ví dụ với rất nhiều ô con.Artist tutorial) Bạn có thể tạo nhiều hình bằng cách sử dụng nhiều cuộc gọi mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000) # the histogram of the data n, bins, patches = plt.hist(x, 50, density=True, facecolor='g', alpha=0.75) plt.xlabel('Smarts') plt.ylabel('Probability') plt.title('Histogram of IQ') plt.text(60, .025, r'$\mu=100,\ \sigma=15$') plt.axis([40, 160, 0, 0.03]) plt.grid(True) plt.show()1 với số hình ngày càng tăng. Tất nhiên, mỗi hình có thể chứa nhiều trục và ô phụ như trái tim bạn mong muốn: Bạn có thể xóa hình hiện tại với ax = plt.subplot() t = np.arange(0.0, 5.0, 0.01) s = np.cos(2*np.pi*t) line, = plt.plot(t, s, lw=2) plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05), ) plt.ylim(-2, 2) plt.show() 4 và các trục hiện tại với ax = plt.subplot() t = np.arange(0.0, 5.0, 0.01) s = np.cos(2*np.pi*t) line, = plt.plot(t, s, lw=2) plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05), ) plt.ylim(-2, 2) plt.show() 5. Nếu bạn thấy khó chịu khi các trạng thái (cụ thể là hình ảnh, hình và trục hiện tại) đang được duy trì cho bạn đằng sau hậu trường, đừng tuyệt vọng: đây chỉ là một trình bao bọc trạng thái mỏng xung quanh một API hướng đối tượng mà bạn có thể sử dụng thay thế (Xem hướng dẫn nghệ sĩ)Nếu bạn đang tạo ra nhiều số liệu, bạn cần phải nhận thức được một điều nữa: bộ nhớ cần thiết cho một con số không được phát hành hoàn toàn cho đến khi hình được đóng rõ ràng với ax = plt.subplot() t = np.arange(0.0, 5.0, 0.01) s = np.cos(2*np.pi*t) line, = plt.plot(t, s, lw=2) plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05), ) plt.ylim(-2, 2) plt.show()6. Xóa tất cả các tham chiếu đến hình và/hoặc sử dụng trình quản lý cửa sổ để giết cửa sổ trong đó hình xuất hiện trên màn hình, là không đủ, vì Pyplot duy trì các tham chiếu nội bộ cho đến khi ax = plt.subplot() t = np.arange(0.0, 5.0, 0.01) s = np.cos(2*np.pi*t) line, = plt.plot(t, s, lw=2) plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05), ) plt.ylim(-2, 2) plt.show()6 được gọi.Text in Matplotlib Plots for a more detailed example) mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000) # the histogram of the data n, bins, patches = plt.hist(x, 50, density=True, facecolor='g', alpha=0.75) plt.xlabel('Smarts') plt.ylabel('Probability') plt.title('Histogram of IQ') plt.text(60, .025, r'$\mu=100,\ \sigma=15$') plt.axis([40, 160, 0, 0.03]) plt.grid(True) plt.show() Làm việc với văn bản# ax = plt.subplot() t = np.arange(0.0, 5.0, 0.01) s = np.cos(2*np.pi*t) line, = plt.plot(t, s, lw=2) plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05), ) plt.ylim(-2, 2) plt.show()8 có thể được sử dụng để thêm văn bản vào một vị trí tùy ý và ax = plt.subplot() t = np.arange(0.0, 5.0, 0.01) s = np.cos(2*np.pi*t) line, = plt.plot(t, s, lw=2) plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05), ) plt.ylim(-2, 2) plt.show()9, # Fixing random state for reproducibility np.random.seed(19680801) # make up some data in the open interval (0, 1) y = np.random.normal(loc=0.5, scale=0.4, size=1000) y = y[(y > 0) & (y < 1)] y.sort() x = np.arange(len(y)) # plot with various axes scales plt.figure() # linear plt.subplot(221) plt.plot(x, y) plt.yscale('linear') plt.title('linear') plt.grid(True) # log plt.subplot(222) plt.plot(x, y) plt.yscale('log') plt.title('log') plt.grid(True) # symmetric log plt.subplot(223) plt.plot(x, y - y.mean()) plt.yscale('symlog', linthresh=0.01) plt.title('symlog') plt.grid(True) # logit plt.subplot(224) plt.plot(x, y) plt.yscale('logit') plt.title('logit') plt.grid(True) # Adjust the subplot layout, because the logit one may take more space # than usual, due to y-tick labels like "1 - 10^{-3}" plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25, wspace=0.35) plt.show()0 và # Fixing random state for reproducibility np.random.seed(19680801) # make up some data in the open interval (0, 1) y = np.random.normal(loc=0.5, scale=0.4, size=1000) y = y[(y > 0) & (y < 1)] y.sort() x = np.arange(len(y)) # plot with various axes scales plt.figure() # linear plt.subplot(221) plt.plot(x, y) plt.yscale('linear') plt.title('linear') plt.grid(True) # log plt.subplot(222) plt.plot(x, y) plt.yscale('log') plt.title('log') plt.grid(True) # symmetric log plt.subplot(223) plt.plot(x, y - y.mean()) plt.yscale('symlog', linthresh=0.01) plt.title('symlog') plt.grid(True) # logit plt.subplot(224) plt.plot(x, y) plt.yscale('logit') plt.title('logit') plt.grid(True) # Adjust the subplot layout, because the logit one may take more space # than usual, due to y-tick labels like "1 - 10^{-3}" plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25, wspace=0.35) plt.show()1 được sử dụng để thêm văn bản vào các vị trí được chỉ định (xem văn bản trong các sơ đồ matplotlib để biết ví dụ chi tiết hơn)Text properties and layout. Tất cả các chức năng ax = plt.subplot() t = np.arange(0.0, 5.0, 0.01) s = np.cos(2*np.pi*t) line, = plt.plot(t, s, lw=2) plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05), ) plt.ylim(-2, 2) plt.show() 8 trả về một ví dụ # Fixing random state for reproducibility np.random.seed(19680801) # make up some data in the open interval (0, 1) y = np.random.normal(loc=0.5, scale=0.4, size=1000) y = y[(y > 0) & (y < 1)] y.sort() x = np.arange(len(y)) # plot with various axes scales plt.figure() # linear plt.subplot(221) plt.plot(x, y) plt.yscale('linear') plt.title('linear') plt.grid(True) # log plt.subplot(222) plt.plot(x, y) plt.yscale('log') plt.title('log') plt.grid(True) # symmetric log plt.subplot(223) plt.plot(x, y - y.mean()) plt.yscale('symlog', linthresh=0.01) plt.title('symlog') plt.grid(True) # logit plt.subplot(224) plt.plot(x, y) plt.yscale('logit') plt.title('logit') plt.grid(True) # Adjust the subplot layout, because the logit one may take more space # than usual, due to y-tick labels like "1 - 10^{-3}" plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25, wspace=0.35) plt.show() 3. Giống như với các dòng ở trên, bạn có thể tùy chỉnh các thuộc tính bằng cách chuyển các đối số từ khóa vào các hàm văn bản hoặc sử dụng import numpy as np # evenly sampled time at 200ms intervals t = np.arange(0., 5., 0.2) # red dashes, blue squares and green triangles plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^') plt.show() 8:Các thuộc tính này được đề cập chi tiết hơn trong các thuộc tính văn bản và bố cục.\(\sigma_i=15\) in the title, you can write a TeX expression surrounded by dollar signs: Sử dụng các biểu thức toán học trong văn bản#Writing mathematical expressions. Thus you can use mathematical text across platforms without requiring a TeX installation. For those who have LaTeX and dvipng installed, you can also use LaTeX to format your text and incorporate the output directly into your display figures or saved postscript -- see Text rendering with LaTeX. Matplotlib chấp nhận các biểu thức phương trình Tex trong bất kỳ biểu thức văn bản nào. Ví dụ: để viết biểu thức \ (\ sigma_i = 15 \) Trong tiêu đề, bạn có thể viết một biểu thức Tex được bao quanh bởi các dấu hiệu đô la:Việc sử dụng hàm ax = plt.subplot() t = np.arange(0.0, 5.0, 0.01) s = np.cos(2*np.pi*t) line, = plt.plot(t, s, lw=2) plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05), ) plt.ylim(-2, 2) plt.show()8 cơ bản trên văn bản đặt tại một vị trí tùy ý trên các trục. Một cách sử dụng phổ biến cho văn bản là để chú thích một số tính năng của cốt truyện và phương pháp # Fixing random state for reproducibility np.random.seed(19680801) # make up some data in the open interval (0, 1) y = np.random.normal(loc=0.5, scale=0.4, size=1000) y = y[(y > 0) & (y < 1)] y.sort() x = np.arange(len(y)) # plot with various axes scales plt.figure() # linear plt.subplot(221) plt.plot(x, y) plt.yscale('linear') plt.title('linear') plt.grid(True) # log plt.subplot(222) plt.plot(x, y) plt.yscale('log') plt.title('log') plt.grid(True) # symmetric log plt.subplot(223) plt.plot(x, y - y.mean()) plt.yscale('symlog', linthresh=0.01) plt.title('symlog') plt.grid(True) # logit plt.subplot(224) plt.plot(x, y) plt.yscale('logit') plt.title('logit') plt.grid(True) # Adjust the subplot layout, because the logit one may take more space # than usual, due to y-tick labels like "1 - 10^{-3}" plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25, wspace=0.35) plt.show()7 cung cấp chức năng trợ giúp để giúp chú thích dễ dàng. Trong một chú thích, có hai điểm cần xem xét: vị trí được chú thích được biểu thị bằng đối số # Fixing random state for reproducibility np.random.seed(19680801) # make up some data in the open interval (0, 1) y = np.random.normal(loc=0.5, scale=0.4, size=1000) y = y[(y > 0) & (y < 1)] y.sort() x = np.arange(len(y)) # plot with various axes scales plt.figure() # linear plt.subplot(221) plt.plot(x, y) plt.yscale('linear') plt.title('linear') plt.grid(True) # log plt.subplot(222) plt.plot(x, y) plt.yscale('log') plt.title('log') plt.grid(True) # symmetric log plt.subplot(223) plt.plot(x, y - y.mean()) plt.yscale('symlog', linthresh=0.01) plt.title('symlog') plt.grid(True) # logit plt.subplot(224) plt.plot(x, y) plt.yscale('logit') plt.title('logit') plt.grid(True) # Adjust the subplot layout, because the logit one may take more space # than usual, due to y-tick labels like "1 - 10^{-3}" plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25, wspace=0.35) plt.show()8 và vị trí của văn bản # Fixing random state for reproducibility np.random.seed(19680801) # make up some data in the open interval (0, 1) y = np.random.normal(loc=0.5, scale=0.4, size=1000) y = y[(y > 0) & (y < 1)] y.sort() x = np.arange(len(y)) # plot with various axes scales plt.figure() # linear plt.subplot(221) plt.plot(x, y) plt.yscale('linear') plt.title('linear') plt.grid(True) # log plt.subplot(222) plt.plot(x, y) plt.yscale('log') plt.title('log') plt.grid(True) # symmetric log plt.subplot(223) plt.plot(x, y - y.mean()) plt.yscale('symlog', linthresh=0.01) plt.title('symlog') plt.grid(True) # logit plt.subplot(224) plt.plot(x, y) plt.yscale('logit') plt.title('logit') plt.grid(True) # Adjust the subplot layout, because the logit one may take more space # than usual, due to y-tick labels like "1 - 10^{-3}" plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25, wspace=0.35) plt.show()9. Cả hai đối số này là matplotlib.pyplot 0 Tuples.ax = plt.subplot() t = np.arange(0.0, 5.0, 0.01) s = np.cos(2*np.pi*t) line, = plt.plot(t, s, lw=2) plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05), ) plt.ylim(-2, 2) plt.show() Trong ví dụ cơ bản này, cả vị trí # Fixing random state for reproducibility np.random.seed(19680801) # make up some data in the open interval (0, 1) y = np.random.normal(loc=0.5, scale=0.4, size=1000) y = y[(y > 0) & (y < 1)] y.sort() x = np.arange(len(y)) # plot with various axes scales plt.figure() # linear plt.subplot(221) plt.plot(x, y) plt.yscale('linear') plt.title('linear') plt.grid(True) # log plt.subplot(222) plt.plot(x, y) plt.yscale('log') plt.title('log') plt.grid(True) # symmetric log plt.subplot(223) plt.plot(x, y - y.mean()) plt.yscale('symlog', linthresh=0.01) plt.title('symlog') plt.grid(True) # logit plt.subplot(224) plt.plot(x, y) plt.yscale('logit') plt.title('logit') plt.grid(True) # Adjust the subplot layout, because the logit one may take more space # than usual, due to y-tick labels like "1 - 10^{-3}" plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25, wspace=0.35) plt.show()8 (đầu mũi tên) và # Fixing random state for reproducibility np.random.seed(19680801) # make up some data in the open interval (0, 1) y = np.random.normal(loc=0.5, scale=0.4, size=1000) y = y[(y > 0) & (y < 1)] y.sort() x = np.arange(len(y)) # plot with various axes scales plt.figure() # linear plt.subplot(221) plt.plot(x, y) plt.yscale('linear') plt.title('linear') plt.grid(True) # log plt.subplot(222) plt.plot(x, y) plt.yscale('log') plt.title('log') plt.grid(True) # symmetric log plt.subplot(223) plt.plot(x, y - y.mean()) plt.yscale('symlog', linthresh=0.01) plt.title('symlog') plt.grid(True) # logit plt.subplot(224) plt.plot(x, y) plt.yscale('logit') plt.title('logit') plt.grid(True) # Adjust the subplot layout, because the logit one may take more space # than usual, due to y-tick labels like "1 - 10^{-3}" plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25, wspace=0.35) plt.show()9 (vị trí văn bản) đều nằm trong tọa độ dữ liệu. Có một loạt các hệ tọa độ khác mà người ta có thể chọn - xem chú thích cơ bản và chú thích nâng cao để biết chi tiết. Nhiều ví dụ có thể được tìm thấy trong các lô chú thích.Basic annotation and Advanced Annotations for details. More examples can be found in Annotating Plots. Logarit và các trục phi tuyến khác#
Một ví dụ về bốn ô với cùng một dữ liệu và các thang đo khác nhau cho trục y được hiển thị bên dưới. # Fixing random state for reproducibility np.random.seed(19680801) # make up some data in the open interval (0, 1) y = np.random.normal(loc=0.5, scale=0.4, size=1000) y = y[(y > 0) & (y < 1)] y.sort() x = np.arange(len(y)) # plot with various axes scales plt.figure() # linear plt.subplot(221) plt.plot(x, y) plt.yscale('linear') plt.title('linear') plt.grid(True) # log plt.subplot(222) plt.plot(x, y) plt.yscale('log') plt.title('log') plt.grid(True) # symmetric log plt.subplot(223) plt.plot(x, y - y.mean()) plt.yscale('symlog', linthresh=0.01) plt.title('symlog') plt.grid(True) # logit plt.subplot(224) plt.plot(x, y) plt.yscale('logit') plt.title('logit') plt.grid(True) # Adjust the subplot layout, because the logit one may take more space # than usual, due to y-tick labels like "1 - 10^{-3}" plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25, wspace=0.35) plt.show() Cũng có thể thêm quy mô của riêng bạn, xem Tổng thời gian chạy của tập lệnh: (0 phút 4.033 giây) ( 0 minutes 4.033 seconds) Phòng trưng bày được tạo ra bởi Sphinx-Gallery Làm thế nào để bạn vẽ các giá trị x trong python?Matplotlib với Python.. Tạo hai danh sách cho các điểm dữ liệu X và Y .. Nhận giá trị phạm vi Xticks .. Vẽ một dòng sử dụng phương thức Plot () với giá trị phạm vi Xtick và điểm dữ liệu y .. Thay thế Xticks bằng giá trị trục x bằng phương thức Xticks () .. Để hiển thị hình, sử dụng phương thức show () .. Làm thế nào để bạn vẽ đồ thị x và y bằng cách sử dụng matplotlib?Vẽ các điểm x và y Theo mặc định, hàm lô () vẽ một đường từ điểm này sang điểm khác.Hàm lấy các tham số để chỉ định các điểm trong sơ đồ.Tham số 1 là một mảng chứa các điểm trên trục x.Tham số 2 là một mảng chứa các điểm trên trục y.By default, the plot() function draws a line from point to point. The function takes parameters for specifying points in the diagram. Parameter 1 is an array containing the points on the x-axis. Parameter 2 is an array containing the points on the y-axis.
Làm thế nào để bạn vẽ dữ liệu trong Python?Dữ liệu cũng có thể được vẽ bằng cách gọi trực tiếp chức năng cốt truyện matplotlib ... Lệnh là plt.plot (x, y). Màu sắc và định dạng của các điểm đánh dấu cũng có thể được chỉ định là một đối số tùy chọn bổ sung, ví dụ: B- là một đường màu xanh lam, G-- là một đường nét đứt màu xanh lá cây .. Làm thế nào để bạn định dạng trục x và y trong python?Với pyplot, bạn có thể sử dụng các hàm xlabel () và ylabel () để đặt nhãn cho trục x và y.use the xlabel() and ylabel() functions to set a label for the x- and y-axis. |