Plotly AttributeError: 'dict' object has no attribute 'write_html'

Plotly AttributeError: 'dict' object has no attribute 'write_html'

我正在尝试将 Plotly 图导出为 HTML,各种文档和 SO 线程建议在图形对象(字典)上失败的方法。任何帮助表示赞赏。最后几行是导致错误的行。该图在交互式 Python window 中绘制得很好,但我无法保存到 html。

代码:

import pandas as pd
import numpy as np
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)

nodes = [
     ['ID', 'Label', 'Color'],
[0, '1',    '#0313fc'],
[1, '2',    '#109609'],
[2, '3',    '#b9bbf0'],
[3, '4',    '#ff0d00'],
[4, '5',    '#0c0b61'],
[5, '6',    '#cc2e0e'],
[6, '7',    '#000000'],
[7, '8',    '#4b8fe3'],
[8, '9',    '#ff0000'],
[9, '10',   '#1403a8'],
[10,    '11',   '#ff0000'],
[11,    '12',   '#ED1C24'],
[12,    '13',   '#ff0000'],
[13,    '14',   '#bf1f0d'],
[14,    '15',   '#147808'],
[15,    '16',   '#00173d'],
[16,    '17',   '#2a782f'],
[17,    '18',   '#00ad0c'],
[18,    '19',   '#FCD116'],
[19,    '20',   '#00d0ff'],
[20,    '21',   '#ffae00'],
[21,    '22',   '#150191'],
[22,    '23',   '#2200ff'],
[23,    '24',   '#2200ff'],
[24,    '25',   '#034EA2'],
[25,    '26',   '#0008ff'],
[26,    '27',   '#000000'],
[27,    '28',   '#125e05'],
]

links = [
['sum','source','target','link_color'],
[70,11,24,'rgba(237, 28, 36, 0.3)'],
[1341,24,13,'rgba(44, 24, 222, 0.3)'],
[416,18,22,'rgba(252, 209, 22, 0.3)'],
[4369,18,19,'rgba(252, 209, 22, 0.3)'],
[953,1,2,'rgba(16, 150, 9, 0.3)'],
[16170,9,26,'rgba(20, 3, 168, 0.3)'],
[503,18,3,'rgba(252, 209, 22, 0.3)'],
[11000,9,3,'rgba(20, 3, 168, 0.3)'],
[7200,15,3,'rgba(0, 23, 61, 0.3)'],
[862,11,6,'rgba(237, 28, 36, 0.3)'],
[4463,11,8,'rgba(237, 28, 36, 0.3)'],
[1508,19,10,'rgba(0, 208, 255, 0.3)'],
[1470,1,10,'rgba(16, 150, 9, 0.3)'],
[1016,2,10,'rgba(185, 187, 240, 0.3)'],
[1100,11,10,'rgba(237, 28, 36, 0.3)'],
[2382,18,10,'rgba(252, 209, 22, 0.3)'],
[2065,9,10,'rgba(20, 3, 168, 0.3)'],
[450,12,10,'rgba(255, 0, 0, 0.3)'],
[187,15,10,'rgba(0, 23, 61, 0.3)'],
[172,19,17  ,'rgba(0, 208, 255, 0.3)'],
[3246,18,13,'rgba(252, 209, 22, 0.3)'],
[46,1,25,'rgba(16, 150, 9, 0.3)'],
[1108,1,0,'rgba(16, 150, 9, 0.3)'],
[1826,11,0,'rgba(237, 28, 36, 0.3)'],
[5500,19,20,'rgba(0, 208, 255, 0.3)'],
[220,1,20,'rgba(16, 150, 9, 0.3)'],
[10136,11,20,'rgba(237, 28, 36, 0.3)'],
[1070,1,16,'rgba(16, 150, 9, 0.3)'],
[3000,11,16,'rgba(237, 28, 36, 0.3)'],
[1534,18,4,'rgba(252, 209, 22, 0.3)'],
[238,11,7,'rgba(237, 28, 36, 0.3)'],
[589,18,7,'rgba(252, 209, 22, 0.3)'],
[931,11,18,'rgba(237, 28, 36, 0.3)'],
[1539,12,18,'rgba(255, 0, 0, 0.3)'],
[9,14,12,'rgba(255, 0, 0, 0.3)'],
[1818,27,15,'rgba(6, 51, 3, 0.3)'],
[1500,19,15,'rgba(0, 208, 255, 0.3)'],
[171,11,15,'rgba(237, 28, 36, 0.3)'],
[4800,18,15,'rgba(252, 209, 22, 0.3)'],
[199,23,15,'rgba(20, 3, 168, 0.3)'],
[797,12,15,'rgba(255, 0, 0, 0.3)'],
[508,4,21,'rgba(122, 122, 122, 0.3)']
]

nodes_headers = nodes.pop(0)
nodes_df = pd.DataFrame(nodes, columns = nodes_headers)
links_headers = links.pop(0)
links_df = pd.DataFrame(links, columns = links_headers)

data_trace = dict(
    type='sankey',
    domain = dict(
      x =  [0,1],
      y =  [0,1]
    ),
    orientation = "h",
    valueformat = ".0f",
    node = dict(
      pad = 10,
      thickness = 30,
      line = dict(
        color = "black",
        width = 0
      ),
      label =  nodes_df['Label'],
      color = nodes_df['Color']
    ),
    link = dict(
      source = links_df['source'],
      target = links_df['target'],
      value = links_df['sum'],
      color = links_df['link_color'],
  )
)

layout =  dict(
    title = "foobar",
    height = 772,
    width = 1000,
    font = dict(
      size = 12
    ),    
)

fig = dict(data=[data_trace], layout=layout)
# this plots the figure, but doesn't create a file
iplot(fig, filename='sankey.html',validate=False)

# this returns an error
fig.write_html("sankey.html")

错误:

AttributeError: 'dict' object has no attribute 'write_html'

只需更改:

fig = dict(data=[data_trace], layout=layout)

至:

fig = go.Figure(dict(data=[data_trace], layout=layout))

你应该可以开始了。

在我的工作目录中生成以下内容: