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

7

30

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

Optional answered 22/5, 2019 at 21:0 Comment(9)
From the image posted by @FoxanNg, I could see that the jupyter instance is using a conda env(which could a virtualenv created for Jupyter). Could we try installing matplotlib using conda instead of pip, in the bootstrap and give it a try.Therefore
When trying to invoke conda in my bootstrap file it does not know where to find it (it gets a command not found error.)Optional
I am not sure how the cluster is setup. But from the image looks like `/opt/conda/bin/conda'. Can we use the full path to install?Therefore
it doesn't think conda is installed at bootstrap: /opt/conda/bin/conda: command not foundOptional
Started an EMR cluster, and found that it doesn't provide conda support by default. Could you confirm if we are not installing Conda via bootstrap?Therefore
We are not installing conda during bootstrapOptional
The % commands are IPython or Jupyter magic commands. Run %lsmagic and check %matplotlib is among them. If %matplotlib is found, run %matplotlib -l to list available backends. You can explicitly require a specific backend by running %matplotlib <backend name>Chyme
If you can't see %matplotlib among the output of %lsmagic, try %pylab. It justs imports matplotlib and numpy . If you want help for a particular magic command try %command?Chyme
@Optional Install matplotlb like this and then try: sudo python3 -m pip install matplotlibFiedler
C
14

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
Crenate answered 1/4, 2020 at 10:55 Comment(4)
This is the command that shows the plot. Finally, an answer from someone that was having the same use case of the original question and mine, an AWS Sagemaker Jupyter notebook connected to a Sparkmagic (PySpark) kernelSpall
When I run %matplot plt after the plot I get error: UsageError: Cell magic %%matplot not found.Antiphonal
For me that just outputs a really long string that is maybe a base64 representation of the image.Stroman
@NicScozzaro try having %matplot plt on a separate cell from the plot generating cell.Elute
C
8

As you mentioned, matplotlib is not installed on the EMR cluster, therefore such error will occur:

error

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:

local

Coop answered 23/5, 2019 at 0:28 Comment(4)
This answer makes the first cell (where I put %%local) run much more quickly but adding any additional imports (such as tensorflow) fails despite being installed and working previously. Upvoting because it technically makes the code snippet run, but not accepting because it renders the notebook nearly unusable.Optional
is not there a way to install matplotlib in that docker container??. I mean, maybe with condaEnduring
docs.aws.amazon.com/emr/latest/ReleaseGuide/…Enduring
This answer may be applicable to other contexts but it is not working on an AWS Jupyter notebook connected to an EMR cluster running the Sparkmagic (PySpark) kernel.Spall
C
4

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/

Correia answered 20/11, 2019 at 12:30 Comment(1)
Is it part of the matplotlib library? Or it is just a AWS thing?Cilice
M
3
%matplot plt

after plt.show() function works for me.

Mesopause answered 3/11, 2022 at 0:2 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Radcliff
A
2

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

Ascham answered 22/5, 2019 at 21:5 Comment(4)
I appreciate the attempted solution. I tried to address this in the question but this fails on the line %matplotlib inline with this error (I'll add the error to the original question): unknown magic command 'matplotlib' UnknownMagic: unknown magic command 'matplotlib'Optional
okay..one more attempt..could you try get_ipython().magic(u'matplotlib inline') instead of %matplotlib inlineAscham
Thanks, but unfortunately get_ipython() returns None and thus get_ipython().magic() fails :(Optional
Using @Optional recommendation I get name 'get_ipython' is not definedSpall
C
1

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
Clippers answered 10/8, 2021 at 22:59 Comment(1)
This is the only answer that worked for me in pyspark kernalYellowknife
J
-1

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()
Jael answered 24/9, 2019 at 20:34 Comment(1)
This does not work on an AWS Sagemaker jupyter notebook running the Sparkmagic (PySpark) kernel.Spall

© 2022 - 2025 — McMap. All rights reserved.