如何将离线Plotly图表导出为交互式HTML

时间:2019-07-25 12:45:23

标签: python export plotly

我创建了一个离线绘图图表。我不知道如何将其导出为交互式HTML文件。我可以轻松导出PNG版本,但是我需要一个交互式文件。

Here is the chart I have generated

我使用的代码

import plotly as py
import plotly.graph_objs as go
import pandas as pd

py.offline.init_notebook_mode(connected=True)



df = pd.read_csv('C:/Users/Downloads/PopularPublisher.csv')
sd = df.nlargest(5,'Views')
fd = sd.sort_values(by='Views', ascending = True)


my_data = [go.Bar( x = fd.Views, y = fd.Publisher, orientation = 'h',)]
my_layout = go.Layout({"title": "Most popular publishers",
                       "yaxis": {"title":"Publisher"},
                       "xaxis": {"title":"Views"},
                       "showlegend": False}, yaxis=go.layout.YAxis(
        tickmode='array',
        automargin=True,
    )
    )


fig = go.Figure(data = my_data, layout = my_layout)

py.offline.iplot(fig)

对于如何导出此文件的任何指导,我深表感谢。

1 个答案:

答案 0 :(得分:1)

您可以将代码的最后一行更改为py.offline.plot(fig, filename="file.html")

或者,如果您已升级到版本4,则可以使用fig.write_html("file.html")