简体   繁体   中英

Plot a graph point to point python

I wonder if there is some way to plot a waveform point to point at a certain rate through the matplotlib so that the graph appears slowly in the window. Or another method to graph appears at a certain speed in the window and not all the points simultaneously. I've been tried this but I can only plot a section of points at a time

import numpy as np
import matplotlib.pyplot as plt
import time

x = np.arange(0,5,0.001)
y = np.sin(2*np.pi*x)

ind_i = 0
ind_f = 300


while ind_f <= len(x):

    xtemp = x[ind_i:ind_f]
    ytemp = y[ind_i:ind_f]

    plt.hold(True)
    plt.plot(xtemp,ytemp)
    plt.show()
    time.sleep(1)    

    ind_i = ind_f
    ind_f = ind_f + 300

You can also do this with Matplotlib's FuncAnimation function. Adapting one of the matplotlib examples :

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

x = np.arange(0,5,0.001)
y = np.sin(2*np.pi*x)

def update_line(num, data, line):
    line.set_data(data[..., :num])
    return line,

fig = plt.figure()

data = np.vstack((x,y))
l, = plt.plot([], [], 'r-')
plt.xlim(0, 5)
plt.ylim(-1, 1)
line_ani = animation.FuncAnimation(fig, update_line, frames=1000,
                           fargs=(data, l), interval=20, blit=False)
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM