matplotlib pyplot error in jupyter notebook - python

Within Jupyter notebook I can import matplotlib but not the pyplot module:
import matplotlib % works
import matplotlib.pyplot as plt % The kernel appears to have died. It will restart automatically.
An alternative test also fails:
import matplotlib
matplotlib.pyplot % AttributeError: module 'matplotlib' has no attribute 'pyplot'
However, I can import the pyplot module from the conda command prompt with no errors:
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.show()
I got the same results in both the "base" environment and a virtual environment that I created.
Does anyone know what the problem is? I've tried uninstalling and reinstalling the maplotlib package, as well as Jupyter notebook, and conda itself.

This imports matplotlib module/library and then you are trying looks for an attribute or variable defined in matplotlib library named as pyplot; which does not exist. pyplot is just an interface for you to call other relevant interactive state-based functions to plot.
import matplotlib
matplotlib.pyplot
In my opinion you should stick to naming/importing convention defined in documentations of libraries for coherent code.
import matplotlib.pyplot as plt
This is all you need.

import matplotlib.pyplot as plt % The kernel appears to have died. It will restart automatically.
This means you have to restart your juypter there will be a cmd window that you could have closed I recommend you to restart the juypter and then
import matplotlib.pyplot as plt
if it does not work
open juypter cmd from search bar and write pip install matplotlib after the task is done in jupyter cmd reopen juypter and than use
import matplotlib.pyplot as plt

Not that I understand what the problem was, but what worked for me is uninstalling matplotlib with conda, and re-installing it with pip. Oftentimes, the conda versions are not the latest, and there is probably a problem with the specific version. As of matplotlib 3.6.1 pyplot does work in jupyter (I tried with base), though reinstallation of kiwisolver might be needed as well.

Related

How do I make matplotlib work in AWS EMR Jupyter notebook?

