2

I have a code to draw a real-time scatter plot using matplotlib based on references. How do i get a real-time line graph instead?

import time
import matplotlib.pyplot as plt

plt.axis([0, 100, -10, 10])
plt.ion()
plt.show()

ts_start = time.time()

## perpetual loop code
    p_x = int(int(time.time())-int(ts_start))
    p_y = mynum # keeps getting generated in the loop code
    plt.scatter(p_x, p_y)
    plt.plot(p_x, p_y)
    plt.draw()
    time.sleep(0.05)

1 Answer 1

2

Here is how I did it

import time
import matplotlib.pyplot as plt

plt.axis([0, 100, -10, 10])
plt.ion()
plt.show()
ydata = [0]
line, = plt.plot(ydata)

ts_start = time.time()

## perpetual loop code
    p_x = int(int(time.time())-int(ts_start))
    p_y = mynum # keeps getting generated in the loop code
    ydata.append(p_y)
    line.set_xdata(np.arange(len(ydata)))
    line.set_ydata(ydata)
    plt.draw()
    time.sleep(0.05)
Sign up to request clarification or add additional context in comments.

1 Comment

You can also look into blitting which will help with performance (so you don't have to re-render all of the text every time).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.