use*_*916 5 plot matplotlib python-2.7 jupyter jupyter-notebook
我正在运行推理算法,并希望在每次迭代后显示似然函数。但是,绘图函数是我导入的包的一部分。我已经设法将其拼凑在一起,以便在外部 GUI 窗口中使用 tkAgg 后端显示该图,但是有什么方法可以使其显示为内联图吗?这是我现在使用的:
%matplotlib inline
#import matplotlib
#matplotlib.use('tkAgg')
import matplotlib.pyplot as plt
import sys
import numpy as np
sys.path.append('/path/to/file')
#______________________________________________________________
import testclass
a = testclass.test()
a.iterator()
Run Code Online (Sandbox Code Playgroud)
如下所示,这应该迭代地绘制一系列点,一次用一个点更新绘图。当我内联运行它时,我只有在它完成运行后才能得到完整的图。
import numpy as np
import matplotlib
matplotlib.use('tkAgg')
import matplotlib.pyplot as plt
import time
class test(object):
def __init__(self):
self.x = np.random.randint(0,50,size=5)
def iterator(self):
for i in range(5):
self.plotter(i)
st = time.time()
while (time.time()-st)<2:
pass
def plotter(self,i):
if not hasattr(self,'fig'):
self.fig = plt.figure()
else:
plt.close(self.fig)
self.fig = plt.figure()
#plt.ion()
self.fig.gca().plot(self.x[:i],'o')
self.fig.show()
Run Code Online (Sandbox Code Playgroud)
import matplotlib
matplotlib.use('tkAgg')
import mypackage
class_instance = mypackage.myclass()
myclass.fit(n_iterations=100)
Run Code Online (Sandbox Code Playgroud)
绘图函数是类的绑定方法,由 fit 方法调用。
def update_plot(self,r,LLst,kkk):
if not hasattr(self,'LL_fig'):
self.LL_fig = plt.figure()
else:
plt.close(self.LL_fig)
self.LL_fig = plt.figure()
#plt.ion()
#self.LL_fig.clf()
ax = self.LL_fig.gca()
ax.plot(LLst[1:],linestyle='-',marker='.')
#plt.gca().set_xlim([0,np.max([50,kkk])])
ax.set_xlim([0,np.max([50,kkk])])
ax.set_xlabel('EM iter')
ax.set_ylabel('$\mathcal{L}( \\theta )$')
seaborn.despine(trim=True,offset=15)
#plt.draw()
self.LL_fig.show()
#display.clear_output(wait=True)
#display.display(plt.gcf())
sys.stdout.write("\riter: %s || LL: %s || message: %s" %(kkk,np.round(LLst[-1],decimals=2), r['status']))
sys.stdout.flush()
Run Code Online (Sandbox Code Playgroud)
另外,如果我每次不关闭并“重新初始化”图形,绘图就会开始变得空洞。任何帮助将非常感激!
如果我尝试使用 matplotlib inline 而不是 tkAgg 后端,我会收到以下警告消息:
UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure
"matplotlib is currently using a non-GUI backend, "
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5455 次 |
| 最近记录: |