Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly notebook mode with google colaboratory

I am am trying out colaboratory with plotly notebook mode - I open a new notebook, copy and paste the following simple example from plotly's documentation, but don't see an output. There is a large blank in the output space where the plot whould normally be.

This works fine in my local notebook (which is a newer version of plotly, but per their docs offline mode should work with the google colab version) Any ideas?

import plotly from plotly.graph_objs import Scatter, Layout  plotly.offline.init_notebook_mode(connected=True)  plotly.offline.iplot({     "data": [Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1])],     "layout": Layout(title="hello world") }) 
like image 911
elz Avatar asked Nov 10 '17 20:11

elz


People also ask

Can you use plotly in Google Colab?

Plotly is now more powerful than ever with a new open source library named JupyterDash. JupyterDash is developed on top of the Dash framework to make it completely suitable for notebook environments such as Colab.

Can I use Google colab instead of Jupyter Notebook?

Both Jupyter Notebook and Google Colab may be the right choice in particular circumstances. Google Colab is an excellent choice for the entry-level developer or the non-programmer who wants to get started fast without having to install anything.

Can you use plotly in Jupyter Notebook?

You can publish Jupyter Notebooks on Plotly. Simply visit plot.ly and select the + Create button in the upper right hand corner. Select Notebook and upload your Jupyter notebook (. ipynb) file!

How do I visualize data in Google Colab?

In order to visualize data from a Pandas DataFrame , you must extract each Series and often concatenate them together into the right format. It would be nicer to have a plotting library that can intelligently use the DataFrame labels in a plot.


1 Answers

plotly version 4.x

As of version 4, plotly renderers know about Colab, so the following is sufficient to display a figure in both Colab and Jupyter (and other notebooks like Kaggle, Azure, nteract):

import plotly.graph_objects as go fig = go.Figure( go.Scatter(x=[1,2,3], y=[1,3,2] ) ) fig.show() 

plotly version 3.x

Here's an example showing the use of Plotly in Colab. (Plotly requires custom initialization.)

https://colab.research.google.com/notebook#fileId=14oudHx5e5r7hm1QcbZ24FVHXgVPD0k8f

You need to define this function:

def configure_plotly_browser_state():   import IPython   display(IPython.core.display.HTML('''         <script src="/static/components/requirejs/require.js"></script>         <script>           requirejs.config({             paths: {               base: '/static/base',               plotly: 'https://cdn.plot.ly/plotly-latest.min.js?noext',             },           });         </script>         ''')) 

And call it in each offline plotting cell:

configure_plotly_browser_state() 
like image 96
Bob Smith Avatar answered Oct 10 '22 23:10

Bob Smith