Save figure as image with preserved zoom and pan

Hi,

I’m trying to save figure after some interaction like zooming and panning.
The problem is that fig.write_image("myimage.png") writes image with default view settings.

Is there a way to keep zoom and pan when exporting figure as image?

Hi @kerim ,

You can set the xaxis range and yaxis range manually to perform zoom before exporting using write_image method.

Suppose your normal xaxis range is from 0 to 10 and your yaxis range from 0 to 100.
You can set xaxis and yaxis range between these values manually

...

fig.update_xaxes(dict(range=(2.5,4)))
fig.update_yaxes(dict(range=(50,60)))

fig.write_image("myimage.png")
1 Like

@farispriadi thank you for the hint.

Seems like a possible workaround.
But is it possible to get current axes ranges? because we don’t know what limits to set after user’s manipulations.

That’s possible with dash, IDK about plotly though. Would have to be done in JS.

2 Likes

@AIMPED thank you for the hint!

Is it possible to integrate that to pure python?

:upside_down_face:

1 Like

Ahaha sorry, can’t undertand the meaning of the second part of the sentance :grinning:
I guess the answer is: yes?)

What you want to do is possible with dash. In plotly, just using python, I would not know how to do that. I actually believe you can’t do it in plotly python as the interactivity happens in the browser → JS.

1 Like

I see now… sad but ok, good to know!

1 Like