python 动态曲线demo

python 动态曲线demo_第1张图片

import numpy as np
import matplotlib.pyplot as plt

# Turn on interactive mode
plt.ion()

# Initialize the plot
fig, ax = plt.subplots()
line, = ax.plot([], [])

# Set the x and y limits of the plot
ax.set_xlim(0, 10)
ax.set_ylim(0, 1)
x=[]
y=[]
i=1
# Start a loop to update the plot with new data
for i in range(100):
    # Generate some random data
    i = i+1
    x.append(i)
    ax.set_xlim(0, i)
    ye = np.random.rand(1)
    y.append(ye)
    
    # Update the plot with the new data
    line.set_data(x, y)
    ax.set_title('Iteration {}'.format(i))
    
    # Redraw the plot
    plt.draw()
    plt.pause(0.1)

 

你可能感兴趣的:(python,matplotlib,开发语言,动态曲线)