By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,479 Members | 1,273 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,479 IT Pros & Developers. It's quick & easy.

Matplotlib show/draw works only once

P: 7
I am writing a GUI program in Tkinter which allows the user to enter in different parameters specifying an ellipse segment. Then, he/she presses a button and the ellipse segment is plotted, using matplotlib. Here is the code (inside a class, but logically stands alone) that does this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. aa = DataArray(self.file0)  #Retrieve data sets
  3. bb = DataArray(self.file1)  #Not used right now
  4. cc = DataArray(self.file2)  #Not used right now
  5. curr = DataArray(self.file3)#Not used right now
  6.  
  7. x = aa.x                 #Get the abcissa values from the initial data set
  8.  
  9. ideal = IdealEllipse(float(self.r.get()) , float(self.r.get()), float(self.r.get()),xall)
  10.  
  11. plt1 = plt.plot(x,ideal)
  12.  
  13. plt.draw()
  14.  
  15.  

It works the first time, but if the user closes the window and then tries to plot it again, only a blank graph shows up. I have tried using pyplot.show(), and I get the same result. Even if I insert pyplot.close() at the top, to close all figures, it still does not change this. Using the figure manager and interactive mode also do not change this result. I have searched through the documentation and message boards all over the place but cannot find an explanation for this. I have the most up-to-date version of matplotlib (for Python 2.6). Why is this happening?

Thanks in advance for any help
Feb 17 '12 #1
Share this Question
Share on Google+
3 Replies

Expert 100+
P: 626
What is self.r.get() and why do you call the function 3 times instead of calling it once and storing the value?
It works the first time, but if the user closes the window and then tries to plot it again, only a blank graph shows up
I'm guessing that r.get() doesn't exist once the window is closed but we can't tell from the code you posted. Storing r.get() in a variable would make the value persistent when the variable still exists after the window is closed.
Feb 17 '12 #2

P: 7
This is all inside the command function for a button. I don't close the GUI, I just close the plot window. And then when I try to bring it back up by pressing the button, I get a blank graph. The repeated r.get() calls is a mistake. The whole function (as I am currently testing it) is:

Expand|Select|Wrap|Line Numbers
  1. def ParOpt(self):
  2.         Ready = (self.r.get() != '') and (self.rp.get() != '') and (self.th.get() != '')
  3.         Ready = Ready and (self.Chg1.get()!= '') and (self.Chg2.get()!= '')
  4.         Ready = Ready and (self.Name0.get() != '') and (self.Name1.get() != '')
  5.         Ready = Ready and (self.Name2.get() != '') and (self.Name3.get() != '')
  6.  
  7.         if Ready == 1:
  8.  
  9.             plt.ion()
  10.             aa = DataArray(self.file0)  #Retrieve data sets
  11.             bb = DataArray(self.file1)
  12.             cc = DataArray(self.file2)
  13.             curr = DataArray(self.file3)            
  14.  
  15.             d1 = float32(self.Chg1.get())
  16.             d2 = float32(self.Chg2.get())
  17.  
  18.             xall = aa.x                 #Get the abcissa values from the initial data set
  19.  
  20.             ideal = IdealEllipse(float(self.r.get()) , float(self.rp.get()), float(self.th.get()),xall)
  21.  
  22.             Solution = Optimize(ideal,aa.s,bb.s,cc.s,d1,d2,curr.s)
  23.  
  24.             s1 = str( SigFig(sqrt(Solution.disp[0,0]),sqrt(Solution.disp[0,0])) )
  25.             s2 = str( SigFig(sqrt(Solution.disp[1,1]),sqrt(Solution.disp[1,1])) )
  26.  
  27.             # See if answer is already open
  28.             try:
  29.                 answer
  30.             except NameError:
  31.                 answer = None
  32.  
  33.             # Test whether variable is defined to be None
  34.             if answer is not None:
  35.                 answer.destroy()
  36.  
  37.             answer = Toplevel()
  38.             answer.title("Optimal Parameter Settings")
  39.  
  40.             aL = Label(answer, text = "Change Bender #1 by "+str(SigFig(Solution.sol[0],float(s1)))+" (+/-"+s1+")",width = 50)
  41.             aL.pack()
  42.  
  43.             aL = Label(answer, text = "Change Bender #2 by "+str(SigFig(Solution.sol[1],float(s2)))+" (+/-"+s2+")",width = 50)
  44.             aL.pack()
  45.  
  46.             nd = [self.r.get()+'\t'+self.rp.get()+'\t'+self.th.get()+'\t'+str(d1)+'\t'+str(d2)+'\t'+'END\n']
  47.             nd.append(self.Name0.get()+'\t\n')
  48.             nd.append(self.Name1.get()+'\t\n')
  49.             nd.append(self.Name2.get()+'\t\n')
  50.             nd.append(self.Name3.get()+'\t\n')
  51.  
  52.             newDefaults = open('C:\Documents and Settings\dmerthe\Desktop\Merthe Ellipse Adjust Test Files\defaultvalues.txt','w')
  53.             newDefaults.writelines(nd)
  54.  
  55.             newDefaults.close()
  56.  
  57.             DiffInit = curr.s - ideal - mean(curr.s - ideal)
  58.             DiffFin = Solution.pred - ideal - mean(Solution.pred - ideal)
  59.  
  60.             cf1 = (bb.s - aa.s)/d1
  61.             cf2 = (cc.s - bb.s)/d2
  62.  
  63.             plt1 = plt.plot(xall,DiffInit,'b',xall,DiffFin,'r--')
  64.  
  65.             xRange = max(xall) - min(xall)
  66.             ymin1 = min(DiffInit)
  67.             ymax1 = max(DiffInit)
  68.             ymin2 = min(DiffFin)
  69.             ymax2 = max(DiffFin)
  70.             ymax = max([ymax1,ymax2])
  71.             yrange = max([ymax1,ymax2]) - min([ymin1,ymin2])
  72.  
  73.             sInit = SigFig(std(DiffInit),std(DiffInit)*1.0E-2)
  74.             sFin = SigFig(std(DiffFin),std(DiffFin)*1.0E-2)
  75.  
  76.             txt1 = plt.text(min(xall)+0.1*xRange, ymax, 'Current State, $\sigma = $'+str(sInit*1.0E6)+ ' $\mu$rad', color = 'blue', fontsize = 14)
  77.             txt2 = plt.text(min(xall)+0.1*xRange, ymax - 0.1*yrange, 'Predicted Final State, $\sigma = $'+str(sFin*1.0E6)+ ' $\mu$rad', color = 'red', fontsize = 14)
  78.  
  79.             plt.draw()
  80.             plt.ioff()
  81.  
