Static document graphics
Save a chart for a report or presentation.
Plotly · Module
Exports a Plotly figure as a static PNG, JPEG, WebP, SVG, or PDF file. The output format can be inferred from the destination or supplied explicitly.
Call this Figure method with a file path or writable object. Plotly converts the figure to a static image, infers the format from the file extension unless format is supplied, and lets you control output dimensions, resolution scaling, and validation.
fig.write_image(file, format=None, scale=None, width=None, height=None, validate=True)| Name | Type | Description |
|---|---|---|
fig | Union[dict, plotly.graph_objects._figure.Figure] | Figure object or dict representing a figure. |
file | Union[str, pathlib.Path] | Local file path or writable object, such as a pathlib.Path or open file descriptor. |
format | Optional[str] | Optional output format: png, jpg/jpeg, webp, svg, or pdf; otherwise inferred from file or defaults. |
scale | Optional[Union[int, float]] | Optional export scale factor. |
width | Optional[int] | Optional exported width in layout pixels. |
height | Optional[int] | Optional exported height in layout pixels. |
validate | bool | Whether to validate the figure before image conversion; defaults to True. |
import plotly.express as px
fig = px.bar(x=["A", "B"], y=[1, 3])
fig.write_image("chart.png", width=800, height=500, scale=2)Creates a bar chart and exports it as a PNG file with explicit dimensions and higher resolution.
Save a chart for a report or presentation.
Generate raster or vector assets in an automated pipeline.
Create high-resolution PNG output for printing.
It converts a Plotly figure into a static image and writes the result to a file or writable object. The supported formats are PNG, JPEG, WebP, SVG, and PDF.
Follow-up: How would you export several figures efficiently? Use plotly.io.write_images, which is recommended for multiple figures and can perform better than repeated write_image calls.
The file argument accepts a local path, pathlib.Path, or writable object. If format is not specified, Plotly derives it from the file extension when available.
Follow-up: What happens when neither format nor a usable extension is provided? Plotly falls back to plotly.io.defaults.default_format.
width and height set the exported image's layout dimensions in pixels. scale multiplies the output resolution relative to those layout dimensions, while validate controls whether the figure is checked before conversion.
Follow-up: When would you set scale instead of width and height? Use scale when preserving the figure's layout size while requesting a higher- or lower-resolution raster export.
plotly.io.write_imageplotly.io.write_imagesplotly.io.to_imageFigure.to_image