Hướng dẫn what is the meaning of plot in python? - ý nghĩa của cốt truyện trong python là gì?
Âm mưu x và y điểmHàm Show
Theo mặc định, hàm 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.x-axis. Tham số 2 là một mảng chứa các điểm trên trục y.y-axis. Nếu chúng ta cần vẽ một dòng từ (1, 3) đến (8, 10), chúng ta phải chuyển hai mảng [1, 8] và [3, 10] cho hàm cốt truyện. Thí dụVẽ một dòng trong sơ đồ từ vị trí (1, 3) đến vị trí (8, 10): Nhập matplotlib.pyplot dưới dạng PLT Nhập Numpy như NP xpoints = np.array ([1, 8]) ypoints = np.array ([3, 10]) plt.plot (xpoints, ypoints) plt.show () Result:
Hãy tự mình thử » Trục X là trục ngang.x-axis is the horizontal axis. Trục Y là trục dọc.y-axis is the vertical axis. Âm mưu mà không có dòngĐể chỉ vẽ các điểm đánh dấu, bạn có thể sử dụng tham số ký hiệu chuỗi phím tắt 'O', có nghĩa là 'nhẫn'. Thí dụVẽ một dòng trong sơ đồ từ vị trí (1, 3) đến vị trí (8, 10): Nhập matplotlib.pyplot dưới dạng PLT Nhập Numpy như NP xpoints = np.array ([1, 8]) ypoints = np.array ([3, 10]) plt.plot (xpoints, ypoints) plt.show () Result:
Hãy tự mình thử » Trục X là trục ngang. Trục Y là trục dọc.Âm mưu mà không có dòng Thí dụVẽ một dòng trong sơ đồ từ vị trí (1, 3) đến vị trí (8, 10): Nhập matplotlib.pyplot dưới dạng PLT Nhập Numpy như NP xpoints = np.array ([1, 8]) ypoints = np.array ([3, 10]) plt.plot (xpoints, ypoints) plt.show () Result:
Hãy tự mình thử » Trục X là trục ngang.Trục Y là trục dọc. Âm mưu mà không có dòng Thí dụVẽ một dòng trong sơ đồ từ vị trí (1, 3) đến vị trí (8, 10): Nhập matplotlib.pyplot dưới dạng PLT Nhập Numpy như NP xpoints = np.array ([1, 8]) ypoints = np.array ([3, 10]) plt.plot (xpoints, ypoints) plt.show () Result:
Hãy tự mình thử » Trục X là trục ngang.x-points in the example above is [0, 1, 2, 3, 4, 5]. 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 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, các trạng thái khác nhau được bảo tồn qua các cuộc gọi chức năng, do đó nó theo dõi những thứ như hình hiện tại và khu vực âm mưu, và các hàm âm mưu được hướng đến các trục hiện tại (xin lưu ý rằng "trục" ở đây và ở hầu hết các nơi trong tài liệu trong tài liệu Đề cập đến phần trục của một hình và không phải là thuật ngữ toán học nghiêm ngặt cho nhiều hơn một trục).part of a figure and not the strict mathematical term for more than one axis). 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 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()4. 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 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()5, 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ì các 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à 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()6. 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()5 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 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()5 để biết danh sách đầy đủ các kiểu và chuỗi định dạng dòng. 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()9 trong ví dụ trên lấy danh sách lines = plt.plot(x1, y1, x2, y2) # use keyword arguments plt.setp(lines, color='r', linewidth=2.0) # or MATLAB style string value pairs plt.setp(lines, 'color', 'r', 'linewidth', 2.0)0 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 lines = plt.plot(x1, y1, x2, y2) # use keyword arguments plt.setp(lines, color='r', linewidth=2.0) # or MATLAB style string value pairs plt.setp(lines, 'color', 'r', 'linewidth', 2.0)1 hoặc lines = plt.plot(x1, y1, x2, y2) # use keyword arguments plt.setp(lines, color='r', linewidth=2.0) # or MATLAB style string value pairs plt.setp(lines, 'color', 'r', 'linewidth', 2.0)2. Matplotlib cho phép bạn cung cấp một đối tượng như vậy với đối số từ khóa lines = plt.plot(x1, y1, x2, y2) # use keyword arguments plt.setp(lines, color='r', linewidth=2.0) # or MATLAB style string value pairs plt.setp(lines, 'color', 'r', 'linewidth', 2.0)3. 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 lines = plt.plot(x1, y1, x2, y2) # use keyword arguments plt.setp(lines, color='r', linewidth=2.0) # or MATLAB style string value pairs plt.setp(lines, 'color', 'r', 'linewidth', 2.0)4. Có một số cách để đặt thuộc tính dòng
Dưới đây là các thuộc tính lines = plt.plot(x1, y1, x2, y2) # use keyword arguments plt.setp(lines, color='r', linewidth=2.0) # or MATLAB style string value pairs plt.setp(lines, 'color', 'r', 'linewidth', 2.0)5 có sẵn.
Bất kỳ màu matplotlib In [69]: lines = plt.plot([1, 2, 3]) In [70]: plt.setp(lines) alpha: float animated: [True | False] antialiased or aa: [True | False] ...snip Làm việc với nhiều hình và trục#MATLAB và 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ó 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 import matplotlib.pyplot as plt plt.figure(1) # the first figure plt.subplot(211) # the first subplot in the first figure plt.plot([1, 2, 3]) plt.subplot(212) # the second subplot in the first figure plt.plot([4, 5, 6]) plt.figure(2) # a second figure plt.plot([4, 5, 6]) # creates a subplot() by default plt.figure(1) # first figure current; # subplot(212) still current plt.subplot(211) # make subplot(211) in the first figure # current plt.title('Easy as 1, 2, 3') # subplot 211 title9 trả về các trục 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) và 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 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()2). 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. 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() 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 ở đâ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()4 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()5 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()6 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()7 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()8. 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()5 là tùy chọn nếu 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. Vì vậy, 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 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()2. 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()3, 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()4 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.Axes Demo for an example of placing axes manually and Multiple subplots for an example with lots of subplots. 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()3 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: import matplotlib.pyplot as plt plt.figure(1) # the first figure plt.subplot(211) # the first subplot in the first figure plt.plot([1, 2, 3]) plt.subplot(212) # the second subplot in the first figure plt.plot([4, 5, 6]) plt.figure(2) # a second figure plt.plot([4, 5, 6]) # creates a subplot() by default plt.figure(1) # first figure current; # subplot(212) still current plt.subplot(211) # make subplot(211) in the first figure # current plt.title('Easy as 1, 2, 3') # subplot 211 title 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()6 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()7. 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ĩ)Artist tutorial) Nếu bạn đang tạo ra nhiều số liệu, bạn cần 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()8. 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()8 được gọi. Làm việc với 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()0 có thể được sử dụng để thêm văn bản vào một vị trí tùy ý 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, # 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()2 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()3 đượ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 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() Tất cả các chức năng # 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 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()5. 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 In [69]: lines = plt.plot([1, 2, 3]) In [70]: plt.setp(lines) alpha: float animated: [True | False] antialiased or aa: [True | False] ...snip0: 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.Text properties and layout. Sử dụng các biểu thức toán học trong văn bản#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:\(\sigma_i=15\) in the title, you can write a TeX expression surrounded by dollar signs: # 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 trước chuỗi tiêu đề rất quan trọng - nó biểu thị rằng chuỗi là một chuỗi thô và không coi dấu gạch chéo ngược khi Python thoát ra. Matplotlib có trình phân tích cú pháp biểu thức Tex tích hợp và công cụ bố cục, và vận chuyển phông chữ toán của riêng mình-để biết chi tiết, hãy xem viết các biểu thức toán học. Do đó, bạn có thể sử dụng văn bản toán học trên các nền tảng mà không cần cài đặt Tex. Đối với những người đã cài đặt latex và DVIPNG, bạn cũng có thể sử dụng latex để định dạng văn bản của mình và kết hợp đầu ra trực tiếp vào số liệu hiển thị của bạn hoặc đã lưu Postcript - xem kết xuất văn bản với latex.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. Chú thích văn bản#Việc sử dụng hàm # 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 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()9 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ố plot() 0 và vị trí của văn bản plot() 1. Cả hai đối số này là plot() 2 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í Logarit và các trục phi tuyến khá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()1 không chỉ hỗ trợ các thang đo trục tuyến tính mà còn cả thang đo logarit và logit. Điều này thường được sử dụng nếu dữ liệu kéo dài nhiều thứ tự cường độ. Thay đổi tỷ lệ của một trục rất dễ dàng:
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.101 giây) ( 0 minutes 4.101 seconds) Phòng trưng bày được tạo ra bởi Sphinx-Gallery Làm thế nào để cốt truyện () làm việc matplotlib?Nếu bạn cung cấp một danh sách hoặc mảng duy nhất cho lệnh lô (), matplotlib giả định rằng đó là một chuỗi các giá trị y và tự động tạo các giá trị x cho bạn. Vì các 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à [0,1,2,3].matplotlib assumes it is a sequence of y values, and automatically generates the x values for you. Since python ranges start with 0, the default x vector has the same length as y but starts with 0. Hence the x data are [0,1,2,3] .
Chú thích cốt truyện là gì?Đôi khi nó rất hữu ích cho người giải quyết vấn đề để chú thích các lô.Văn bản có thể được bao gồm trên một lô để chỉ ra một điểm quan tâm hoặc làm nổi bật một tính năng cụ thể của cốt truyện.Phần mã bên dưới xây dựng một biểu đồ dòng đơn giản và áp dụng ba chú thích (ba mũi tên có văn bản) trên cốt truyện.Text can be included on a plot to indicate a point of interest or highlight a specific feature of a plot. The code section below builds a simple line plot and applies three annotations (three arrows with text) on the plot.
Các lô trong Numpy là gì?Để vẽ đồ thị trong Python, chúng tôi sẽ sử dụng thư viện matplotlib.Matplotlib được sử dụng cùng với dữ liệu numpy để vẽ bất kỳ loại đồ thị nào.Từ matplotlib, chúng tôi sử dụng hàm cụ thể, tức là pyplot (), được sử dụng để vẽ dữ liệu hai chiều.Matplotlib library. Matplotlib is used along with NumPy data to plot any type of graph. From matplotlib we use the specific function i.e. pyplot(), which is used to plot two-dimensional data.
Các loại lô khác nhau trong Python là gì?Âm mưu khác.. 'Bar' hoặc 'Barh' cho các lô thanh .. 'Hist' cho biểu đồ .. 'Box' cho BoxPlot .. 'KDE' hoặc 'Mật độ' cho các sơ đồ mật độ .. 'Khu vực' cho các lô khu vực .. 'Phân tán' cho các lô phân tán .. 'Hexbin' cho các lô bin hình lục giác .. 'Pie' cho các lô bánh .. |