如何解决将绘图图形保存到 html 文件后,您可以稍后将其作为图形重新阅读吗?
我想编辑数据,可能会在图表中添加更多轨迹。 我找到了一种将 html 文件显示为图形的方法,但不能对其进行编辑。
from IPython.display import HTML
HTML(filename='file_name.html')
解决方法
通常 Plotly JSON 应该用于图表的(反)序列化(例如 figure.write_json
和 plotly.read_json
)。但是,如果您只有图表的 HTML 表示形式,则相同的 Plotly JSON 会被馈送到 plotly.js
那里,并且可以提取出来。
仅用于演示。 plotly==5.1.0
已使用。
import json
import re
import plotly.express
def write():
fig = plotly.express.bar(y=[0,1,2,3,5,8,13,21,34])
fig.write_html('plot.html',full_html=True)
fig.write_json('plot.json')
def read_from_json():
return plotly.io.read_json('plot.json')
def read_from_html():
with open('plot.html') as f:
html = f.read()
call_arg_str = re.findall(r'Plotly\.newPlot\((.*)\)',html[-2**16:])[0]
call_args = json.loads(f'[{call_arg_str}]')
plotly_json = {'data': call_args[1],'layout': call_args[2]}
return plotly.io.from_json(json.dumps(plotly_json))
if __name__ == '__main__':
write()
read_from_json()
read_from_html()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。