Matplotlib có thể vẽ biểu đồ thời gian thực bằng Python không?

Để vẽ dữ liệu trong thời gian thực bằng Matplotlib hoặc tạo hoạt ảnh trong Matplotlib, chúng tôi liên tục cập nhật các biến sẽ được vẽ bằng cách lặp lại trong một vòng lặp và sau đó vẽ các giá trị được cập nhật. Để xem cốt truyện được cập nhật trong thời gian thực thông qua hình ảnh động, chúng tôi sử dụng nhiều phương pháp khác nhau như hàm

matplotlib.animation.FuncAnimation[fig, 
                                   func, 
                                   frames=None,
                                   init_func=None, 
                                   fargs=None,
                                   save_count=None, 
                                   *, 
                                   cache_frame_data=True,
                                   **kwargs]
2,
matplotlib.animation.FuncAnimation[fig, 
                                   func, 
                                   frames=None,
                                   init_func=None, 
                                   fargs=None,
                                   save_count=None, 
                                   *, 
                                   cache_frame_data=True,
                                   **kwargs]
3 cùng với
matplotlib.animation.FuncAnimation[fig, 
                                   func, 
                                   frames=None,
                                   init_func=None, 
                                   fargs=None,
                                   save_count=None, 
                                   *, 
                                   cache_frame_data=True,
                                   **kwargs]
4

matplotlib.animation.FuncAnimation[fig, 
                                   func, 
                                   frames=None,
                                   init_func=None, 
                                   fargs=None,
                                   save_count=None, 
                                   *, 
                                   cache_frame_data=True,
                                   **kwargs]
2 Chức năng

Chúng tôi có thể cập nhật cốt truyện trong thời gian thực bằng cách cập nhật các biến

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = []
y = []

figure, ax = plt.subplots[figsize=[4,3]]
line, = ax.plot[x, y]
plt.axis[[0, 4*np.pi, -1, 1]]

def func_animate[i]:
    x = np.linspace[0, 4*np.pi, 1000]
    y = np.sin[2 * [x - 0.1 * i]]
    
    line.set_data[x, y]
    
    return line,

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]

ani.save[r'animation.gif', fps=10]

plt.show[]
0 và
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = []
y = []

figure, ax = plt.subplots[figsize=[4,3]]
line, = ax.plot[x, y]
plt.axis[[0, 4*np.pi, -1, 1]]

def func_animate[i]:
    x = np.linspace[0, 4*np.pi, 1000]
    y = np.sin[2 * [x - 0.1 * i]]
    
    line.set_data[x, y]
    
    return line,

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]

ani.save[r'animation.gif', fps=10]

plt.show[]
1, sau đó hiển thị các cập nhật thông qua hình ảnh động bằng cách sử dụng
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = []
y = []

figure, ax = plt.subplots[figsize=[4,3]]
line, = ax.plot[x, y]
plt.axis[[0, 4*np.pi, -1, 1]]

def func_animate[i]:
    x = np.linspace[0, 4*np.pi, 1000]
    y = np.sin[2 * [x - 0.1 * i]]
    
    line.set_data[x, y]
    
    return line,

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]

ani.save[r'animation.gif', fps=10]

plt.show[]
2

cú pháp

matplotlib.animation.FuncAnimation[fig, 
                                   func, 
                                   frames=None,
                                   init_func=None, 
                                   fargs=None,
                                   save_count=None, 
                                   *, 
                                   cache_frame_data=True,
                                   **kwargs]

Mã số

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = []
y = []

figure, ax = plt.subplots[figsize=[4,3]]
line, = ax.plot[x, y]
plt.axis[[0, 4*np.pi, -1, 1]]

def func_animate[i]:
    x = np.linspace[0, 4*np.pi, 1000]
    y = np.sin[2 * [x - 0.1 * i]]
    
    line.set_data[x, y]
    
    return line,

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]

ani.save[r'animation.gif', fps=10]

plt.show[]

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = []
y = []

figure, ax = plt.subplots[figsize=[4,3]]
line, = ax.plot[x, y]
plt.axis[[0, 4*np.pi, -1, 1]]

