Cannot run matplotlib with Apache Zeppelin - python

I am using Zeppelin and matplotlib to visualize some data. I try them but fail with the error below. Could you give me some guidance how to fix it?
%pyspark
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()
And here is the error I've got
Traceback (most recent call last):
File "/tmp/zeppelin_pyspark-3580576524078731606.py", line 235, in <module>
eval(compiledCode)
File "<string>", line 1, in <module>
File "/usr/lib64/python2.6/site-packages/matplotlib/pyplot.py", line 78, in <module>
new_figure_manager, draw_if_interactive, show = pylab_setup()
File "/usr/lib64/python2.6/site-packages/matplotlib/backends/__init__.py", line 25, in pylab_setup
globals(),locals(),[backend_name])
File "/usr/lib64/python2.6/site-packages/matplotlib/backends/backend_gtkagg.py", line 10, in <module>
from matplotlib.backends.backend_gtk import gtk, FigureManagerGTK, FigureCanvasGTK,\
File "/usr/lib64/python2.6/site-packages/matplotlib/backends/backend_gtk.py", line 8, in <module>
import gtk; gdk = gtk.gdk
File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py", line 64, in <module>
_init()
File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py", line 52, in _init
_gtk.init_check()
RuntimeError: could not open display
I also try to add these lines, but still cannot work
import matplotlib
matplotlib.use('Agg')

The following works for me with Spark & Python 3:
%pyspark
import matplotlib
import io
# If you use the use() function, this must be done before importing matplotlib.pyplot. Calling use() after pyplot has been imported will have no effect.
# see: http://matplotlib.org/faq/usage_faq.html#what-is-a-backend
matplotlib.use('Agg')
import matplotlib.pyplot as plt
def show(p):
img = io.StringIO()
p.savefig(img, format='svg')
img.seek(0)
print("%html <div style='width:600px'>" + img.getvalue() + "</div>")
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
show(plt)
The Zeppelin documentation suggests that the following should work:
%python
import matplotlib.pyplot as plt
plt.figure()
(.. ..)
z.show(plt)
plt.close()
This doesn't work for me with Python 3, but looks to be addressed with the soon-to-be-merged PR #1213.

Note that as of Zeppelin 0.7.3, matplotlib integration is much more seamless, so the methods described here are no longer necessary. https://zeppelin.apache.org/docs/latest/interpreter/python.html#matplotlib-integration

As per #eddies suggestion, I tried and this is what worked for me on Zeppelin 0.6.1 python 2.7
%python
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.figure()
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
z.show(plt, width='500px')
plt.close()

