plotly inside jupyter notebook python

codingknob picture codingknob · Dec 25, 2016 · Viewed 19.8k times · Source

Does anyone know how to use plotly inside jupyter notebook using python?

The documentation is not very well organized, at-least not from my point of view.

For example, I can run the following code but it generates a graph in a HTML file that can be accessed OUTSIDE of jupyter notebook. Is there a way for the graph to be rendered inside the notebook?

What is also not clear to me (as documentation is poor) is that does one require a credentials to use plotly for plots inside jupyter notebook? Are the credentials only required for hosting the plots on their cloud and nothing more?

As well I find that there isn't any real documentation of cufflinks. All it says is it makes using plotly with pandas dataframes easier. But for someone who isn't a developer it would be nice if there was detailed documentation on why exactly is it necessary and what exactly is it doing that makes life easier.

import plotly.plotly as py
from plotly.graph_objs import *

trace0 = Scatter(
  x=[1, 2, 3, 4],
  y=[10, 15, 13, 17]
)
trace1 = Scatter(
  x=[1, 2, 3, 4],
  y=[16, 5, 11, 9]
)
data = Data([trace0, trace1])

plotly.offline.plot(data, filename = 'basic-line')

/Users/blahblah/anaconda/lib/python2.7/site-packages/plotly/offline/offline.py:433: UserWarning:

Your filename `basic-line` didn't end with .html. Adding .html to the end of your file.

'file:///Users/blahblach/Box Sync/NS/NBooks/basic-line.html'
In [ ]:

If I change the last line in the code to:

py.iplot(data, filename = 'basic-line')

I get the credential error:

PlotlyLocalCredentialsError: 
Couldn't find a 'username', 'api-key' pair for you on your local machine. To sign in temporarily (until you stop running Python), run:
>>> import plotly.plotly as py
>>> py.sign_in('username', 'api_key')

Even better, save your credentials permanently using the 'tools' module:
>>> import plotly.tools as tls
>>> tls.set_credentials_file(username='username', api_key='api-key')

For more help, see https://plot.ly/python.

UPDATE:

I tried to execute the pandas examples as described here.

I get credential errors for all df.iplot() or Series.iplot() commands.

Can someone kindly explain why am I getting credential errors despite using iplot().

There is also no useful documentation regarding cufflinks.

The plot.ly documentation is one of the worst I have seen. Organization is a mess and not very example friendly.

Answer

Psidom picture Psidom · Dec 25, 2016

From the docs, you need to initiate the Plotly Notebook with init_notebook_mode, also note that when you call py.iplot it is still calling the plot function from online plotly module, you need to import the iplot(not plot) from plotly.offline and use it for offline plot and inside notebook rendering. You don't need the credentials for offline plot:

from plotly.offline import init_notebook_mode, iplot
from plotly.graph_objs import *

init_notebook_mode(connected=True)         # initiate notebook for offline plot

trace0 = Scatter(
  x=[1, 2, 3, 4],
  y=[10, 15, 13, 17]
)
trace1 = Scatter(
  x=[1, 2, 3, 4],
  y=[16, 5, 11, 9]
)

iplot([trace0, trace1])               # use plotly.offline.iplot for offline plot

enter image description here