def func_animate[i]:
    x = np.linspace[0, 4*np.pi, 1000]
    y = np.sin[2 * [x - 0.1 * i]]
    
    line.set_data[x, y]
    
    return line,

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]

ani.save[r'animation.gif', fps=10]

plt.show[]
3 là đối tượng hình có cốt truyện sẽ được cập nhật

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = []
y = []

figure, ax = plt.subplots[figsize=[4,3]]
line, = ax.plot[x, y]
plt.axis[[0, 4*np.pi, -1, 1]]

def func_animate[i]:
    x = np.linspace[0, 4*np.pi, 1000]
    y = np.sin[2 * [x - 0.1 * i]]
    
    line.set_data[x, y]
    
    return line,

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]

ani.save[r'animation.gif', fps=10]

plt.show[]
4 là chức năng được gọi ở mỗi khung. Đối số đầu tiên của nó đến từ giá trị tiếp theo
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = []
y = []

figure, ax = plt.subplots[figsize=[4,3]]
line, = ax.plot[x, y]
plt.axis[[0, 4*np.pi, -1, 1]]

def func_animate[i]:
    x = np.linspace[0, 4*np.pi, 1000]
    y = np.sin[2 * [x - 0.1 * i]]
    
    line.set_data[x, y]
    
    return line,

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]

ani.save[r'animation.gif', fps=10]

plt.show[]
5

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = []
y = []

figure, ax = plt.subplots[figsize=[4,3]]
line, = ax.plot[x, y]
plt.axis[[0, 4*np.pi, -1, 1]]

def func_animate[i]:
    x = np.linspace[0, 4*np.pi, 1000]
    y = np.sin[2 * [x - 0.1 * i]]
    
    line.set_data[x, y]
    
    return line,

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]

ani.save[r'animation.gif', fps=10]

plt.show[]
6 bằng với
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = []
y = []

figure, ax = plt.subplots[figsize=[4,3]]
line, = ax.plot[x, y]
plt.axis[[0, 4*np.pi, -1, 1]]

def func_animate[i]:
    x = np.linspace[0, 4*np.pi, 1000]
    y = np.sin[2 * [x - 0.1 * i]]
    
    line.set_data[x, y]
    
    return line,

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]

ani.save[r'animation.gif', fps=10]

plt.show[]
7. Các giá trị từ 0 đến 9 được chuyển đến
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = []
y = []

figure, ax = plt.subplots[figsize=[4,3]]
line, = ax.plot[x, y]
plt.axis[[0, 4*np.pi, -1, 1]]

def func_animate[i]:
    x = np.linspace[0, 4*np.pi, 1000]
    y = np.sin[2 * [x - 0.1 * i]]
    
    line.set_data[x, y]
    
    return line,

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]

ani.save[r'animation.gif', fps=10]

plt.show[]
4 ở mỗi khung. Chúng ta cũng có thể gán một interalbe cho
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = []
y = []

figure, ax = plt.subplots[figsize=[4,3]]
line, = ax.plot[x, y]
plt.axis[[0, 4*np.pi, -1, 1]]

def func_animate[i]:
    x = np.linspace[0, 4*np.pi, 1000]
    y = np.sin[2 * [x - 0.1 * i]]
    
    line.set_data[x, y]
    
    return line,

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]

ani.save[r'animation.gif', fps=10]

plt.show[]
5, giống như một danh sách
ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]
0

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]
1 là độ trễ giữa các khung trong đơn vị của
ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]
2

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = []
y = []

figure, ax = plt.subplots[figsize=[4,3]]
line, = ax.plot[x, y]
plt.axis[[0, 4*np.pi, -1, 1]]

def func_animate[i]:
    x = np.linspace[0, 4*np.pi, 1000]
    y = np.sin[2 * [x - 0.1 * i]]
    
    line.set_data[x, y]
    
    return line,

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]

ani.save[r'animation.gif', fps=10]

plt.show[]
6

Chúng ta có thể lưu hoạt ảnh vào

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]
3 hoặc
ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]
4 với các tham số như
ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]
5 và
ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]
6

matplotlib.animation.FuncAnimation[fig, 
                                   func, 
                                   frames=None,
                                   init_func=None, 
                                   fargs=None,
                                   save_count=None, 
                                   *, 
                                   cache_frame_data=True,
                                   **kwargs]
