Cannot move Matplotlib plot window and exit it usi

2019-03-01 03:33发布

问题:

I'm running Python v3.5 and matplotlib v1.4.3 on Windows 10 Home. Up to recently, I write the python script using matplotlib with PyQt GUI. The 'plt.show()' code will be written in another module not __main__. When I run this code, Matplotlib figure cannot be moved and exit using red button X at the top of the right side of figure. Strangely, The chart is shown and It really does work well.

Why does this symptom happens? and How can I revise it?

回答1:

I stumbled on a similar problem. This 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.

I have tried to solve the problem by putting either the PyQT GUI or the matplotlib inside another thread. But that doesn't work. Both PyQT and matplotlib need to run in the main thread.

So here is a workaround. You can start the matplotlib figure from a newly created python shell:

import subprocess
...

    # When you click the button in your PyQT GUI,
    # Create a new process:
    myMatProcess = subprocess.Popen(['python', 'myGraph.py'], shell=True)

Now your PyQT GUI and the matplotlib figure you're drawing have their own python interpreter shell. And they can run smoothly without blocking each other.

If you have any further questions, don't hesitate to ask. I'd be happy to help you out.