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ú

Show

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:

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?

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:

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?

[]

Đị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()

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?

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()

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?

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.

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?

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ụ:

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?

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()

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?

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()

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?

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.pyplot0 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()

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?

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#

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

plt.xscale('log')

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()

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?

Cũng có thể thêm quy mô của riêng bạn, xem matplotlib.pyplot4 để biết chi tiết.

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.