Help required on matplotlib. Yes, I did not forget calling the pyplot.show().
$ ipython --pylab
import matplotlib.pyplot as p
p.plot(range(20), range(20))
It returns matplotlib.lines.Line2D at 0xade2b2c as the output.
p.show()
There is nothing to happen. No error message. No new window. Nothing. I install matplotlib by using pip and I didn't take any error messages.
Details:
I use,
Ubuntu
IPython v0.11
Python v2.6.6
matplotlib v1.0.1
If I set my backend to template in ~/.matplotlib/matplotlibrc,
then I can reproduce your symptoms:
~/.matplotlib/matplotlibrc:
# backend : GtkAgg
backend : template
Note that the file matplotlibrc may not be in directory ~/.matplotlib/. In this case, the following code shows where it is:
>>> import matplotlib
>>> matplotlib.matplotlib_fname()
In [1]: import matplotlib.pyplot as p
In [2]: p.plot(range(20),range(20))
Out[2]: [<matplotlib.lines.Line2D object at 0xa64932c>]
In [3]: p.show()
If you edit ~/.matplotlib/matplotlibrc and change the backend to something like GtkAgg, you should see a plot. You can list all the backends available on your machine with
import matplotlib.rcsetup as rcsetup
print(rcsetup.all_backends)
It should return a list like:
['GTK', 'GTKAgg', 'GTKCairo', 'FltkAgg', 'MacOSX', 'QtAgg', 'Qt4Agg',
'TkAgg', 'WX', 'WXAgg', 'CocoaAgg', 'agg', 'cairo', 'emf', 'gdk', 'pdf',
'ps', 'svg', 'template']
Reference:
Customizing matplotlib
I ran into the exact same problem on Ubuntu 12.04, because I installed matplotlib (within a virtualenv) using
pip install matplotlib
To make long story short, my advice is: don't try to install matplotlib using pip or by hand; let a real package manager (e.g. apt-get / synaptic) install it and all its dependencies for you.
Unfortunately, matplotlib's backends (alternative methods for actually rendering your plots) have all sorts of dependencies that pip will not deal with. Even worse, it fails silently; that is, pip install matplotlib appears to install matplotlib successfully. But when you try to use it (e.g. pyplot.show()), no plot window will appear. I tried all the different backends that people on the web suggest (Qt4Agg, GTK, etc.), and they all failed (i.e. when I tried to import matplotlib.pyplot, I get ImportError because it's trying to import some dependency that's missing). I then researched how to install those dependencies, but it just made me want to give up using pip (within virtualenv) as a viable installation solution for any package that has non-Python package dependencies.
The whole experience sent me crawling back to apt-get / synaptic (i.e. the Ubuntu package manager) to install software like matplotlib. That worked perfectly. Of course, that means you can only install into your system directories, no virtualenv goodness, and you are stuck with the versions that Ubuntu distributes, which may be way behind the current version...
%matplotlib inline
For me working with notebook, adding the above line before the plot works.
Just type:
plt.ion()
See https://www.youtube.com/watch?v=1zmV8lZsHF4 at 23:30 !
plt is used because of my import: import matplotlib.pyplot as plt
I'm using python2.7 on a mac with iTerm2.
For future reference,
I have encountered the same problem -- pylab was not showing under ipython. The problem was fixed by changing ipython's config file {ipython_config.py}. In the config file
c.InteractiveShellApp.pylab = 'auto'
I changed 'auto' to 'qt' and now I see graphs
What solved my problem was just using the below two lines in ipython notebook at the top
%matplotib inline
%pylab inline
And it worked. I'm using Ubuntu16.04 and ipython-5.1
Adding the following two lines before importing pylab seems to work for me
import matplotlib
matplotlib.use("gtk")
import sys
import pylab
import numpy as np
I had to install matplotlib from source to get this to work. The key instructions (from http://www.pyimagesearch.com/2015/08/24/resolved-matplotlib-figures-not-showing-up-or-displaying/) are:
$ workon plotting
$ pip uninstall matplotlib
$ git clone https://github.com/matplotlib/matplotlib.git
$ cd matplotlib
$ python setup.py install
By changing the backend, as #unutbu says, I just ran into loads more problems with all the different backends not working either.
Similar to #Rikki, I solved this problem by upgrading matplotlib with pip install matplotlib --upgrade. If you can't upgrade uninstalling and reinstalling may work.
pip uninstall matplotlib
pip install matplotlib
Be sure to have this startup script enabled :
( Preferences > Console > Advanced Options )
/usr/lib/python2.7/dist-packages/spyderlib/scientific_startup.py
If the standard PYTHONSTARTUP is enabled you won't have an interactive plot
For me the problem happens if I simply create an empty matplotlibrc file under ~/.matplotlib on macOS. Adding "backend: macosx" in it fixes the problem.
I think it is a bug: if backend is not specified in my matplotlibrc it should take the default value.
After running your code include:
import pylab as p
p.show()
if you are a nube from ruby, don't forget the parenthesis - show()
If you are working with yolov5 the fixes described here might not work as in my case. YOLOv5 developers have turned off the preview of the images using plt.show(), so most likely this will happen to you. To resolve make sure that your environment is correctly configured using requirements.txt file that comes with yolov5 and then use the workaround:
import torch
import matplotlib
model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # Load model first
matplotlib.use('TkAgg') # Change backend after loading model
as described here: https://github.com/ultralytics/yolov5/issues/2779
you can also try another backend from the ones mentioned above like 'Qt4Agg' or smth else.
I found that I needed window = Tk() and then window.mainloop()
It also shows the error when you don't call plt.plot() on anything. I got that error haha. I though I was calling it in a loop but right before the loop I overwrote my array to a blank array so it never entered the loop
For Ubuntu 12.04:
sudo apt-get install python-qt4
virtualenv .env --no-site-packages
source .env/bin/activate
easy_install -U distribute
ln -s /usr/lib/python2.7/dist-packages/PyQt4 .
ln -s /usr/lib/python2.7/dist-packages/sip.so .
pip install matplotlib
Related
I am trying to learn charting in Python and keep getting this message in Jupyter:
The kernel appears to have died. It will restart automatically.
My other basic programs are working, so it looks like using Matplotlib is causing this problem. Any thoughts on how to resolve this?
import matplotlib.pyplot as plt
plt.plot()
plt.show()
if you don't solve the problem after using method above, you can try this:
import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
add two lines of codes in your python code. This mothed is also useful to pycharm.
I try the solution of this video, based on this post, and it worked for me:
Run Anaconda Prompt as administrator
conda install --yes freetype=2.10.4
I did the following :
conda install --yes freetype=2.10.4
it works. Great!
I know this question has been asked many times before but I cannot find a solution that works for me. I'm running Python 3.5.3 under Blender 2.79 in Windows 10. As I require Blender 2.79, upgrading the Python version is not possible.
I've installed matplotlib and I can import it. However, when I try to import pyplot (e.g. import matplotlib.pyplot as plt), I get a crash to desktop with no error messages of any kind. This happens if I run Python externally or from inside Blender. The weird thing is that I was able to avoid this by changing backends to agg (matplotlib.use('agg')) and this is still working in a previous installation (so I know it's possible to get this to work !), but not in a separate, new installation of Blender. I've tried other backends but they make no difference.
The exact procedure I've tried is as follows :
Downloaded Blender 2.79 from a zip file (I'm using Windows 10) and unpacked it (call this directory /Blender/)
In /Blender/2.79/python/bin I run the command ./python -m ensurepip
In /Blender/2.79/python/scripts I run pip3 install --upgrade pip --user. This gives me pip3 version 20.2.4
In the same directory I run pip3 install --target="/Blender/2.79/python/lib/site-packages" matplotlib --upgrade. The "upgrade" switch is to prevent warnings that the "/bin" directory exists. Doesn't make any difference if I remove it, there's no existing installation of matplotlib. This gives me matplotlib version 3.0.3
Finally I start Python by /Blender/2.79/python/bin/python.exe, and do the above mentioned importmatplotlib.pyplot command which causes the crash.
I've tried this from a completely fresh installation of Blender, which comes with no existing external modules installed. Could there be some conflict with other Python modules elsewhere ? How would I go about diagnosing what's going on ?
Thanks for any ideas !
FIXED ! I remembered I found the solution months ago, but stupidly wrote down the answer in the wrong file...
The problem is that matplotlib is looking for a file that doesn't exist. In c:/users/me/.matplotlib (a hidden file), there's a file ""fontlist-vXXX.json", where XXX is the version number. This is set in line 951 of the file "font_manager.py", located in /python/lib/site-packages/matplotlib. In my case, the font_manager was looking for version 300 but the actual file was 310. Changing the version number in the font_manager.py made everything work correctly.
I'm using a remote machine for computation and data analysis. I don't have sudo access in this machine, for data analysis purposes I want to use python libraries such as vtk etc since pip is not installed in this machine, I set up a 'virtual-environment' and install VTK there,
I used the method explained here (https://stackoverflow.com/a/13958308/10755782). Then I could install vtk in the virtual environment using pip.
But now the problem is that I can't import matplotlib.pyplot, it is giving me the error
ImportError: No module named Tkinter.
This error is strange because matplotlib.pyplot works in this particular system outside my virtual-environment. There are a few workarounds to start using matplotlib.pyplot without tkinter such as this (https://stackoverflow.com/a/49988926/10755782), but this is not helpfull in my case as this machine is remote-accessed and I need 'tkinter' to view the graphs on my screen.
I tried installing 'tkinter' inside my virtual-env, but I could not ( since we can't install 'tkinter' with pip and I don't have sudo access).
As I could not find any immediate solution to this problem anywhere I tried to fix this myself and I did the following,
inside my virtual environment, the version of matplotlib was 2.2.4 and outside it was 1.2.0 ( which is working fine). So I downgraded the version of matplotlib inside my virtual environment to 1.2.0
pip install 'matplotlib==1.2.0' --force-reinstall
now I have the same version of matplotlib inside and outside the virtual env. But now, when I try import matplotlilb.pyplot as plt I'm getting the error
ImportError: /b/home/ipcms/rcheenik/Python_virt-env/python2.7/my_new_env/lib/python2.7/site-
packages/matplotlib/_cntr.so: undefined symbol: _intel_fast_memset
Is there any way to fix this ^^ error? any of these following will be able to help me.
without OR is there any way to install tkinter without sudo ? or inside the 'virtual-environment' ?
OR is there any alternatives to matplotlib.pyplot which works without 'tkinter' and still display graphs remotely? ( not favourable, as I have to rewrite the entire code )
Thanks in advance for the help.
I found this answer to a similar question however, which I believe would solve your issue.
https://stackoverflow.com/a/49988926/8775307
It imports matplotlib.pyplot without tkinter. I don't know if all the features are actually included though, so you'll have to test and let us know :).
You could always write to the administrator and request Tkinter - it's a widely used and useful package, so they might be willing to include it.
I am trying to display the x y z coordinate of an image in matplotlib. the example code work perfectly well on the global python installation: As I move the cursor the x,y,z values get updated instantaneously. However, when I run the example code on a python virtual environment, I would click on the images several times for the coordinate to show in the first place, then when I click on different positions it would update for some. After few clicks, the coordinates will no longer update.
I don't know how to debug this.
This is likely to be a problem with the macosx backend for matplotlib. Switch to using an alternative backend for matplotlib (e.g. use qt4 instead of 'macosx'). For details of how to switch backend and what exactly that means - see the docs here. Note that you might have to install the backend first - e.g. pyqt to use the qt4agg backend as I'm suggesting here.
In summary - the backend deals with the output from matplotlib and matplotlib can target different output formats. These can be gui display output formats (for instance wx, qt4 and so on), or file outputs (for instance pdf). These are known as interactive and non-interactive backends respectively.
To change backend either do
import matplotlib
matplotlib.use('qt4agg')
in code, or - if you want to change for every time you start matplotlib - edit your matplotlibrc file setting the backend attribute e.g.
backend: Qt4Agg
N.B. I was alerted by a comment that since posting this answer, matplotlib docs now refer to this issue and suggest a workaround, although the commenter noted that the solution offered in this answer (switch to Qt backend) worked for them where the official docs workaround was not possible for them.
What finally worked for me was to make a local matplotlibrc file containing the directive: backend: TkAgg.
# Within working directory where running python
vim matplotlibrc
# new file via vim, Nano, whatever
backend: TkAgg
This was useful:
import matplotlib
print matplotlib.rcParams['backend']
Other Notes:
I had also installed pyqt using Homebrew (brew install pyqt) and copied (could just move it) into $MyVirtEnv/lib/python2.7/site-packages/ from /usr/local/lib/python2.7/site-packages/PyQt4
But when I
import matplotlib
matplotlib.use('qt4Agg')
import librosa
"Matplotlib qt-based backends require an external PyQt4, PyQt5,\n"
ImportError: Matplotlib qt-based backends require an external PyQt4, PyQt5,
or PySide package to be installed, but it was not found.
Had also tried
pip install pyside
Successfully installed pyside-1.2.4
Same error
>>> import pyside
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pyside
Also got the following error at some point when trying to reimport matplotlib (i think).
from . import cache
ImportError: cannot import name cache
As pointed out in Matplotlib virtualenv FAQ, vext is the solution here.
It allows to use the system-wide pyqt5 for instance.
This is necessary until PyQt5 setup knows about virtualenvs:
pip3 install vext.pyqt5
Just add plt.show() at the end and the problem was solved for me.
I have an application in python2.7 which runs fine.
Because i wanted to run it on a different laptop,i installed the matplotlib1.1 .
The problem is that the application finishes but no plot window exists.
Problem with Gtk or Tk? I tried to do in the matplotlibrc file "backend:TkAgg" or "GtkAgg" but still the same.
I forgot to mention that the laptop runs linux.
----------EDIT-------------------
I tried the installation again and it shows a message:
You may need to define display for tk to work so that setup can
determine where your libraries are located.Tkinter present but header
files are not found.
Some part of code:
import matplotlib.pyplot as plt
import scipy as sc
from matplotlib.animation import ArtistAnimation
...........
fig=plt.gcf()
ani=ArtistAnimation(fig,result,interval=10,repeat=False)
plt.show()
Even with TkAgg, matplotlib does not necessarily show the plot until you explicitly tell it to.
If you use pylab, you can explicitly cause it to show with something like this:
import pylab
pylab.plot([1,2,1,3])
pylab.show()
The call to show() will return when you close the window.
You can make it automatically show when you do a plot by writing
interactive: true
in your matplolibrc file. There is a system-wide config file in mpl-data/matplotlibrc in the installed packate. You have a personal config in ~/.matplotlib/matplotlibrc
Some systems seem to have interactive=true by default, others do not.
I found the answer here.
Finally!
(I have done manual install because it wanted the version 1.1)
I tried this :
sudo apt-get install python-pip
sudo pip install matplotlib