3 Cùng với
matplotlib.animation.FuncAnimation[fig, 
                                   func, 
                                   frames=None,
                                   init_func=None, 
                                   fargs=None,
                                   save_count=None, 
                                   *, 
                                   cache_frame_data=True,
                                   **kwargs]
4

Chúng tôi có thể cập nhật cốt truyện theo thời gian thực bằng cách cập nhật các biến

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = []
y = []

figure, ax = plt.subplots[figsize=[4,3]]
line, = ax.plot[x, y]
plt.axis[[0, 4*np.pi, -1, 1]]

def func_animate[i]:
    x = np.linspace[0, 4*np.pi, 1000]
    y = np.sin[2 * [x - 0.1 * i]]
    
    line.set_data[x, y]
    
    return line,

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]

ani.save[r'animation.gif', fps=10]

plt.show[]
0 và
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = []
y = []

figure, ax = plt.subplots[figsize=[4,3]]
line, = ax.plot[x, y]
plt.axis[[0, 4*np.pi, -1, 1]]

def func_animate[i]:
    x = np.linspace[0, 4*np.pi, 1000]
    y = np.sin[2 * [x - 0.1 * i]]
    
    line.set_data[x, y]
    
    return line,

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]

ani.save[r'animation.gif', fps=10]

plt.show[]
1 với
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = []
y = []

figure, ax = plt.subplots[figsize=[4,3]]
line, = ax.plot[x, y]
plt.axis[[0, 4*np.pi, -1, 1]]

def func_animate[i]:
    x = np.linspace[0, 4*np.pi, 1000]
    y = np.sin[2 * [x - 0.1 * i]]
    
    line.set_data[x, y]
    
    return line,

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]

ani.save[r'animation.gif', fps=10]

plt.show[]
61 và
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = []
y = []

figure, ax = plt.subplots[figsize=[4,3]]
line, = ax.plot[x, y]
plt.axis[[0, 4*np.pi, -1, 1]]

def func_animate[i]:
    x = np.linspace[0, 4*np.pi, 1000]
    y = np.sin[2 * [x - 0.1 * i]]
    
    line.set_data[x, y]
    
    return line,

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]

ani.save[r'animation.gif', fps=10]

plt.show[]
62, sau đó hiển thị các cập nhật thông qua hoạt ảnh bằng cách sử dụng
matplotlib.animation.FuncAnimation[fig, 
                                   func, 
                                   frames=None,
                                   init_func=None, 
                                   fargs=None,
                                   save_count=None, 
                                   *, 
                                   cache_frame_data=True,
                                   **kwargs]
3, đây là một phương pháp dựa trên JavaScript

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = []
y = []

figure, ax = plt.subplots[figsize=[4,3]]
line, = ax.plot[x, y]
plt.axis[[0, 4*np.pi, -1, 1]]

def func_animate[i]:
    x = np.linspace[0, 4*np.pi, 1000]
    y = np.sin[2 * [x - 0.1 * i]]
    
    line.set_data[x, y]
    
    return line,

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]

ani.save[r'animation.gif', fps=10]

plt.show[]
5

Tại đây, các giá trị của

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = []
y = []

figure, ax = plt.subplots[figsize=[4,3]]
line, = ax.plot[x, y]
plt.axis[[0, 4*np.pi, -1, 1]]

def func_animate[i]:
    x = np.linspace[0, 4*np.pi, 1000]
    y = np.sin[2 * [x - 0.1 * i]]
    
    line.set_data[x, y]
    
    return line,

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]

ani.save[r'animation.gif', fps=10]

plt.show[]
0 và
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = []
y = []

figure, ax = plt.subplots[figsize=[4,3]]
line, = ax.plot[x, y]
plt.axis[[0, 4*np.pi, -1, 1]]

def func_animate[i]:
    x = np.linspace[0, 4*np.pi, 1000]
    y = np.sin[2 * [x - 0.1 * i]]
    
    line.set_data[x, y]
    
    return line,

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]

ani.save[r'animation.gif', fps=10]

plt.show[]
1 được cập nhật liên tục và cốt truyện cũng được cập nhật theo thời gian thực

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = []
y = []