Change this:
import matplotlib
matplotlib.use('Agg')
with
import matplotlib.pyplot as plt; plt.rcdefaults()
plt.switch_backend('agg')
Complete code example Spark 2.2.0 + python3(anaconda3.5):
%spark.pyspark
import matplotlib.pyplot as plt; plt.rcdefaults()
plt.switch_backend('agg')
import numpy as np
import io
def show(p):
img = io.StringIO()
p.savefig(img, format='svg')
img.seek(0)
print ("%html <div style='width:600px'>" + img.getvalue() + "</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)

I would suggest you to use IPython/IPySpark interpreter in zeppelin 0.8.0 which will be released soon. The matplotlib integration in ipython is almost the same as jupyter. There's one tutorial https://www.zepl.com/viewer/notebooks/bm90ZTovL3pqZmZkdS9lN2Q3ODNiODRkNjA0ZjVjODM1OWZlMWExZjM4OTk3Zi9ub3RlLmpzb24

Related

Apache Zeppelin gives error when using matplotlib

The following piece of code works fine when I run the script in pycharm
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.patches as mpatches
c1 = mpatches.Patch(color="green",label="No Traffic")
c2 = mpatches.Patch(color="red",label="Traffic")
df = predict_df.limit(100).toPandas()
colors = {0:"red",1:"green"}
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(df["avgSpeed"],df["vehicleCount"],df["avgMeasuredTime"],c=df["prediction"].apply(lambda x: colors[x]),s=100,marker="o")
ax.set_xlabel('avgMeasuredTime')
ax.set_ylabel('avgSpeed')
ax.set_zlabel('vehicleCount')
plt.title("BiKmeans Traffic Data")
plt.legend(handles=[c1,c2])
plt.show()
I have installed matplotlib using pip, also based on some similar questions tried to install as sudo apt-get install python-matplotlib
but I get the same error in Zepplin,
File "/home/benjamin/.local/lib/python2.7/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 280, in <lambda>
key=lambda col: col.do_3d_projection(renderer), AttributeError: 'PathCollection' object has no attribute 'do_3d_projection'
The version of matplotlib is 2.2.0

importerror cannot import name candlestick

I got an error message from Python:
Traceback (most recent call last):
File "stock_script.py", line 9, in <module>
from matplotlib.finance import candlestick
ImportError: cannot import name candlestick
Why?
Matplotlib code has been changed...
Go into the script and replace "candlestick" by "candlestick_ohlc"
So it should read:
from matplotlib.finance import candlestick_ohlc
candlestick_ohlc(ax1, ...
Not very nice changing the name and breaking existing code...
I have fixed this by changing the import like this.
from matplotlib.finance import candlestick_ohlc as candlestick
No further changes are then needed in the code.
You need to add an underscore to it to use this same library:
from matplotlib.finance import _candlestick

Easy Install Astropysics gives Errors when transforming Coordinates, coordsys

I have some code that I wrote and imported to a new computer, I tried to do easy_install on astropysics and networkx but it seems that my previous (working) code has trouble with the easy_install version of astropysics. I've not seen this one before so I thought maybe someone here would have an idea. My relevant code is:
import os, sys
import numpy as np
import matplotlib.pyplot as plt
import asciitable
from scipy import stats
import astropysics
import astropysics.obstools
import astropysics.coords
import math
import pylab as P
import random
from random import randint
from pylab import *
from astropysics.coords import ICRSCoordinates,GalacticCoordinates
from scipy.optimize import curve_fit
f=open(sys.argv[1])
y= asciitable.read(f,Reader=asciitable.CommentedHeader,delimiter=' ')
f.close()
vhc = y['Vhelavg']
verr = y['Verravg']
Radeg=y['Radeg']
Decdeg=y['Decdeg']
Rcoeff=y['Rcoeffavg']
logg=y['loggavg']
vhcgood= (vhc != -99.9) & (vhc >= -400) & (vhc <= 400) & (logg<=2.3)
vhcg=vhc[vhcgood]
l=[]
b=[]
Radg=Radeg[vhcgood]
Decdg=Decdeg[vhcgood]
for i in xrange(len(Radg)):
gcoords=ICRSCoordinates(Radg[i],Decdg[i]).convert(GalacticCoordinates)
l.append(gcoords.l.radians)
b.append(gcoords.b.radians)
The Error is:
Traceback (most recent call last):
File "gauss01_15.py", line 46, in <module>
gcoords=ICRSCoordinates(Radg[i],Decdg[i]).convert(GalacticCoordinates)
File "/Library/Python/2.7/site-packages/Astropysics-1.0-py2.7.egg/astropysics/coords/coordsys.py", line 1895, in convert
res = EpochalLatLongCoordinates.convert(self,tosys,optimize)
File "/Library/Python/2.7/site-packages/Astropysics-1.0-py2.7.egg/astropysics/coords/coordsys.py", line 1781, in convert
res = LatLongCoordinates.convert(self,tosys,optimize)
File "/Library/Python/2.7/site-packages/Astropysics-1.0-py2.7.egg/astropysics/coords/coordsys.py", line 1728, in convert
return CoordinateSystem.convert(self,tosys)
File "/Library/Python/2.7/site-packages/Astropysics-1.0-py2.7.egg/astropysics/coords/coordsys.py", line 1098, in convert
convpath = CoordinateSystem.getTransformPath(self.__class__,tosys)
File "/Library/Python/2.7/site-packages/Astropysics-1.0-py2.7.egg/astropysics/coords/coordsys.py", line 1038, in getTransformPath
path = nx.shortest_path(g,fromsys,tosys,weighted=True)
TypeError: shortest_path() got an unexpected keyword argument 'weighted'
I've never gotten this before and never had any issues with the simple python coordinate transform in astropysics. Any ideas are appreciated.
You probably need to upgrade your astropysics version. There was a change in the networkx (nx) dependency of astropysics, that has been taken care of in more recent version of astropysics. (The keyword argument weighted changed to weight, and apparently you have incompatible versions of astropysics and networkx to handle this change.)
The relevant fix is show in these lines in astropysics.
You probably want to upgrade the networkx package as well, if that doesn't happen automatically for you.
easy_install --upgrade astropysics
easy_install --upgrade networkx
hopefully does it for you.

AttributeError: 'module' object has no attribute 'cbook'

I am trying to run a simple code and I have all the dependencies for matplotlib and numpy installed in my Canopy. Still I am getting error.
import cv2
import numpy as np
import matplotlib.pyplot as plt
x = cv2.imread('jay.jpg')
plt.imshow(x, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
plt.show()
Error:
%run "c:\users\jay\appdata\local\temp\tmppvibq9.py"
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
c:\users\jay\appdata\local\temp\tmppvibq9.py in <module>()
2 import numpy as np
3 import matplotlib.cbook
----> 4 import matplotlib.pyplot as plt
5
6 x = cv2.imread('jay.jpg')
C:\Users\Jay\AppData\Local\Enthought\Canopy\System\Lib\site-packages\matplotlib\pyplot.py in <module>()
27 from cycler import cycler
28 import matplotlib
---> 29 import matplotlib.colorbar
30 from matplotlib import style
31 from matplotlib import _pylab_helpers, interactive
C:\Users\Jay\AppData\Local\Enthought\Canopy\System\Lib\site-packages\matplotlib\colorbar.py in <module>()
30
31 import matplotlib as mpl
---> 32 import matplotlib.artist as martist
33 import matplotlib.cbook as cbook
34 import matplotlib.collections as collections
C:\Users\Jay\AppData\Local\Enthought\Canopy\System\Lib\site-packages\matplotlib\artist.py in <module>()
9 import numpy as np
10 import matplotlib
---> 11 import matplotlib.cbook as cbook
12 from matplotlib.cbook import mplDeprecation
13 from matplotlib import docstring, rcParams
AttributeError: 'module' object has no attribute 'cbook'
Dependencies I have installed for numpy and matplotlib:
1.) libsvm-3.17.win64-py2.7
2.) pyparsing-2.0.3-1.win64-py2.7
3.) python-dateutil-2.4.2-2.win64-py2.7
4.) pytz-2015.7-1.win64-py2.7
5.) six-1.10.0-1.win64-py2.7
6.) scipy-0.13.3.win64-py2.7
7.) numpy-MKL-1.9.2-2.win64-py2.7
8.) Matplotlib 1.5.1-win64-py2.7
9.) pip 8.0.2-1.win64-py2.7
Try this:
Close your jupyter notebook and terminate ipython.
Restart Terminal
Enter this command in Terminal:
export LANG=en_US.UTF-8;export LC_ALL=en_US.UTF-8
Re-run your ipython / Jupyter notebook now. It works like a charm.
conda install matplotlib --force
I don't know for sure that this is causing your problem, but you are running your code in the wrong Python environment:
C:\Users\Jay\AppData\Local\Enthought\Canopy\System\
You should be running it in
C:\Users\Jay\AppData\Local\Enthought\Canopy\User
See
https://support.enthought.com/entries/23646538-Make-Canopy-User-Python-be-your-default-Python
and
http://docs.enthought.com/canopy/configure/faq.html#where-are-all-of-the-python-packages-in-my-user-python-environment
You can find which 'matplotlib.py' is imported and then open that file and check if it has 'cbook' or not.
import imp
imp.find_module("matplotlib")
i had the same problem and it was because of this line :
from sys import stdout
i deleted this import and the problem is gone now

