Figure to_image with annotations

Using plotly.io.to_image(fig, format=‘png’) is there any way to have it export any annotations on the figure to the image it creates?

Hi @Inci ,

using an example from the docs, you could do:

binary_data=fig.to_image('png')

with open('image_with_annotations.png', 'wb') as f:
    f.write(binary_data)

Full code:

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[0, 1, 3, 2, 4, 3, 4, 6, 5]
))

fig.add_trace(go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[0, 4, 5, 1, 2, 2, 3, 4, 2]
))

fig.add_annotation(
        x=2,
        y=5,
        xref="x",
        yref="y",
        text="max=5",
        showarrow=True,
        font=dict(
            family="Courier New, monospace",
            size=16,
            color="#ffffff"
            ),
        align="center",
        arrowhead=2,
        arrowsize=1,
        arrowwidth=2,
        arrowcolor="#636363",
        ax=20,
        ay=-30,
        bordercolor="#c7c7c7",
        borderwidth=2,
        borderpad=4,
        bgcolor="#ff7f0e",
        opacity=0.8
        )

fig.update_layout(showlegend=False)

binary_data=fig.to_image('png')

with open('image_with_annotations.png', 'wb') as f:
    f.write(binary_data)

I am not sure, if I understood you correctly, though :upside_down_face: