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.

Giới thiệu với pyplot#

matplotlib.pyplot 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

plt.plot[[1, 2, 3, 4], [1, 4, 9, 16], 'ro']
plt.axis[[0, 6, 0, 20]]
plt.show[]
0 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 bằng nhãn, v.v.

Trong matplotlib.pyplot 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

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

  • 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

    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.
    plt.plot[[1, 2, 3, 4], [1, 4, 9, 16], 'ro']
    plt.axis[[0, 6, 0, 20]]
    plt.show[]
    
    3 Trả về danh sách các đố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[]
    
    3; ví dụ:
    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. 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
    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[]
    
    7 để có được phần tử đầu tiên của danh sách đó:

  • 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. 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.
    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 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

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.

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

[

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 |
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 |
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]
6]

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

[

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]
7 |
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 |
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 |
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 |
In [69]: lines = plt.plot[[1, 2, 3]]

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

Đường dây hoặc LW

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

đánh dấu

[

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

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

MarkeredGecolor hoặc MEC

Bất kỳ màu matplotlib

MarkeredgeWidth hoặc Mew

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

đánh dấu

Bất kỳ màu matplotlib

MarkeredgeWidth hoặc Mew

float

MarkerFaceColor hoặc MFC

đánh dấu hoặc MS

Markeever

[Không có | Số nguyên | [Startind, sải bước]]

người chọn

được sử dụng trong lựa chọn dòng tương tác

solid_capstyle

Pickradius

solid_joinstyle

[

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 |
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 |
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]
6]

Bán kính lựa chọn dòng dò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]
1 |
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 |
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]

biến đổi

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

có thể nhìn thấy

np.array

[Đúng | Sai]

np.array

xdata

ydata

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

Bài Viết Liên Quan

Chủ Đề