plotlyplotly-pythonplotly.js

Python plotly div size in a html


I am using plotly to create plots for my website. However, when I save the plot as html to inject it in the website, the div containing the plot is way larger than the plot itself. See below from the browser inspection:

In red rectangles, the area that I want to be remove from the div. Oversized div

In blue, the actual graph area more or less. Actual plot not taking so much space

Is there a way to constrain the div to the (more or less) actual size of the plot?

Code:

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')

Solution

  • Following @r-beginners comment, the margins are adjusted with:

    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()