I am running an inference algorithm and would like to show the likelihood function after each iteration. However, the plotting function is part of a package that i am importing. I've managed to cobble it together such that the plot is shown using the tkAgg backend in an external gui window, but is there any way to make it show as an inline plot? Here is what I'm using now:
Minimal Working Example
Jupyter Code
%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()
as can be seen below this should iteratively plot a series of dots updating the plot with one dot at a time. When I run it inline I only get the full plot after it has finished running.
Class Code
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()
Original Code
Notebook Code
import matplotlib
matplotlib.use('tkAgg')
import mypackage
class_instance = mypackage.myclass()
myclass.fit(n_iterations=100)
the plotting function is a bound method of the class and is called by the fit method.
Plotting Function Function
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()
Also, if I don't close and 're-initialise' the figure each time, the plot starts coming up empty. Any help would be much appreciated!
edit:
if I try using matplotlib inline instead of tkAgg backend I get the following warning message:
UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure
"matplotlib is currently using a non-GUI backend, "