我最近发现 plotly 并发现它非常适合绘图,现在我有一个问题,我想将多个 plot 保存到一个 html 中,请问该怎么做?
*我想保存多个图,即图、图 1、图 2 等,而不是一个包含多个图的子图,因为我发现子图中的图太小了。
原文由 Victor 发布,翻译遵循 CC BY-SA 4.0 许可协议
我最近发现 plotly 并发现它非常适合绘图,现在我有一个问题,我想将多个 plot 保存到一个 html 中,请问该怎么做?
*我想保存多个图,即图、图 1、图 2 等,而不是一个包含多个图的子图,因为我发现子图中的图太小了。
原文由 Victor 发布,翻译遵循 CC BY-SA 4.0 许可协议
这是一个看起来不错的示例:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.figure_factory as ff
import numpy as np
import plotly
y1 = np.random.randn(200) - 1
y2 = np.random.randn(200)
y3 = np.random.randn(200) + 1
x = np.linspace(0, 1, 200)
colors = ['#3f3f3f', '#00bfff', '#ff7f00']
fig = make_subplots(
rows=3, cols=2,
column_widths=[0.55, 0.45],
row_heights=[1., 1., 1.],
specs=[[{"type": "scatter"}, {"type": "xy"}],
[{"type": "scatter"}, {"type": "xy", "rowspan": 2}],
[{"type": "scatter"}, None ]])
fig.add_trace(
go.Scatter(x = x,
y = y1,
hoverinfo = 'x+y',
mode='lines',
line=dict(color='#3f3f3f',
width=1),
showlegend=False,
),
row=1, col=1
)
fig.add_trace(
go.Scatter(x = x,
y = y2,
hoverinfo = 'x+y',
mode='lines',
line=dict(color='#00bfff',
width=1),
showlegend=False,
),
row=2, col=1
)
fig.add_trace(
go.Scatter(x = x,
y = y3,
hoverinfo = 'x+y',
mode='lines',
line=dict(color='#ff7f00',
width=1),
showlegend=False,
),
row=3, col=1
)
boxfig= go.Figure(data=[go.Box(x=y1, showlegend=False, notched=True, marker_color="#3f3f3f", name='3'),
go.Box(x=y2, showlegend=False, notched=True, marker_color="#00bfff", name='2'),
go.Box(x=y3, showlegend=False, notched=True, marker_color="#ff7f00", name='1')])
for k in range(len(boxfig.data)):
fig.add_trace(boxfig.data[k], row=1, col=2)
group_labels = ['Group 1', 'Group 2', 'Group 3']
hist_data = [y1, y2, y3]
distplfig = ff.create_distplot(hist_data, group_labels, colors=colors,
bin_size=.2, show_rug=False)
for k in range(len(distplfig.data)):
fig.add_trace(distplfig.data[k],
row=2, col=2
)
fig.update_layout(barmode='overlay')
plotly.offline.plot(fig, filename='test.html')
#fig.show()
原文由 Essegn 发布,翻译遵循 CC BY-SA 4.0 许可协议
2 回答5.2k 阅读✓ 已解决
2 回答1.1k 阅读✓ 已解决
4 回答1.4k 阅读✓ 已解决
3 回答1.3k 阅读✓ 已解决
3 回答1.3k 阅读✓ 已解决
2 回答869 阅读✓ 已解决
1 回答1.7k 阅读✓ 已解决
在 Plotly API 中有一个函数
to_html
返回图形的 HTML。此外,您可以设置选项参数full_html=False
这将为您提供包含图形的 DIV。您可以通过附加包含图形的 DIV 将多个图形写入一个 HTML:
https://plot.ly/python-api-reference/generated/plotly.io.to_html.html
您还可以使用 Beautiful Soup 进行 DOM 操作,并将 DIV 准确插入 HTML 中您需要的位置。
https://beautiful-soup-4.readthedocs.io/en/latest/#append