Plotly is renowned for its interactive web-based plots, but you often need to save these plots as static image files (PNG, JPEG, SVG) or as interactive HTML files for sharing or embedding. See how to save your Plotly figures to local files.
Prerequisites
To save plots as static images (PNG, JPEG, SVG, PDF), you need to install the kaleido
package. Kaleido is a fast, cross-platform library for static image export from Plotly.py.
pip install plotly kaleido
If you only need to save as HTML, kaleido is not strictly necessary, but it’s good practice to have it for image export.
1. Saving as an Interactive HTML File
Saving a Plotly figure as an HTML file preserves its interactivity (zooming, panning, hovering). This is ideal for sharing plots that can be viewed in any web browser.
import plotly.graph_objects as go
fig = go.Figure(data=go.Scatter(x=[1, 2, 3], y=[4, 1, 2]))
fig.update_layout(title_text="My Interactive Plot")
# Save as HTML
fig.write_html("my_interactive_plot.html")
print("Plot saved as my_interactive_plot.html")
The fig.write_html() method saves the figure as a standalone HTML file. You can open this file directly in your web browser.
2. Saving as Static Image Files (PNG, JPEG, SVG, PDF)
For static images suitable for reports, presentations, or print, use the fig.write_image() method. This requires the kaleido package.
Example: Saving as PNG
import plotly.graph_objects as go
fig = go.Figure(data=go.Bar(x=['A', 'B', 'C'], y=[10, 20, 15]))
fig.update_layout(title_text="My Bar Chart")
# Save as PNG
fig.write_image("my_bar_chart.png")
print("Plot saved as my_bar_chart.png")
Example: Saving as JPEG with custom size
import plotly.graph_objects as go
fig = go.Figure(data=go.Scatter(x=[1, 2, 3], y=[4, 1, 2]))
fig.update_layout(title_text="My Scatter Plot")
# Save as JPEG with specified width and height
fig.write_image("my_scatter_plot.jpeg", width=800, height=600)
print("Plot saved as my_scatter_plot.jpeg")
Example: Saving as SVG
SVG (Scalable Vector Graphics) is excellent for plots that need to scale without losing quality.
import plotly.graph_objects as go
fig = go.Figure(data=go.Pie(labels=['Apples', 'Bananas', 'Cherries'], values=[30, 40, 30]))
fig.update_layout(title_text="My Pie Chart")
# Save as SVG
fig.write_image("my_pie_chart.svg")
print("Plot saved as my_pie_chart.svg")
Example: Saving as PDF
import plotly.graph_objects as go
fig = go.Figure(data=go.Scatter(x=[1, 2, 3], y=[4, 1, 2]))
fig.update_layout(title_text="My PDF Plot")
# Save as PDF
fig.write_image("my_pdf_plot.pdf")
print("Plot saved as my_pdf_plot.pdf")
Key Parameters for fig.write_image()
file
: The path and filename for the output file (e.g., “plot.png”, “plot.pdf”). The extension determines the format.format
: (Optional) Explicitly specify the format (e.g., ‘png’, ‘jpeg’, ‘svg’, ‘pdf’). If not provided, it’s inferred from the file extension.width
: (Optional) The width of the output image in pixels.height
: (Optional) The height of the output image in pixels.scale
: (Optional) A scaling factor for the image. A scale of 2 will produce an image twice the default size (or width/height if specified). Useful for high-resolution images.
Troubleshooting kaleido Issues
If you encounter errors when using fig.write_image(), ensure that kaleido is correctly installed. Sometimes, firewall or proxy settings can interfere with kaleido’s internal processes. If issues persist, try reinstalling kaleido or checking your network configuration.