Reputation: 5708
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")
})
Upvotes: 51
Views: 58191
Reputation: 380
Solution
import plotly.io as pio
pio.renderers.default = "colab"
You need to change the default render. Here is an excerpt from the documentation. https://plot.ly/python/renderers/#setting-the-default-renderer
The current and available renderers are configured using the
plotly.io.renderers
configuration object. Display this object to see the current default renderer and the list of all available renderers.
>>>import plotly.io as pio
>>>pio.renderers
Renderers configuration
-----------------------
Default renderer: 'notebook_connected'
Available renderers:
['plotly_mimetype', 'jupyterlab', 'nteract', 'vscode',
'notebook', 'notebook_connected', 'kaggle', 'azure', 'colab',
'cocalc', 'databricks', 'json', 'png', 'jpeg', 'jpg', 'svg',
'pdf', 'browser', 'firefox', 'chrome', 'chromium', 'iframe',
'iframe_connected', 'sphinx_gallery']
Upvotes: 27
Reputation: 3572
simply pass "colab"
as the value for the parameter renderer
in fig.show(renderer="colab")
example :
import plotly.graph_objects as go
fig = go.Figure(
data=[go.Bar(y=[2, 1, 3])],
layout_title_text="A Figure Displayed with the 'colab' Renderer"
)
fig.show(renderer="colab")
Upvotes: 51
Reputation: 38619
plotly
version 4.xAs 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.xHere'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()
Upvotes: 61
Reputation: 1526
configure_plotly_browser_state()
can be executed before running every cell by using IPython's pre_run_cell hook:
import IPython
IPython.get_ipython().events.register('pre_run_cell', configure_plotly_browser_state)
Upvotes: 15
Reputation: 340
You need to add a method in order to use Plotly in Colab.
def enable_plotly_in_cell():
import IPython
from plotly.offline import init_notebook_mode
display(IPython.core.display.HTML('''<script src="/static/components/requirejs/require.js"></script>'''))
init_notebook_mode(connected=False)
This method must be executed for every cell which is displaying a Plotly graph.
Sample:
from plotly.offline import iplot
import plotly.graph_objs as go
enable_plotly_in_cell()
data = [
go.Contour(
z=[[10, 10.625, 12.5, 15.625, 20],
[5.625, 6.25, 8.125, 11.25, 15.625],
[2.5, 3.125, 5., 8.125, 12.5],
[0.625, 1.25, 3.125, 6.25, 10.625],
[0, 0.625, 2.5, 5.625, 10]]
)
]
iplot(data)
Reference: https://colab.research.google.com/notebooks/charts.ipynb#scrollTo=WWbPMtDkO4xg
Upvotes: 18