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ểm

Hàm plot[] được sử dụng để vẽ các điểm [điểm đánh dấu] trong sơ đồ.

Theo mặc định, hàm plot[] vẽ một dò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.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
import numpy as np

xpoints = np.array [[1, 8]] ypoints = np.array [[3, 10]]
ypoints = np.array[[3, 10]]

plt.plot [xpoints, ypoints] plt.show []
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
import numpy as np

xpoints = np.array [[1, 8]] ypoints = np.array [[3, 10]]
ypoints = np.array[[3, 10]]

plt.plot [xpoints, ypoints] plt.show []
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
import numpy as np

xpoints = np.array [[1, 8]] ypoints = np.array [[3, 10]]
ypoints = np.array[[3, 8, 1, 10]]

plt.plot [xpoints, ypoints] plt.show []
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
import numpy as np

xpoints = np.array [[1, 8]] ypoints = np.array [[3, 10]]

plt.plot [xpoints, ypoints] plt.show []
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.

Giới thiệu với pyplot#

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 là một tập hợp các chức năng làm cho matplotlib hoạt động như MATLAB. Mỗ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[]
2 tạo ra một số thay đổi đối với một hình: ví dụ: tạo ra một hình, tạo ra một khu vực âm mưu trong một hình, vẽ một số dòng trong một khu vực âm mưu, trang trí cốt truyện với nhãn, v.v.

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

  • Sử dụng từ khóa đối số:

  • Sử dụng các phương thức setter của một thể hiện

    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.
    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 Trả về một danh sách các đối tượng
    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; ví dụ:
    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]
    
    8. Trong mã bên dưới, chúng tôi sẽ giả sử rằng chúng tôi chỉ có một dòng để danh sách được trả về có độ dài 1. Chúng tôi sử dụng việc giải nén Tuple 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]
    
    9 để có được phần tử đầu tiên của danh sách đó:

  • 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]
      ...snip
    
    0. Ví dụ dưới đây sử dụng chức năng kiểu MATLAB để đặt nhiều thuộc tính trên danh sách các 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]
      ...snip
    
    0 hoạt động minh bạch với danh sách các đối tượng hoặc một đối tượng. Bạn có thể sử dụng các đối số từ khóa Python hoặc các cặp giá trị kiểu MATLAB:

    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]
    

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.

Tài sản

Loại giá trị

Alpha

float

hoạt hình

[Đúng | Sai]

Antialiased hoặc AA

[Đúng | Sai]

clip_box

Antialiased hoặc AA

clip_on

[Đúng | Sai]

clip_path

Antialiased hoặc AA

một phiên bản matplotlib.transform.bbox

một thể hiện đường dẫn và một thể hiện biến đổi, một bản vá

màu hoặc c

Bất kỳ màu matplotlib

dash_capstyle

chứa

dash_joinstyle

chức năng kiểm tra đình đám

[

In [69]: lines = plt.plot[[1, 2, 3]]

In [70]: plt.setp[lines]
  alpha: float
  animated: [True | False]
  antialiased or aa: [True | False]
  ...snip
3 |
In [69]: lines = plt.plot[[1, 2, 3]]

In [70]: plt.setp[lines]
  alpha: float
  animated: [True | False]
  antialiased or aa: [True | False]
  ...snip
4 |
In [69]: lines = plt.plot[[1, 2, 3]]

In [70]: plt.setp[lines]
  alpha: float
  animated: [True | False]
  antialiased or aa: [True | False]
  ...snip
5]

[

In [69]: lines = plt.plot[[1, 2, 3]]

In [70]: plt.setp[lines]
  alpha: float
  animated: [True | False]
  antialiased or aa: [True | False]
  ...snip
6 |
In [69]: lines = plt.plot[[1, 2, 3]]

In [70]: plt.setp[lines]
  alpha: float
  animated: [True | False]
  antialiased or aa: [True | False]
  ...snip
4 |
In [69]: lines = plt.plot[[1, 2, 3]]

In [70]: plt.setp[lines]
  alpha: float
  animated: [True | False]
  antialiased or aa: [True | False]
  ...snip
8]

dấu gạch ngang

Trình tự mực bật/tắt theo điểm

dữ liệu

[NP.Array XDATA, NP.Array Ydata]

