Skip to content

Instantly share code, notes, and snippets.

@andershammar
Created July 1, 2015 07:42
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andershammar/9070e0f6916a0fbda7a5 to your computer and use it in GitHub Desktop.
Save andershammar/9070e0f6916a0fbda7a5 to your computer and use it in GitHub Desktop.
Example showing how to use matplotlib from a Zeppelin notebook
%pyspark
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
import StringIO
def show(p):
img = StringIO.StringIO()
p.savefig(img, format='svg')
img.seek(0)
print "%html <div style='width:600px'>" + img.buf + "</div>"
# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people))
plt.barh(y_pos, performance, xerr=error, align='center', alpha=0.4)
plt.yticks(y_pos, people)
plt.xlabel('Performance')
plt.title('How fast do you want to go today?')
show(plt)
@meniluca
Copy link

Thank you, I was looking for this!

@andersgoransson
Copy link

Thanks Anders, works like a charm ;-)

@ramialbatal
Copy link

Great thanks 👍

@sgumg
Copy link

sgumg commented Mar 28, 2016

Thank you,
and one comment it may help,

I added p.clf() in the show function for clear the figure, because of without those clear function, figures are overlapped.

@gdshen
Copy link

gdshen commented Apr 24, 2016

just a comment for Python3 users
import io instead of import StringIO
and use img.getvalue() in the following code
print("%html <div style='width:600px'>" + img.getvalue() + "</div>")

@carlessanagustin
Copy link

It doesn't work for me :(
This is the output:

Traceback (most recent call last):
  File "/tmp/zeppelin_pyspark.py", line 222, in <module>
    eval(compiledCode)
  File "<string>", line 14, in <module>
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2526, in barh
    ax = gca()
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 803, in gca
    ax =  gcf().gca(**kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 450, in gcf
    return figure()
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 423, in figure
    **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 79, in new_figure_manager
    return new_figure_manager_given_figure(num, figure)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 87, in new_figure_manager_given_figure
    window = Tk.Tk()
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1767, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
TclError: no display name and no $DISPLAY environment variable

Can you please advice me what am I missing?
Thank you.
c.

@Gmousse
Copy link

Gmousse commented Jun 8, 2016

+1 I have the same error
TclError: no display name and no $DISPLAY environment variable

EDIT: Resolved

import os
import matplotlib
matplotlib.use('Agg') 
os.system("export DISPLAY=:0")

@meniluca
Copy link

meniluca commented Jul 5, 2016

With the example above I had to add:

plt.switch_backend('agg')

Otherwise I get:

RuntimeError: Invalid DISPLAY variable

Cheers,
Luca

@larkz
Copy link

larkz commented Oct 19, 2016

This method seems to lag up the Zeppelin notebook, any idea why this is?

@deus42
Copy link

deus42 commented Dec 2, 2016

Thanks @H4ml3t

@seufagner
Copy link

Save my time, @H4ml3t!

@seufagner
Copy link

The size of your sample @deus42

@ShivanandKhobanna
Copy link

Thank you @meniluca

@livelybug
Copy link

livelybug commented Oct 7, 2017

Hello,
I have a zeppelin running on Ubuntu. Then access the zeppelin from a Windows machine, via Chrome.

There is a XManager running on the Windows machine, which will display Ubuntu apps like gedit, firefox by setup:
export DISPLAY=192.168.1.42:0.0

After trying all the solutions above, always hit error:
The XManager seems not connected.

TclError: no display name and no $DISPLAY environment variable

Appreciated if any advice.

@jalborno
Copy link

Smashing, thank you very much!

@will-gao42
Copy link

awesome, saved a lot of time! Thanks!

@NeerajBhadani
Copy link

Try

z.showplot(plt)

or

z.showmatplot(plt)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment