maplotlib.pyplot.show() works with python but not with jupyter console - python

I have looked at this question and also this one. It looks like for me, matplotlib.pyplot.show() shows a figure from python, but not from jupyter console.
matplotlib.matplotlib_fname() returns the same matplotlibrc file location for both.
However, when I try to find the backend being used with matplotlib.rcParams['backend'] jupyter console tells me - 'module://ipykernel.pylab.backend_inline', regardless of which backend I have modified the matplotlibrc file to use.
Python on the other hand, correctly shows the backend I'm using; currently 'TkAgg'.
I installed matplotlib using python -mpip install -U matplotlib.
I'm using the following versions:
Windows 10
Jupyter console 5.2.0
Python 2.7.14
IPython 5.5.0
I can make do with using python, but it would be nice to figure this out for jupyter console as well.

First note that plt.show() works as expected, also in Juypter.
This uses the default 'module://ipykernel.pylab.backend_inline' backend. This backend is set by Jupyter, independently of the rcParams setting.
You may set the backend using matplotlib.use()
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.show()
or just using IPython magic %matplotlib backendname
%matplotlib tk
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.show()
You may change the backend using pyplot.switch_backend()
plt.switch_backend("TkAgg")
plt.plot([1,2,3])
plt.show()
or using the same IPython magic
%matplotlib tk
plt.plot([1,2,3])
plt.show()
If you want to set the backend to be used by default, see this question:
Change default backend for matplotlib in Jupyter Ipython

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

matplotlib pyplot Invalid DISPLAY variable

While importing and attempting the following:
import matplotlib
from matplotlib import pyplot as plt
plt.plot([1,2,3],[1,4,9])
plt.show()
I get the following error. How do I fix? I am running Python 2.7, and notebook version 4.1.0. Thank you.
RuntimeError: Invalid DISPLAY variable
When running a jupyter notebook on a server, the server may not even be able to display the plot. The usual solution would be to use a non-interactive backend. In case of a jupyter notebook this would be done by adding
%matplotlib inline
at the top of the notebook, such that graphics are shown as png images.

Plot Window Hangs or is Empty in Python

I am having trouble with plots in a Jupyter Notebook in Python 3.5 on Mac OSX. The following code will hang when executed:
import numpy as np
import matplotlib.pyplot as plt
#%matplotlib inline
myfig = plt.plot(range(5))
plt.show()
If I restart the kernel and un-comment '%matplotlib inline', I do get plots to work inline. However, I'd like to be plotting in a separate window.
If I insert the following code at the beginning:
import matplotlib
matplotlib.use('Agg')
then restart the kernel and run, the code will not hang, but nothing will be plotted, no window opened.
Details:
Mac Book Pro running OSX El Capitan
Anaconda Python 3.5 in a Jupyter Notebook
backend is "MacOSX".
There is a post on GitHub mentioning that using Qt4Agg as backend worked...
If it is not available (and if you can), you might want to try using Hombrew to install Python (instead of Anaconda), Qt and/or Gtk with which you'll be able to use matplotlib without problem.

ipython notebook on linux VM running matplotlib interactive with nbagg

I want buttons and other interactive matplotlib objects to appear from within my ipython notebook.
Here is what I've done:
Installed http://datasciencetoolbox.org, it is a vagrant box with ipython installed and version 1.3.1 of matplotlib.
I needed to upgrade matplotlib to the latest version, because it has this capability to do inline interactive plots. What's new in Matplotlib 1.4.1
I needed to run sudo apt-get install pkg-config and
sudo pip install matplotlib --upgradein order to get that going.
Then, in order to produce the nice (i.e. error-free) screenshot below, I went into the .ipython/dst-profile/ipython_notebook_config.py file and erased the line about IPKernelApp.pylab='inline' to be able to run the matplotlib.use('nbagg') command.
Then I was able to create the screenshot below. However, things still look poor. Those buttons are not buttons. That is an image of buttons. Please advise on how to make those buttons come to life!
Oh... and check this out if this helps you help me.
Thanks!
Basically you are facing two issues
the %pylab inlinecall overrides the matplotlib.use('nbagg')call, to use the inline backend instead of the nbagg backend which you are actually wanting. If you use a recent version of IPython (2.3) you can directly use %matplotlib nbagg (or %matplotlib notebook) to load the nbagg backend instead of your %pylabcall.
once you enabled the nbagg backend you will need to explicitly show it, ie. add a plt.show() call at the end of your script -> Update: with IPython 2.3.1 this is no longer needed (thanks #tcaswell for the hint)
With this you get the interactive matplotlib experience embedded in the IPython notebook. However, a quick try of your code does't yield to the desired result. The Button reacts and the callback is executed but the print call doesn't show anything. Anyway, to see that it's working try the following simple example (requires IPython 2.3):
%matplotlib nbagg
from matplotlib.widgets import Button
import matplotlib.pyplot as plt
def callback(event):
plt.text(event.xdata, event.ydata, 'clicked')
f,a = plt.subplots(1)
b1 = Button(a,'Button1')
b1.on_clicked(callback)
plt.show()
Btw. it is highly recommended to use %matplotlib instead of %pylab as later leads to some side effects, see here.

in Ipython notebook / Jupyter, Pandas is not displaying the graph I try to plot

I am trying to plot some data using pandas in Ipython Notebook, and while it gives me the object, it doesn't actually plot the graph itself. So it looks like this:
In [7]:
pledge.Amount.plot()
Out[7]:
<matplotlib.axes.AxesSubplot at 0x9397c6c>
The graph should follow after that, but it simply doesn't appear. I have imported matplotlib, so that's not the problem. Is there any other module I need to import?
Note that --pylab is deprecated and has been removed from newer builds of IPython, The recommended way to enable inline plotting in the IPython Notebook is now to run:
%matplotlib inline
import matplotlib.pyplot as plt
See this post from the ipython-dev mailing list for more details.
Edit:Pylab has been deprecated please see the current accepted answer
Ok, It seems the answer is to start ipython notebook with --pylab=inline.
so ipython notebook --pylab=inline
This has it do what I saw earlier and what I wanted it to do.
Sorry about the vague original question.
With your import matplotlib.pyplot as plt just add
plt.show()
and it will show all stored plots.
simple after importing the matplotlib you have execute one magic if you have started the ipython as like this
ipython notebook
%matplotlib inline
run this command everything will be shown perfectly
start ipython with ipython notebook --pylab inline ,then graph will show inline.
import matplotlib as plt
%matplotlib as inline
All you need to do is to import matplotlib.
import matplotlib.pyplot as plt

Categories

Resources