2

I have code for "live" plotting with Matplotlib in Python, but it closes once it's done. I would like the plot to remain open.

Code below

import time
import matplotlib.pyplot as plt


    plt.ion()
    plt.show()

    for i in range(10):
        time.sleep(1)
        x = i ** 2
        plt.scatter(i, x)
        plt.draw()

1 Answer 1

2

Maybe you want something like this:

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

def make_data():
        for i in range(100):
            yield i, i*2

fig, ax = plt.subplots()
color = plt.cm.cubehelix(np.linspace(0.1,0.9,100))
plot, = ax.plot([], [],'o')
xdata, ydata = [], []
ax.set_ylim(0, 1)
ax.set_xlim(0, 1)

def run(data):
    x,y = data
    xdata.append(x)
    ydata.append(y)
    xmin, xmax = ax.get_xlim()
    ymin, ymax = ax.get_ylim()    
    if y > ymax:
        ax.set_xlim(xmin, 1+xmax)
        ax.set_ylim(ymin, 1+ymax)
        ax.figure.canvas.draw()

    plot.set_color(color[x])
    plot.set_data(xdata,ydata)
    return plot,

ani = animation.FuncAnimation(fig,run,make_data,blit=True,interval=10,repeat=False)
plt.show()

Maybe scatter would be better since it might allow for different colors of each circle.

Sign up to request clarification or add additional context in comments.

Comments

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.