简体   繁体   English

停止关闭matplotlib图

[英]Stop matplotlib plot from closing

I have code for "live" plotting with Matplotlib in Python, but it closes once it's done. 我有使用Matplotlib在Python中进行“实时”绘图的代码,但是一旦完成,它就会关闭。 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()

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. 也许分散会更好,因为它可能允许每个圆圈使用不同的颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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