Python : When trying to plot a second figure, an empty tkinter window is displayed
-
-2
down vote
favorite
I have an interface with 4 tabs, in each tab, I have a button which when clicked generates a graph with matplotlib. We can display multiple plots on the same graph.Before, I have several problems such the graph window is frozen or cannot display multiple plots and I resolved it by this code :
fig = plt.figure(self.numFiguresPlot2_1) fig.canvas.set_window_title(self.DropDownListDisplaySector2_1.currentText()) x = np.arange(0, self.stockParam.nTotZones, 1) y = pd.to_numeric(matrix4["Price"]) print("Moyenne = ") print(y.mean(0)) z = pd.to_numeric(matrix4["Adjust"]*y)/100 + y # plot data if(firstPlot): price = plt.plot(x, y, label = ("Price")) shadowPrice = plt.plot(x, z, label = ("Price + adjust for iteration "+self.DropDownListDisplayIter2_1.currentText())) plt.ion() plt.legend() draw_thread = threading.Thread(target=plt.show) draw_thread.start() plt.ioff()
Now I have another problem. In fact, when I close the first figure and I try to plot a second figure, an empty tkinter window is displayed and the program crashes. I don't understand this problem.
Can you help me ?
Thanks in advance.
-
Hello,
I have another problem. When trying to integrate my code as a pluging in QGIS, I get this error :
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\OSGEO4~1\apps\Python27\lib\threading.py", line 808, in __bootstrap_inner
self.run()
File "C:\OSGEO4~1\apps\Python27\lib\threading.py", line 761, in run
self.__target(*self.__args, **self.__kwargs)
File "C:\OSGEO4~1\apps\Python27\lib\site-packages\matplotlib\pyplot.py", line 253, in show
return _show(*args, **kw)
File "C:\OSGEO4~1\apps\Python27\lib\site-packages\matplotlib\backend_bases.py", line 193, in call
self.mainloop()
File "C:\OSGEO4~1\apps\Python27\lib\site-packages\matplotlib\backends\backend_qt5.py", line 150, in mainloop
signal.signal(signal.SIGINT, signal.SIG_DFL)
ValueError: signal only works in main thread -
@EJWA said in Python : When trying to plot a second figure, an empty tkinter window is displayed:
ValueError: signal only works in main thread
This error message actually tells you what is wrong: signal() can only be used in main thread. You can find this information in Python documentation: https://docs.python.org/2/library/signal.html
"Some care must be taken if both signals and threads are used in the same program. The fundamental thing to remember in using signals and threads simultaneously is: always perform signal() operations in the main thread of execution." -
@EJWA To clarify: it's not about Qt signals, its about UNIX signals. I don't know your code, so I don't know where this signal() Python stuff is used. What exactly are you integrating and how? And when does this error occur?
Are you using any threads?
"File "C:\OSGEO4~1\apps\Python27\lib\site-packages\matplotlib\backends\backend_qt5.py", line 150, in mainloop
signal.signal(signal.SIGINT, signal.SIG_DFL)"
C:\OSGEO4~1\apps\Python27\lib\site-packages\matplotlib\backends\backend_qt5.py - is this what you're integrating? Its the file where the error occurs. -
Hello,
I am integrating my code as a plugin in QGIS using this method http://www.qgistutorials.com/fr/docs/building_a_python_plugin.html.
It consists on GUI interface which have a button when clicked generate graphs with matplotlib. Every time, we select an iteration in GUI, a plot will be drawn on the graph. So, the graph can have many plots.
Before, I have a problem with graph windows : I can't control it : I can't close it or save or zoom, so I use threading which helps me. Here is what I put in my code (function used when the button "generate graph" is clicked) :
fig = plt.figure(self.numFiguresPlot2_1) fig.canvas.set_window_title(self.DropDownListDisplaySector2_1.currentText()) x = np.arange(0, self.stockParam.nTotZones, 1) y = pd.to_numeric(matrix4["Price"]) print("Moyenne = ") print(y.mean(0)) z = pd.to_numeric(matrix4["Adjust"]*y)/100 + y # plot data if(firstPlot): price = plt.plot(x, y, label = ("Price")) shadowPrice = plt.plot(x, z, label = ("Price + adjust for iteration "+self.DropDownListDisplayIter2_1.currentText())) plt.ion() plt.legend() draw_thread = threading.Thread(target=plt.show) draw_thread.start() plt.ioff()
-
@EJWA In Qt you should never do any UI related stuff in other thread than GUI thread! This is not supported.
-
@jsulm So any alternatives to do that ? I have used other solutions like subprocess http://stackoverflow.com/questions/36675269/cannot-move-matplotlib-plot-window-and-exit-it-using-red-x-button and I don't get the result what I want. I am really confused to do that.
-
@EJWA "Before, I have a problem with graph windows : I can't control it : I can't close it or save or zoom" - you should investigate why. It should work without threads.
-
@jsulm When I investigate I found this answer ( http://stackoverflow.com/questions/36675269/cannot-move-matplotlib-plot-window-and-exit-it-using-red-x-button)
"his is because your matplotlib figure and your PyQt GUI are both running in the same main thread. Since they are in the main thread, only one of them has the CPU for itself"
If it is not correct, I must search for another reason.
-
@EJWA In the link you posted there is a workaround:
import subprocess ... # When you click the button in your PyQT GUI, # Create a new process: myMatProcess = subprocess.Popen(['python', 'myGraph.py'], shell=True)
-
Hello,
Finally, I found the solution .
First, in my script I put :
import matplotlib
matplotlib.use('Qt4Agg')then in function of plot I put :
plt.legend()
plt.show()
plt.draw()and it works for me correctly except there is a warning :
QCoreApplication::exec: The event loop is already running
-
import matplotlib
matplotlib.use('Qt4Agg')plt.legend()
plt.draw()
plt.show(block=False)