> For the complete documentation index, see [llms.txt](https://larhues-personal-organization.gitbook.io/intro-to-data-visualization/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://larhues-personal-organization.gitbook.io/intro-to-data-visualization/exercises-and-examples/the-gapminder-dataset/exporting-a-static-image.md).

# 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:

```python
!pip install -U kaleido
!pip install plotly
```

```python
import kaleido
import plotly
import plotly.express as px
import plotly.io as pio
```

```python
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.
