5

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, "

1 Answer 1

2

Use the cell magic %matplotlib inline (if you aren't familiar with cell magics, just place it in a line on its on in one of your cells)

4
  • I've tried this, this just leads to the warning message: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure "matplotlib is currently using a non-GUI backend, " included this in main question, thx :] Commented Feb 22, 2017 at 12:09
  • then its just blank, with no warning. To clarify, with the fig.show there is also no plot Commented Feb 22, 2017 at 12:10
  • hey, thanks for checking this. Does the plot show while it is running, or just after it has run completely? I have the same versions and everything is up to date, so also stumped Commented Feb 22, 2017 at 13:55
  • ah I see, the goal was to show the plot iteratively adding points, so it should be showing one point at a time, not just plot at the end. Mine does plot at the end Commented Feb 22, 2017 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.