I'm new to Python and matplotlib. A simple script I wrote is crashing and I was able to reproduce the crash with the following code:
import matplotlib.pyplot as plt
plt.figure(1)
plt.figure(2)
#plt.show()
The error is python.exe has stopped working. If I uncomment the plt.show(), it still crashes depending on the order I close the plots (no crash if 2 is closed first, crash if 1 is closed first). I'm using Windows 7, Python 3.4, and I installed the individual modules from www.lfd.uci.edu/~gohlke/pythonlibs/. Do I have something configured incorrectly or a misunderstanding of how to use matplotlib?
You need to set the TkAgg backend explicitly. With the following code, the problem is resolved.
import matplotlib
matplotlib.use("TkAgg")
from matplotlib import pyplot as plt
Note that setting the TkAgg backend after importing pyplot does not work either; it crashes too. You need to set it before importing pyplot.
I was having this issue, I thought it was some line in my code causing the bug, but in fact the very act of importing matplotlib.pyplot was killing my program. I solved it by first running it in verbose mode:
python -v [programname].py
This shows the last action the importer does before crashing. For me, the last line of this was:
import 'PyQt5' # <_frozen_importlib_external.SourceFileLoader object at 0x000001F8EC9C0908>
This suggested to me that the dependent library PyQt5 was causing issues, so I ran pip install PyQt5, and magically everything started working.
This could be issue with python 3.x
I have tried with python 2.7 on my windows machine and it works perfectly fine!
You can either downgrade your python to 2.7 or if you feel its too late to do why dont you give it a try to call close()
Import matplotlib
matplotlib.use('wxAgg')
Import matplotlib.pyplot as plt
# your scripts
plt.close('all')
I had a similar issue in OSX when I updated to Python 3.4. IDLE was also crashing and there was a warning telling me the version was unstable.
I solved it by following the prompts and updating the version of Tcl/Tk (8.5.9) - http://www.python.org/download/mac/tcltk .
For macOS, just make sure that
~/.matplotlib/matplotlibrc contains:
backend: MacOSX
You don't need the other backends unless you specifically want them. Alternatively, perhaps you can do:
import matplotlib
matplotlib.use("MacOSX")
though I have not tested that.
Related
I am using PyCharm on my MacBook to code and now I wanted to make a simple plot. When I run the file via the usual 'Run' command (do not know what it is called), it nicely shows my plots, but when I run the file in the Python console (which I find more convenient because you can access your variables) it does not show anything. On the other hand, when I just type it in the Python console afterwards, it does work.
I have read some things about backends and other 'solutions' as I am apparently not the only one with this issue. Mine says macosx and gives the command: "Backend MacOSX is interactive backend. Turning interactive mode on." after running the file in the Python console. I tried changing the backend:
import matplotlib
# matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
but that does not work (no plot pops up). And yes, I use plt.show() after my plotting section :)
I also tried with 'QtAgg' but then I get: "ImportError: Failed to import any qt binding"
Now I am completely new to this backends stuff (I think it has to do with this), so I could really use some clear directions on how I can solve this issue.
Thanks in advance!
I am not sure we can solve this bug with some adjustment. I think you need to fresh start. I suggest you to start a new clean venv and install a new matplotlib there.
I am analyzing flow cytometry data using python 2.7.12 with the FlowCytometryTools0.4.5 module. After updating to High Sierra (macOS), plot-figure windows will no longer close after being called with the show() funtion. Figures are created with pylab 1.13.3 which was installed with matplotlib 2.1.2 alongside FlowCytometryTools 0.4.5 - all modules were installed using the pip install command in the terminal (ex: python2.7 pip install matplotlib). Python still runs in the background after you attempt to close the window, and scripts will continue to run as if the figure has closed, but the window remains on screen, making future work difficult as the window cannot be minimized or removed without force quitting the Launcher (space ship icon).
As mentioned before, FlowCytometryTools uses pylab, so I was interested if plotting through pyplot would yield a different result, but, alas, it was not to be. Even basic pyplot.plot(s), independent of the FlowCytometry module, had the same problem. This problem did not exist when running the same script(s) and versions of Python, matplotlib, FlowCytometryTools when running on OS X Sierra. Interestingly, this problem does not occur when plotting with pyplot using python 3.6 and matplotlib 2.1.2, but, unfortunately FlowCytometryTools runs only on Python 2.
Both pylab and matplotib use the backend 'MacOSX' - at lease that's what appears when I enter the command pylab.rcParams['backend'].
A simple set of code that produces the un-closable figure window is below:
import matplotlib.pyplot as plt
x = range(10)
y = range(10)
plt.plot(x,y)
plt.show()
After entering the show() command the figure appears, but the window cannot be closed without force quitting the Launcher. Again, I am using Python 2.7.12, matplotlib 2.1.2 and pylab 1.13.3. This was not a problem using the same code when running OS X Sierra, and is not a problem when the same code above is run with Python 3.6 and matplotlib 2.1.2.
If anyone has any ideas please let me know.
It seems like nobody had this problem before: When running the following code lines in DEBUG mode, my python crashes. I'm using python 2.7 with PyCharm 2016.3.
import matplotlib
matplotlib.use("qt4agg")
from matplotlib import pyplot as plt
If I run stepwise, it works. But if I set the breakpoint after this block, matplotlib.use() and import pyplot seem to get in each other's way. Does anyone know the problem?
So I was finally able to figure it out: The solution is to uncheck the "PyQt compatible" option under Python Debugger in the Project Settings of PyCharm. This option seems to cause the mentioned issues when using pyqt and debug mode.
As of late, I can't get my Matplotlib plots to show up. I have a very simple script:
import matplotlib.pyplot as plt
plt.plot([1,2,3])
but nothing ever shows up. If I include the line
plt.show()
then my Python process hangs.
In my ~/.config/matplotlib/matplotlibrc file I have
backend : MacOSX
interactive : True
I'm a little embarrassed to ask this question. I've been a Matplotlib user for many years and have never had this problem. I don't know where to begin to fix this problem. Help!
I'm using Matplotlib 2.0.0 with Python 3.5.2 from Anaconda.
The answer, as pointed out by #ImportanceOfBeingErnest is that the backend configuration for me wasn't working. I'm not sure if I need to install some additional libraries or not. I decided not to use the MacOSX backend and used the Qt5Agg backend. This worked just fine and I didn't have to install any new libraries.
I've just remove the line
interactive: True
enter code here
from my ~/matplotlib/matplotlibrc. It works fine with only backend: MacOSX using the plt.show() command.
I seem to have a problem that is in parts very similar to the one mentioned here:
Python with eclipse import problem
But unfortunatly just in parts otherwise that would have solved mine as well.
I use Eclipse SDK, Version: 3.7.0 with PyDev 101.
Furthermore I have installed
numpy-1.6.1rc1-win32-superpack-python2.6.exe
and
matplotlib-1.0.1.win32-py2.6.exe
as noted here:
http://matplotlib.sourceforge.net/users/installing.html
I have rebuild all the packages and looks the site-packages are listed.
(by the way as you see it is an Python version installed with ArcGIS )
If I test a script for instance a very simple one like:
import numpy
import matplotlib
import pylab as pl
I get the following error in Eclipse:
import matplotlib
import pylab as pl
from matplotlib.pylab import *
ImportError: No module named pylab
Even though the interpreter for Pydev is pointing to the appropriate version of python and matplotlib is installed properly in there (site-packages) it does not work in Eclipse. In iPython it works perfect.
What still needs to be done to get matplotlib work in Eclipse?
Thanks a lot!
Werner
pylab is in matplotlibs namespace, so this should work:
import matplotlib.pylab as pylab
I found that turning off interactive move and then calling show worked.
import matplotlib.pyplot as plt
#...your code...
plt.ioff()
plt.show()