Trouble saving matplotlib animation [duplicate]

This question already has an answer here:
Using FFmpeg and IPython
(1 answer)
Closed 7 years ago.
I'm using matplotlib to make an animated heatmap. I have data in a text file (rs_h) with 3 columns - x, y, z; i'm using scatterplot to make a simple heatmap, and then using the animation package to update the heatmap over time
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
data = pd.read_table('rs_h', header=None, sep=r"\s*")
frames = np.array_split(data, 9)
def main():
numframes = 9
numpoints = 75
x, y, c = np.random.random((3, numpoints))
fig = plt.figure()
scat = plt.scatter(x, y, c=c)#, s=100)
ani = animation.FuncAnimation(fig, update_plot, frames=xrange(numframes),
interval = 5)
#ani.save("movie.avi", codec='avi')
plt.show()
def update_plot(i):
frame = frames[i]
scat = plt.scatter(frame[0], frame[1], c=frame[2])
return scat,
main()
I'm having no trouble getting the animated heatmap; however, i run into an issue when i try to save the animation
/Users/Arjun/anaconda/lib/python2.7/site-packages/matplotlib/animation.py:695: UserWarning: MovieWriter ffmpeg unavailable
warnings.warn("MovieWriter %s unavailable" % writer)
Traceback (most recent call last):
File "heat_ani.py", line 29, in <module>
main()
File "heat_ani.py", line 21, in main
ani.save("movie.avi", codec='avi')
File "/Users/Arjun/anaconda/lib/python2.7/site-packages/matplotlib/animation.py", line 712, in save
with writer.saving(self._fig, filename, dpi):
AttributeError: 'str' object has no attribute 'saving'
Anyone know what the issue is and how to get around it?
EDIT: The issue was that i didn't have ffmpeg installed. A simple brew install allowed the code to work
I found a solution for Linux. Basically you need ffmpeg library or libav-tools
Open terminal and type as root
apt-get install ffmpeg
or
apt-get install libav-tools
Hope it might help.

Categories