Exporting a Static Image
Exporting a static image from a Plotly Express visualization allows you to save your plots as high-quality files for use in presentations, reports, or publications. Plotly supports exporting images in various formats such as PNG, JPEG, SVG, and PDF. To do this, you need to install the kaleido
library, which serves as a fast and efficient image export engine. After creating a Plotly figure, use the .write_image()
method to save it to a specified file path. For example, after creating a chart (fig
), you can save it as fig.write_image("plot.png")
. You can also adjust the resolution and size by specifying parameters like width
, height
, and scale
. This functionality ensures that your interactive Plotly visualizations can be seamlessly incorporated into static documents or shared in non-interactive formats. Here’s an example:
!pip install -U kaleido
!pip install plotly
import kaleido
import plotly
import plotly.express as px
import plotly.io as pio
import plotly.express as px
# Sample data and visualization
df = px.data.gapminder()
fig = px.scatter(
df[df['year'] == 2007],
x='gdpPercap',
y='lifeExp',
size='pop',
color='continent',
title='Life Expectancy vs GDP per Capita (2007)',
log_x=True
)
# Save the figure as a static image
fig.write_image("scatter_plot.png", width=800, height=600, scale=2)
Ensure you have kaleido
installed (pip install -U kaleido
) before using the export functionality.
Last updated