我正在使用
plotly
为我的网站创建绘图。然而,当我将绘图保存为 html 以将其注入网站时,包含绘图的 div
比绘图本身大得多。从浏览器检查看如下:
有没有办法将 div 限制为(或多或少)绘图的实际大小?
代码:
import plotly.graph_objects as go
fig = go.Figure()
fig.update_layout(ternary_sum=100, showlegend=False, ternary_aaxis_linecolor='black', ternary_aaxis_linewidth=2.5,
ternary_baxis_linecolor='black', ternary_baxis_linewidth=2.5, ternary_caxis_linecolor='black',
ternary_caxis_linewidth=2.5)
fig.add_trace(go.Scatterternary({
'mode': 'markers',
'a': [int(round(20,0))],
'b': [int(round(50,0))],
'c': [int(round(30,0))],
'opacity': 1,
'marker': {'color': '#FFFFFF', 'size': 24, 'line': {'width': 2.2, 'color': 'black'}},
'visible': True, }))
fig.show()
fig.write_html('test_ternary_plot.html')
根据@r-beginners 评论,边距调整为:
fig.update_layout(height=300,width=300, margin=dict(t=40,b=40,l=0,r=0))
MWE:
import plotly.graph_objects as go
fig = go.Figure()
fig.update_layout(ternary_sum=100, showlegend=False, ternary_aaxis_linecolor='black', ternary_aaxis_linewidth=2.5,
ternary_baxis_linecolor='black', ternary_baxis_linewidth=2.5, ternary_caxis_linecolor='black',
ternary_caxis_linewidth=2.5)
fig.add_trace(go.Scatterternary({
'mode': 'markers',
'a': [int(round(20,0))],
'b': [int(round(50,0))],
'c': [int(round(30,0))],
'opacity': 1,
'marker': {'color': '#FFFFFF', 'size': 24, 'line': {'width': 2.2, 'color': 'black'}},
'visible': True, }))
fig.update_layout(height=300,width=300, margin=dict(t=40,b=40,l=0,r=0))
fig.show()