nhân vật

một phiên bản matplotlib.figure.figure

nhãn mác

bất kỳ chuỗi

LineStyle hoặc LS

[

In [69]: lines = plt.plot[[1, 2, 3]]

In [70]: plt.setp[lines]
  alpha: float
  animated: [True | False]
  antialiased or aa: [True | False]
  ...snip
9 |
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[]
0 |
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[]
1 |
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[]
2 |
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[]
3 | ...]

Đường dây hoặc LW

giá trị nổi trong các điểm

đánh dấu

một thể hiện đường dẫn và một thể hiện biến đổi, một bản vá

màu hoặc c

[

In [69]: lines = plt.plot[[1, 2, 3]]

In [70]: plt.setp[lines]
  alpha: float
  animated: [True | False]
  antialiased or aa: [True | False]
  ...snip
9 |
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[]
0 |
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[]
1 |
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[]
2 |
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[]
3 | ...]

Đường dây hoặc LW

một thể hiện đường dẫn và một thể hiện biến đổi, một bản vá

màu hoặc c

float

Bất kỳ màu matplotlib

chứa

chức năng kiểm tra đình đám

[

In [69]: lines = plt.plot[[1, 2, 3]]

In [70]: plt.setp[lines]
  alpha: float
  animated: [True | False]
  antialiased or aa: [True | False]
  ...snip
3 |
In [69]: lines = plt.plot[[1, 2, 3]]

In [70]: plt.setp[lines]
  alpha: float
  animated: [True | False]
  antialiased or aa: [True | False]
  ...snip
4 |
In [69]: lines = plt.plot[[1, 2, 3]]

In [70]: plt.setp[lines]
  alpha: float
  animated: [True | False]
  antialiased or aa: [True | False]
  ...snip
5]

[

In [69]: lines = plt.plot[[1, 2, 3]]

In [70]: plt.setp[lines]
  alpha: float
  animated: [True | False]
  antialiased or aa: [True | False]
  ...snip
6 |
In [69]: lines = plt.plot[[1, 2, 3]]

In [70]: plt.setp[lines]
  alpha: float
  animated: [True | False]
  antialiased or aa: [True | False]
  ...snip
4 |
In [69]: lines = plt.plot[[1, 2, 3]]

In [70]: plt.setp[lines]
  alpha: float
  animated: [True | False]
  antialiased or aa: [True | False]
  ...snip
8]

dấu gạch ngang

solid_capstyle

chứa

solid_joinstyle

chức năng kiểm tra đình đám

[

In [69]: lines = plt.plot[[1, 2, 3]]

In [70]: plt.setp[lines]
  alpha: float
  animated: [True | False]
  antialiased or aa: [True | False]
  ...snip
3 |
In [69]: lines = plt.plot[[1, 2, 3]]

In [70]: plt.setp[lines]
  alpha: float
  animated: [True | False]
  antialiased or aa: [True | False]
  ...snip
4 |
In [69]: lines = plt.plot[[1, 2, 3]]

In [70]: plt.setp[lines]
  alpha: float
  animated: [True | False]
  antialiased or aa: [True | False]
  ...snip
5]

[

In [69]: lines = plt.plot[[1, 2, 3]]

In [70]: plt.setp[lines]
  alpha: float
  animated: [True | False]
  antialiased or aa: [True | False]
  ...snip
6 |
In [69]: lines = plt.plot[[1, 2, 3]]

In [70]: plt.setp[lines]
  alpha: float
  animated: [True | False]
  antialiased or aa: [True | False]
  ...snip
4 |
In [69]: lines = plt.plot[[1, 2, 3]]

In [70]: plt.setp[lines]
  alpha: float
  animated: [True | False]
  antialiased or aa: [True | False]
  ...snip
8]

dấu gạch ngang

[Đúng | Sai]

Antialiased hoặc AA

np.array

một phiên bản matplotlib.transform.bbox

np.array

một thể hiện đường dẫn và một thể hiện biến đổi, một bản vá

màu hoặc c

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 title
9 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  0] & [y  0] & [y  0] & [y  0] & [y  0] & [y  0] & [y  0] & [y  0] & [y  0] & [y 

Bài Viết Liên Quan

Chủ Đề