This is very close to this question, but I have added a few details specific to my question:
Matplotlib Plotting using AWS-EMR jupyter notebook
I would like to find a way to use matplotlib inside my Jupyter notebook. Here is the code-snippet in error, it's fairly simple:
notebook
import matplotlib
matplotlib.use("agg")
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.show()
I chose this snippet because this line alone fails as it tries to use TKinter (which is not installed on an AWS EMR cluster):
import matplotlib.pyplot as plt
When I run the full notebook snippet, the result is no runtime error but also nothing happens (no graph is shown.) My understanding on one way this can work is by adding either of the following snips:
pyspark magic notation
%matplotlib inline
results
unknown magic command 'matplotlib'
UnknownMagic: unknown magic command 'matplotlib'
IPython explicit magic call
from IPython import get_ipython
get_ipython().run_line_magic('matplotlib', 'inline')
results
'NoneType' object has no attribute 'run_line_magic'
Traceback (most recent call last):
AttributeError: 'NoneType' object has no attribute 'run_line_magic'
to my notebook which invokes a spark magic command which inlines matplotlib plots (at least that's my interpretation.) I have tried both of these after using a bootstrap action:
EMR bootstrap
sudo pip install matplotlib
sudo pip install ipython
Even with these added, I still get an error that there is no magic for matplotlib. So my question is definitely:
Question
How do I make matplotlib work in an AWS EMR Jupyter notebook?
(Or how do I view graphs and plot images in AWS EMR Jupyter notebook?)
The answer by #00schneider actually works.
import matplotlib.pyplot as plt
# plot data here
plt.show()
after
plt.show()
re-run the magic cell that contains the below, and you will see a plot on your AWS EMR Jupyter PySpark notebook
%matplot plt
As you mentioned, matplotlib is not installed on the EMR cluster, therefore such error will occur:
However, it is actually available in the managed Jupyter notebook instance (the docker container). Using the %%local magic will allow you to run the cell locally:
Import matplotlib as
import matplotlib.pyplot as plt
and use the magic command %matplot plt instead as shown in the tutorial here: https://aws.amazon.com/de/blogs/big-data/install-python-libraries-on-a-running-cluster-with-emr-notebooks/
The following should work:
import matplotlib
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
Run the entire script in one cell
To plot something in AWS EMR notebooks, you simply need to use %matplot plt. You can see this documented about midway down this page from AWS.
For example, if I wanted to make a quick plot:
import matplotlib.pyplot as plt
plt.clf() #clears previous plot in EMR memory
plt.plot([1,2,3,4])
plt.show()
%matplot plt
%matplot plt
after plt.show() function works for me.
Try below code. FYI we have matplotlib 3.1.1 installed in Python3.6 on emr-5.26.0 and i used PySpark Kernel.
Make sure that "%matplotlib inline" is first line in cell
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.show()

ImportError: no module named miniconda

I've installed miniconda on my mac following the instructions from the python website. However, when I write any new script and try and import miniconda, matplotlib or Pandas, I get the error above.
My script so far
from miniconda import *
from matplotlib import pyplot as plt
import pandas as pd
import seaborn as sns
CORRECTED
my code is now
from matplotlib import pyplot as plt
import pandas as pd
import seaborn as sns
I'm now getting the same import error for pandas and seaborn.
UPDATED:
Turns out the corrected code above works and the issue was with Python runner. The code runs when executed through the terminal. Probably an issue with permissions!
Miniconda is not a module indeed it is a variant of Anaconda python distribution, hence you could not import it. You could get further info about miniconda from the link below.
https://conda.io/miniconda.html

IPython with and Without Notebook Differences

One of the most important improvisations of Python that are my favorites are IPython and IPython Notebook.
I was watching and repeating what's shown in this video and found some issues.
As specified in the video, I use ipython --pylab to launch IPython.
And I use ipython notebook --pylab to launch IPython Notebook.
Issues: scatter() would not work in IPython NoteBook (I get a NameError) but works fine in IPython.
Same is the case with the function rand(). I guess pylab is loaded along with matplotlib, scipy, numpy, random and other essential libraries.
Please tell me if I am wrong. By the way, both my IPython and IPython NoteBook load from my Anaconda Dist., if that means anything.
Also any resource where I can know what all is loaded when I use --pylab would help.
Thanks.
This is what the pylab flag does:
import numpy
import matplotlib
from matplotlib import pylab, mlab, pyplot
np = numpy
plt = pyplot
from IPython.core.pylabtools import figsize, getfigs
from pylab import *
from numpy import *
That said, it is recommended that you launch the notebook without the flag (just ipython notebook) and then run:
%matplotlib inline
For more details see No Pylab Thanks.
Regarding your scatter problem, you should try the following:
%matplotlib inline
import matplotlib.pyplot as plt
plt.scatter([1,2], [1,2])
Here's another example of why you shouldn't use %pylab inline:
Before %pylab inline: bool(all(i for i in range(3))) => False
After %pylab inline: bool(all(i for i in range(3))) => True
The %pylab inline statement imports numpy.all, which has a different behavior. See help(all) before and after %pylab inline to see. Also, try print(', '.join(sorted(globals().keys()))) before & after to see the vast number of things that get imported.
As mentioned by others, %matplotlib inline avoids this and the subsequent subtle / hard-to-find issues that it causes.

matplotlib.pyplot has no attribute 'style'

I am trying to set a style in matplotlib as per tutorial http://matplotlib.org/users/style_sheets.html
import matplotlib.pyplot as plt
plt.style.use('ggplot')
but what I get in return is:
AttributeError: 'module' object has no attribute 'style'
My matplotlib version is 1.1.1 (and I'm on a Mac running Mavericks). Where are the styles in this version?
thanks!
My matplotlib version is 1.1.1
There's your problem. The style package was added in version 1.4. You should update your version.
In ipython notebook I also had to include %matplotlib inline, otherwise I would still get the same error.
%matplotlib inline
import matplotlib
matplotlib.style.use('ggplot')
For people using matplotlib 2.x and discovering this question can use the following snippet:
import matplotlib.style
import matplotlib as mpl
mpl.style.use('classic') # any style.
This is described in the documentation here. Note the import matplotlib.style is important.
I tried all solutions listed on StackOverflow but somehow none of these work for me. Finally I found a method which worked. Following are the details:
Environment:
OS : Ubuntu 16
Python Version : 3.5.
MatPlotLib Version : 2.0.2
Correct Way of importing 'style module'
import matplotlib
matplotlib.use
import matplotlib.pyplot as plt
plt.style.use('ggplot')
The matplotlib help reads:
:func:~matplotlib.use (ignore syntax as "`" did not work either on command line or script file)
a function for setting the matplotlib backend. If used, this
function must be called immediately after importing matplotlib
for the first time. In particular, it must be called
before importing pylab (if pylab is imported).
Somehow without issuing this command, it was not possible to access the 'Style' module.
Hope this helps.
For MatPlotLib version 3.6.3 and above, the following code to be used to use "seaborn" style as the seaborn has been deprecated since version 3.6:
import matplotlib
matplotlib.use
import matplotlib.pyplot as plt
plt.style.use("seaborn-v0_8")

matplotlib does not work in Eclipse

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()

Categories

Resources