Python Charts

Python plotting and visualization demystified

Export Plotly Charts as Static Images with Kaleido

Export Plotly charts to static PNG, JPEG, SVG, or PDF files using Kaleido.

TL;DR

Install Kaleido and call fig.write_image(...) or fig.to_image(...) to export Plotly charts as static files without opening a browser.

import plotly.graph_objects as go

fig = go.Figure(data=[go.Bar(x=['A', 'B', 'C'], y=[3, 4, 5])])
fig.write_image('plotly-chart.png')

Plotly chart exported with Kaleido

Install Kaleido

Kaleido is Plotly's recommended static image export engine. Install it with:

pip install kaleido

If you use plotly version 5.x or newer, Kaleido is often installed automatically, but it is still the safest way to generate files offline.

Export a PNG file with fig.write_image()

Use fig.write_image('chart.png') to save a static PNG file directly from a Plotly figure.

import plotly.graph_objects as go

fig = go.Figure(data=[go.Bar(x=['A', 'B', 'C'], y=[3, 4, 5])])
fig.write_image('plotly-static-image.png')

Export different file formats

Kaleido supports PNG, JPEG, SVG, PDF, and more. Pass a filename with the desired extension.

import plotly.graph_objects as go

fig = go.Figure(data=[go.Bar(x=['A', 'B', 'C'], y=[3, 4, 5])])
fig.write_image('plotly-static-image.svg')
fig.write_image('plotly-static-image.pdf')

Generate image bytes with fig.to_image()

If you need raw image bytes to send to a web service, save to a buffer, or attach to a report, use fig.to_image().

import plotly.graph_objects as go

fig = go.Figure(data=[go.Bar(x=['A', 'B', 'C'], y=[3, 4, 5])])
image_bytes = fig.to_image(format='png')
with open('plotly-static-image.png', 'wb') as f:
    f.write(image_bytes)

Use kaleido with Plotly Express

Plotly Express figures also support the same image APIs.

import plotly.express as px

df = px.data.gapminder().query('year == 2007').head(5)
fig = px.bar(df, x='country', y='gdpPercap')
fig.write_image('plotly-express-static.png')

When to use static exports

Static Kaleido images are ideal for:

  • reports and slide decks
  • email attachments
  • PDFs and printed output
  • notebook previews without browser interaction

Troubleshooting

If fig.write_image() raises an error, check that Kaleido is installed and the Plotly version is compatible. On some systems, reinstalling kaleido or upgrading Plotly fixes the issue.

Summary

  • Install Kaleido with pip install kaleido.
  • Use fig.write_image('chart.png') for offline PNG export.
  • Use fig.to_image() when you need raw bytes.
  • Export SVG/PDF by changing the output filename extension.
  • Plotly Express uses the same API as graph objects.