How to get plotly graphs within jupyter notebook? - python-3.x

I am trying to create plotly bar graphs offline within Jupyter notebook. But I am getting the below error message. I am using x and y-axis from the data frame called train. X variable called Sex(Male or female) and Y variable called Survived (1 or 0)
Error message
"PlotlyError: Because you didn't supply a 'file_id' in the call, we're assuming you're trying to snag a figure from a url. You supplied the url, '', we expected it to start with 'https://plot.ly'.
Run help on this function for more information."
import plotly
from plotly.graph_objs import Bar, Scatter, Figure, Layout
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
from plotly.graph_objs import *
import numpy as np
data = [go.Bar(x=train.Sex,
y=train.Survived)]
py.iplot(data, filename='jupyter-basic_bar')
Any suggestions on how to get graphs within Jupyter notebook offline?

Change the line: py.iplot(data, filename='jupyter-basic_bar')
to iplot(data)
Example:

Related

Counting ipywidgets Intslider steps in Jupyter notebook

I am using the IntSlider() method from the ipywidgets() class to visualize and record the count for each step of a parameter in a seaborn Kdeplot(). Any quick fix is deeply appreciated. Thanks
import ipywidgets as widgets
from ipywidgets import Play
from IPython.display import display
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
cars = sns.load_dataset('mpg')
#widgets.interact(bw=(.1,5,.1))
def bandwidth(bw=0):
sns.kdeplot(cars.horsepower,lw=3,fill=True,bw_adjust=bw)
plt.xlim(-20,200)
plt.ylim(0, 0.04);
The widgets work well, but I need a way to record/capture the count (Good and Bad) of each step move in the slider; hopefully get the data in a list or table.

can anyone explain why this code regarding plotly libraries worked well in jupyter notebook but showed an error when i ran it in intellij

I saw this video on youtube : using plotting a dataframe using iplot method imported from plotly.offline module.
I ran this code on intellij but got an error saying :
latin-1' codec can't encode characters in position 0-9: ordinal not in range(256)
i looked up for a solution but couldn't anything. Then i ran this code in jupyter notebook and it worked just fine
can anyone explain this.
source code -
import pandas as pd
import numpy as np
import chart_studio.plotly as py
from plotly.offline import *
import cufflinks as cf
init_notebook_mode(connected=True)
cf.go_offline()
df=pd.DataFrame(np.random.randn(50,4),columns=['a','b','c','d'])
df.iplot()

Why will Seaborn function 'regplot' not run in Jupyter?

I am having trouble with code Seaborn regplot function in Jupyter notebooks using Watson-Studio.
Using Python 3.6, the code appears to get stuck whilst processing, and this happens until I stop the code.
When I run this using IDLE on my Mac, the code runs perfectly and the plot shows.
Seems to happen with plots lmplot and regplot, however boxplots etc do show as normal.
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
df = pd.read_csv(csv.csv)
sns.regplot(x = 'independent', y = 'dependent', data = df)
The expected results should be a graph of the linear relationship between the two variables, however I am just getting a loading bar.
When I stop running the kernel, the graph exists as a scatterplot with no line of best fit. Of course this has the error in notebook as 'Keyboard Interrupted'.
Could this possibly be a bug? Thanks for your help.
Set ci parameter to none and it will solve your problem.
sns.regplot(x = 'independent', y = 'dependent', data = df, ci = None)

Update a Bokeh Span with an interact element in Jupyter notebook

I am trying to make a span in bokeh using jupyter widgets.
from ipywidgets import interact
import numpy as np
from scipy.stats import norm
from bokeh.sampledata.daylight import daylight_warsaw_2013
from bokeh.io import push_notebook, show, output_notebook
from bokeh.plotting import figure
from bokeh.models import Span
output_notebook()
p = figure()
x_axis = np.arange(-10, 10, 0.001)
# Mean = 0, SD = 2.
y_axis = norm.pdf(x_axis,0,2)
p.line(x_axis, y_axis, line_dash='solid', line_width=2)
cutoff = Span(location=1,
dimension='height', line_color='green',
line_dash='dashed', line_width=2)
p.add_layout(cutoff)
show(p, notebook_handle=True)
def update(new_cutoff_location):
cutoff.location = new_cutoff_location
push_notebook()
interact(update, new_cutoff_location = 1.0)
When I run this code I get ValueError: PATCH-DOC message requires at least one event at push_notebook(). I suspect this indicates that the update to cutoff.location isn't getting detected, so it it looks as if there are no changes to send. Passing the handle doesn't seem to make a difference. Looking at the sample code in this github issue, it looks like there used to be an set method on span elements, but there doesn't appear to be one on my span element cutoff. Maybe there is a different function I am supposed to call to register the change?
I'm on bokeh 0.12.11 with jupyter 1.0.0, jupyter-client 5.1.0, jupyter-console 5.2.0, jupyter-core 4.4.0
This appears to be a regression in Bokeh 0.12.11. Your code works with version 0.12.10 so the immediate workaround is to downgrade. I've made GitHub issue here that you can follow. We will issue a new point release with a fix ASAP.
UPDATE: The issue is now fixed in recent versions of Bokeh

Plotly graph shows a blank space on GitHub

I am using Plotly to make graphs in my IPython notebook. I am able to view graphs on my IPython notebook when I upload them on GitHub they are displayed as blank spaces.
I read on the web that Plotly currently does not support iframes and hence the issue, but is there a workaround?
Here's the link to my GitHub Ipython notebook:
https://github.com/dhavalbhinde/bhinde_dhaval_spring2017/blob/master/Finals/Analysis%203.ipynb
Please, can someone advice how should I handle them?
I found a way to show Plotly plots on Github. They aren’t interactive anymore but it’s better than nothing.
First
import plotly.io as pio
pio.renderers
you can see the list of available renders.
*if you get an error on this step you can simply just install orca:
conda install -c plotly plotly-orca
and then there are 2 possible ways.
you can pass "svg" to .show() like this:
fig = px.scatter_3d(iris, x=transformed_iris['component1'], y=transformed_iris['component2'], z=transformed_iris['component3'],color='species')
fig.show(renderer="svg")
or you can set the pio.renderers.default to svg:
pio.renderers.default = "svg"
Import these and this and it will work :
from plotly.offline import plot, iplot, init_notebook_mode
import plotly.graph_objs as go
init_notebook_mode(connected=True)

Resources