Feb 17 '12 #3

Expert 100+
P: 626
This code accepts a number and plots that number of ellipsis. When the plot window is closed it goes back to the Tkinter entry box to get another number, which you can then plot. Note that you can only do one thing at a time, so when the plot window is open the Tkinter window is not responsive.
Expand|Select|Wrap|Line Numbers
  1. try:
  2.     import Tkinter as tk     ## Python 2.x
  3. except ImportError:
  4.     import tkinter as tk     ## Python 3.x
  5.  
  6. from pylab import figure, show, rand
  7. from matplotlib.patches import Ellipse
  8.  
  9. class MatPlotTest():
  10.     def __init__(self):
  11.         self.num=1
  12.         self.root = tk.Tk()
  13.         self.create_entry()
  14.         self.root.mainloop()
  15.  
  16.     def create_entry(self):
  17.         tk.Label(self.root, text="number to create").grid()
  18.         self.entry=tk.Entry(self.root)
  19.         self.entry.grid(row=0, column=1)
  20.         self.entry.focus_set()
  21.  
  22.         tk.Button(self.root, text="draw ellipsesiess", command=self.plot_it, 
  23.                   bg="lightblue").grid(row=2, column=0)
  24.         tk.Button(self.root, text="Exit", command=self.root.quit, 
  25.                   bg="red").grid(row=2, column=1, sticky="WE")
  26.  
  27.     def plot_it(self):
  28.         self.num=int(self.entry.get())
  29.         ells = [Ellipse(xy=rand(2)*10, width=rand(), height=rand(), angle=rand()*360)
  30.                 for ctr in xrange(self.num)]
  31.  
  32.         fig = figure()
  33.         self.ax = fig.add_subplot(111, aspect='equal')
  34.         for e in ells:
  35.             self.ax.add_artist(e)
  36.             e.set_clip_box(self.ax.bbox)
  37.             e.set_alpha(rand())
  38.             e.set_facecolor(rand(3))
  39.  
  40.         self.ax.set_xlim(0, 10)
  41.         self.ax.set_ylim(0, 10)
  42.  
  43.         show()
  44.  
  45. MP=MatPlotTest() 
Feb 18 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.