Python : When trying to plot a second figure, an empty tkinter window is displayed
-
@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."Hello @jsulm and thanks for your reply.
I have never used signals and I have no idea how to use it in my case. Could you give me some examples ?
Thanks in advance.
-
Hello @jsulm and thanks for your reply.
I have never used signals and I have no idea how to use it in my case. Could you give me some examples ?
Thanks in advance.
@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()
-
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.
-
@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.
-
@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.
-
@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.
-
@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)