Search code examples
pythonplotlyplotly-python

Plotly: How to save only main figure to png?


This code:

fig = go.Figure(data =
go.Contour(
    z=[[10, 10.625, 12.5, 15.625, 20],
       [5.625, 6.25, 8.125, 11.25, 15.625],
       [2.5, 3.125, 5., 8.125, 12.5],
       [0.625, 1.25, 3.125, 6.25, 10.625],
       [0, 0.625, 2.5, 5.625, 10]]
))

fig.write_image("logs/test.png")

Produces this:

enter image description here

I would like only the main contour plot saved, with no x and y labels and no colorbar.

I expect that I could take a reliable sub-area of the image, but maybe there's an easier way?


Solution

  • You can use:

    fig.update_traces(showscale=False)
    fig.update_layout(yaxis={'visible': False, 'showticklabels': False},
                      xaxis={'visible': False, 'showticklabels': False})
    

    Plot

    enter image description here

    Complete code:

    import plotly.graph_objects as   go
    
    fig = go.Figure(data =
    go.Contour(
        z=[[10, 10.625, 12.5, 15.625, 20],
           [5.625, 6.25, 8.125, 11.25, 15.625],
           [2.5, 3.125, 5., 8.125, 12.5],
           [0.625, 1.25, 3.125, 6.25, 10.625],
           [0, 0.625, 2.5, 5.625, 10]]
    ))
    
    fig.update_traces(showscale=False)
    fig.update_layout(yaxis={'visible': False, 'showticklabels': False},
                      xaxis={'visible': False, 'showticklabels': False})
    fig.show()
    
    fig.write_image("contour2.png")