I have this Plotly graph, and I’m trying to save it as a png or JPEG file
import plotly.graph_objects as go
import numpy as np
np.random.seed(1)
N = 100
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
sz = np.random.rand(N) * 30
fig = go.Figure()
fig.add_trace(go.Scatter(
x=x,
y=y,
mode="markers",
marker=go.scatter.Marker(
size=sz,
color=colors,
opacity=0.6,
colorscale="Viridis"
)
))
fig.show()
I want to save this fig as an image
>Solution :
You can do that by using the .write_image()
function; by this function, you will be able to write your image as PNG/JPEG/WEBP or as a pdf, even and many other type
as png: fig.write_image("fig1.png")
as jpeg : fig.write_image("images/fig1.jpeg")
The plotly.io.write_image
function is used to write an image to a file or file-like python object. You can also use the .write_image
graph object figure method.