figure, ax = plt.subplots[figsize=[4,3]]
line, = ax.plot[x, y]
plt.axis[[0, 4*np.pi, -1, 1]]

def func_animate[i]:
    x = np.linspace[0, 4*np.pi, 1000]
    y = np.sin[2 * [x - 0.1 * i]]
    
    line.set_data[x, y]
    
    return line,

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]

ani.save[r'animation.gif', fps=10]

plt.show[]
66 bật chế độ tương tác. Cốt truyện sẽ không được cập nhật nếu nó không được gọi

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = []
y = []

figure, ax = plt.subplots[figsize=[4,3]]
line, = ax.plot[x, y]
plt.axis[[0, 4*np.pi, -1, 1]]

def func_animate[i]:
    x = np.linspace[0, 4*np.pi, 1000]
    y = np.sin[2 * [x - 0.1 * i]]
    
    line.set_data[x, y]
    
    return line,

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]

ani.save[r'animation.gif', fps=10]

plt.show[]
67 là phương pháp dựa trên JavaScript để xóa các số liệu trên mỗi lần lặp lại để các số liệu liên tiếp không trùng nhau

Tuy nhiên, để tạo phân tán thời gian thực, chúng ta chỉ cần cập nhật các giá trị của

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = []
y = []

figure, ax = plt.subplots[figsize=[4,3]]
line, = ax.plot[x, y]
plt.axis[[0, 4*np.pi, -1, 1]]

def func_animate[i]:
    x = np.linspace[0, 4*np.pi, 1000]
    y = np.sin[2 * [x - 0.1 * i]]
    
    line.set_data[x, y]
    
    return line,

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]

ani.save[r'animation.gif', fps=10]

plt.show[]
0 và
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = []
y = []

figure, ax = plt.subplots[figsize=[4,3]]
line, = ax.plot[x, y]
plt.axis[[0, 4*np.pi, -1, 1]]

def func_animate[i]:
    x = np.linspace[0, 4*np.pi, 1000]
    y = np.sin[2 * [x - 0.1 * i]]
    
    line.set_data[x, y]
    
    return line,

ani = FuncAnimation[figure,
                    func_animate,
                    frames=10,
                    interval=50]

ani.save[r'animation.gif', fps=10]

plt.show[]
1 và thêm các điểm phân tán trong mỗi lần lặp. Trong trường hợp này, chúng ta không cần xóa mọi hình vì biểu đồ phân tán thường biểu thị một điểm riêng biệt trên mặt phẳng và các điểm có rất ít cơ hội chồng lên nhau

Loại biểu đồ nào có thể được vẽ bằng matplotlib?

Các loại lô Matplotlib .
Lô đất
Lô đất
Âm mưu phân tán
Lô bánh
Diện Tích
Sơ đồ biểu đồ

Ba loại ô được cung cấp bởi matplotlib là gì?

Các loại ô .
cốt truyện [x, y]
tán xạ[x, y]
thanh[x, chiều cao]
gốc[x, y]
bước [x, y]
fill_between[x, y1, y2]
biểu đồ ngăn xếp [x, y]

Matplotlib có thể được sử dụng để vẽ đồ thị 3D không?

Để vẽ các hình 3D bằng matplotlib, chúng tôi cần nhập bộ công cụ mplot3d, bộ công cụ này bổ sung khả năng vẽ đồ thị 3D đơn giản cho matplotlib . Khi chúng tôi đã nhập bộ công cụ mplot3d, chúng tôi có thể tạo các trục 3D và thêm dữ liệu vào các trục. Trước tiên hãy tạo một trục 3D. Cái rìu = plt.

Matplotlib có được sử dụng để trực quan hóa dữ liệu không?

Matplotlib là thư viện trực quan hóa dữ liệu đa nền tảng được xây dựng trên mảng NumPy và được thiết kế để hoạt động với ngăn xếp SciPy rộng hơn. Nó được John Hunter hình thành vào năm 2002, ban đầu là một bản vá cho IPython để cho phép vẽ sơ đồ kiểu MATLAB tương tác thông qua gnuplot từ dòng lệnh IPython.

Chủ Đề