Apache Zeppelin gives error when using matplotlib - python

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

Related

Empty graph shown when using iplot from chart_studio insted of plotly

I am following a python tutorial about the use of plotly.
Here are some commands I have to run to import functions and methods I will use
import plotly.plotly as py
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
However, when I run commands on my jupyter nootbook, it says the use of plotly is deprecated and it recommends me to use the module chart_studio instead (error points to line import plotly.plotly as py):
ImportError: The plotly.plotly module is deprecated, please install
the chart-studio package and use the chart_studio.plotly module
instead.
So I run
pip install chart_studio
and try to replace the line above with functions and methods coming from the chart_studio module.
Here is my code:
import chart_studio.plotly as py
import plotly.graph_objects as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
data = dict(type= 'cloropleth',
locations = ['AZ','CA','NY'],
locationmode = 'USA-states',
colorscale = 'Portland',
text = ['text 1','text 2','text 3'],
z = [1,2,3],
colorbar = {'Title':'Colorbar title goes here'})
mylayout = dict(geo={'scope':'usa'})
choromap = go.Figure(data = [data], layout=mylayout, skip_invalid=True)
iplot(choromap)
The problem is that, when running the final line iplot(choromap), I get this empty graph
While in the tutorial this other graph appears
What is wrong?
Please note that I installed cufflinks-0.17.3 plotly-4.5.4
Did you try to cleanly install plotly?
Uninstall plotly using pip
!pip uninstall plotly
Then uninstall plotly using conda
!conda uninstall plotly
After that, install the last version using pip
!pip install plotly
check Plotly version
import plotly
plotly.__version__
SOLVED
In my code there were 2 errors:
at line
type= 'cloropleth',
I had the mispelled value 'cloropleth', where the correct value is 'choropleth',
and then at line
colorbar = {'Title':'Colorbar title goes here'})
I had 'Title', where the correct key is 'title' (lowercase).
Fixed them and now the map is correctly displayed.
Also, it was not necessary to install chart_studio.
So in the end the correct code is:
import plotly.graph_objects as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
data = dict(type= 'choropleth',
locations = ['AZ','CA','NY'],
locationmode = 'USA-states',
colorscale = 'Portland',
text = ['text 1','text 2','text 3'],
z = [1,2,3],
colorbar = {'title':'Colorbar title goes here'})
mylayout = dict(geo={'scope':'usa'})
choromap = go.Figure(data = [data], layout=mylayout)
iplot(choromap)

Plotly - name 'iplot' is not defined

been playing around with plotly for Python, but constantly running into the same error message stated above. I installed plotly on Anaconda, getting the error message both on Jupyter and Spyder respectively:
#data manipulation
import pandas as pd
import numpy as np
#loading csv
df = df = pd.read_csv(r'C:\Users\hendev\Desktop\FIFA18 - Ultimate Team players.csv')
import plotly.graph_objs as go
# prepare data
x2017 = df.overall[df.added_date == 2017]
x2018 = df.overall[df.added_date == 2018]
trace1 = go.Histogram(
x=x2017,
opacity=0.75,
name = "2017",
marker=dict(color='rgba(171, 50, 96, 0.6)'))
trace2 = go.Histogram(
x=x2018,
opacity=0.75,
name = "2018",
marker=dict(color='rgba(12, 50, 196, 0.6)'))
data = [trace1, trace2]
layout = go.Layout(barmode='overlay',
title=' Rating added in 2017 and 2018',
xaxis=dict(title='students-staff ratio'),
yaxis=dict( title='Count'),
)
fig = go.Figure(data=data, layout=layout)
iplot(fig)
Any idea what I'm doing wrong?
Note: $ from plotly.plotly import iplot will work only till plotly v3.10.0 The plotly.plotly module has been deprecated in plotly v4.0. Check your version first:
$ python3 -c 'import plotly; print(plotly.__version__)' # for Python 3
$ python -c 'import plotly; print(plotly.__version__)' # for Python 2
v4.0 onwards, the functionality has been split into two modes: online and offline.
Online
If you wish to have the figures rendered online, you will now need to import chart-studio and use
from chart_studio.plotly import plot, iplot
Offline
If you want to render the images locally, you have multiple options:
from plotly.offline import iplot
# your code
iplot(fig)
or
from plotly.subplots import make_subplots
fig = make_subplots(# your args)
# your code
fig.show()
or
import plotly.io as pio
# your code
pio.show(fig)
or
import plotly.graph_objects as go
fig = go.Figure(# your args)
# your code
fig.show()
You can read up more on the renderers here.
You forgot to import it
from plotly.plotly import iplot
You need to call iplot with py:
import plotly.plotly as py
py.iplot(fig)
Just in case, if you want to plot in offline mode and save plot as file.hmtl:
import plotly.offline as py
plotly.offline.init_notebook_mode()
py.iplot(fig, filename="file.html")
Do not be afraid of looking through in documentation: here you can find nice and understandable examples how to use plotly properly. For example, here you can see how to plot simple bar chart, etc.
For Jupyter Notebook and Kaggle Notebooks, this helped me.
from plotly.offline import iplot

Cannot run matplotlib with Apache Zeppelin

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

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