Saving Plotly Express Plots as HTML and Static Image Files
Last Updated :
23 Jul, 2025
Plotly Express is a high-level interface for Plotly, making it easy to create beautiful, interactive visualizations with just a few lines of code. One of the great features of Plotly is the ability to save these plots as HTML files or static image files for sharing and publication purposes. This article will guide you through the process of saving Plotly Express plots into both HTML and static image files.
Introduction to Plotly Express
Plotly Express provides a concise and consistent API for creating a wide variety of visualizations, including scatter plots, line charts, bar charts, and more. It's particularly well-suited for quickly generating interactive plots that can be embedded in web pages or saved as standalone HTML files.
Plotly Express is part of the Plotly library, which is known for its ability to create interactive and visually appealing plots. While viewing these plots in a Jupyter notebook or a web browser is convenient, there are times when you need to save them as static images or HTML files for reports, presentations, or sharing with others.
Saving Plotly Express Plots as HTML Files
Saving your Plotly Express plots as HTML files is straightforward and allows you to retain the interactivity of the plots. Here’s how you can do it:
1. Create a Plotly Express Plot
Python
import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x='sepal_width', y='sepal_length', color='species')
2. Save the Plot as an HTML File:
Python
# Save the plot to an HTML file
fig.write_html("scatter_plot.html")
Output:
Save the Plot as an HTML FileThis will save the plot as an HTML file named scatter_plot.html
in your working directory. You can open this file in any web browser to view the interactive plot.
Saving Plotly Express Plots as Static Image Files
To save Plotly Express plots as static images, you can use the Kaleido library. Kaleido is a lightweight dependency that simplifies the process of exporting Plotly figures as static images.
Ensure you have Kaleido installed:
pip install -U kaleido
1. Create a Plotly Express Plot:
Python
import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x='sepal_width', y='sepal_length', color='species')
2. Save the Plot as a Static Image:
Python
# Save the plot as a PNG file
fig.write_image("scatter_plot.png")
# Save the plot as a JPEG file
fig.write_image("scatter_plot.jpeg")
# Save the plot as a PDF file
fig.write_image("scatter_plot.pdf")
Output:
Plot as a Static ImageCustomizing Image Export Settings
Kaleido allows you to customize various settings for image export, such as the image's width, height, and scale. Here’s how you can do it:
1. Set Default Export Settings:
import plotly.io as pio
# Set default export settings
pio.kaleido.scope.default_width = 800
pio.kaleido.scope.default_height = 600
pio.kaleido.scope.default_scale = 2
2. Export with Custom Settings:
# Save the plot with custom settings
fig.write_image("scatter_plot_custom.png", width=800, height=600, scale=2)
Combining Multiple Plots in a Single HTML File
Sometimes, you may need to save multiple plots in a single HTML file. Here's how you can achieve that:
1. Create Multiple Plots:
Python
import plotly.express as px
df = px.data.iris()
fig1 = px.scatter(df, x='sepal_width', y='sepal_length', color='species')
fig2 = px.scatter(df, x='petal_width', y='petal_length', color='species')
2. Save Multiple Plots in a Single HTML File:
Python
from plotly.subplots import make_subplots
# Create a subplot figure
fig = make_subplots(rows=1, cols=2)
# Add the first plot to the subplot
for trace in fig1.data:
fig.add_trace(trace, row=1, col=1)
# Add the second plot to the subplot
for trace in fig2.data:
fig.add_trace(trace, row=1, col=2)
# Save the subplot as an HTML file
fig.write_html("combined_plots.html")
Output:
Multiple Plots in a Single HTML FileConclusion
Saving Plotly Express plots as HTML or static image files is a valuable skill for data visualization and sharing. With the methods described in this article, you can easily save your interactive plots as HTML files or export them as high-quality static images. Whether you're preparing a report, a presentation, or simply sharing your visualizations, these techniques will ensure your plots are preserved in the desired format.