简体   繁体   English

显示一些数字而不停止matplotlib中的计算

[英]Showing a few figures without stopping calculations in matplotlib

Hi I would like to show a few figures in matplotlib without stopping calculations. 嗨,我想在不停止计算的情况下在matplotlib中显示一些数字。 I would like the figure to show up right after the calculations that concern it are finished for example: 我希望该图在有关计算完成后立即显示,例如:

import numpy as np
import pylab as py

x=np.linspace(0,50,51)
y=x
fig, axs = plt.subplots(1, 1)
cs = axs.plot(x, y)

now i want to show the plot without blocking the possibility to make some other calculations 现在我想显示该图而又不妨碍进行其他计算的可能性

plt.show(block=False)
plt.pause(5)

I create the second plot 我创建第二个情节

y1=2*x
fig1, axs1 = plt.subplots(1, 1)
cs1 = axs1.plot(x, y1)

plt.show()

This works however the first freezes (after 5 secound pause which I added) until I call plt.show() at the end. 但这有效,但是第一次冻结(在我添加5秒停顿之后),直到我最后调用plt.show()为止。 It is crucial that the first figure shows and works, then after calculations another figure is added to it. 至关重要的是,第一个数字必须显示并起作用,然后在计算后再向其添加另一个数字。

The following code should do what you want. 下面的代码应做您想要的。 I did this in an IPython Notebook. 我是在IPython Notebook中完成的。

from IPython import display
import matplotlib.pyplot as plt

def stream_plot(iterable, plotlife=10.):

        for I in iterable:
            display.clear_output(wait=True)
            output = do_calculations_on_i(I)
            plt.plot(output)
            display.display(plt.gca());
            time.sleep(plotlife); #how long to show the plot for

the wait=True will wait to clear the old plot until it has something new to plot, or any other output is printed. wait = True将等待清除旧图,直到有新图要打印,或者打印任何其他输出为止。

I put the sleep in there so I can observe each plot before it is wiped away. 我把睡眠放在那儿,这样我就可以观察到每一个阴谋在被抹去之前。 This was useful for having to observe distributions for several entities. 这对于必须观察多个实体的分布很有用。 You may or may not need it for what you want to do. 您可能需要也可能不需要它。

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

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