2
votes

In my jupyter notebook, I made an offline interactive plot with plotly. I'm trying to save this interactive plot as a html file locally.

Below is my code. However, I cannot find my file I intended to save anywhere. Does anyone know what I did wrong? Thanks a lot.

py.offline.init_notebook_mode(connected=True)
import plotly as py
import plotly.graph_objs as go
import numpy as np

y = np.random.randn(500)
data = [go.Histogram(y=y)]

py.offline.iplot(data, filename='myplot.html')
3
A similar example: stackoverflow.com/questions/57567113/… can be used to save a plot directly via write_html - Eulenfuchswiesel

3 Answers

2
votes

You're using iplot which is the interactive plot method that plots it in the Jupyter notebook for you to see. If you want to generate the HTML file change iplot to plot and it will create it for you:

import plotly as py
py.offline.init_notebook_mode(connected=True)
import plotly.graph_objs as go
import numpy as np

y = np.random.randn(500)
data = [go.Histogram(y=y)]

py.offline.plot(data, filename='myplot.html')
2
votes

Plotly 4.x:

Use to_html or write_html.

import plotly.io as pio
import plotly.express as px
df = px.data.iris()
fig = px.bar(df, x='sepal_length', y='sepal_width')
pio.write_html(fig, file='iris.html')

reference

0
votes

Moreover, if you are using Cufflinks or any other offline mode, you can export the html interactive graph in the following way:

# Original cufflinks call to plot the graph within a Jupyter Notebook:

plyo.iplot(pv.iplot(asFigure=True,kind='bar',xTitle='REF',yTitle='Defects',title='Kind of Defects',barmode='stack'))

# Export to html file

fig =pv.iplot(asFigure=True,kind='bar',xTitle='REF',yTitle='Defects',title='Kind of Defects',barmode='stack')

pio.write_html(fig, file='defects.html')