#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
4 min read min read

Saving and Exporting

Learn to save your Plotly charts as files

Saving and Exporting

Why Save Charts?

You need to save charts for:

  • Reports and presentations
  • Sharing with others
  • Website or blog posts
  • Print materials

Save as HTML

HTML keeps all interactivity:

code.py
import plotly.express as px
import pandas as pd

df = pd.DataFrame({
    'Product': ['A', 'B', 'C'],
    'Sales': [100, 150, 80]
})

fig = px.bar(df, x='Product', y='Sales')

# Save as HTML
fig.write_html('my_chart.html')

Open HTML in browser to interact!

Save as PNG

Static image:

code.py
# Install kaleido first: pip install kaleido

fig.write_image('my_chart.png')

Save as PDF

code.py
fig.write_image('my_chart.pdf')

Save as SVG

Best for quality scaling:

code.py
fig.write_image('my_chart.svg')

Set Image Size

code.py
fig.write_image('my_chart.png', width=800, height=600)

Set Image Quality

code.py
fig.write_image('my_chart.png', scale=2)  # 2x resolution

scale=2 makes image twice as sharp!

Save Multiple Formats

code.py
# Save in all formats
fig.write_html('chart.html')
fig.write_image('chart.png')
fig.write_image('chart.pdf')
fig.write_image('chart.svg')

Get Image as Bytes

For web apps:

code.py
image_bytes = fig.to_image(format='png')

Get HTML as String

code.py
html_string = fig.to_html()

Include Plotly.js

For standalone HTML:

code.py
fig.write_html('chart.html', include_plotlyjs=True)

For smaller file (needs internet):

code.py
fig.write_html('chart.html', include_plotlyjs='cdn')

Complete Example

code.py
import plotly.express as px
import pandas as pd

# Create chart
df = pd.DataFrame({
    'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
    'Sales': [100, 120, 115, 140, 160]
})

fig = px.line(df, x='Month', y='Sales',
              title='Monthly Sales Report',
              markers=True)

fig.update_layout(
    xaxis_title='Month',
    yaxis_title='Sales ($)',
    template='plotly_white'
)

# Save for different uses
fig.write_html('sales_interactive.html')  # For web
fig.write_image('sales_report.png', width=800, height=500, scale=2)  # For presentation
fig.write_image('sales_print.pdf')  # For print

Format Comparison

FormatInteractiveFile SizeBest For
HTMLYesLargeWeb, sharing
PNGNoSmallReports, slides
PDFNoMediumPrint, documents
SVGNoSmallWeb, scaling

Troubleshooting

Error: "kaleido" not found?

terminal
pip install kaleido

Image is blurry?

code.py
fig.write_image('chart.png', scale=2)  # Use higher scale

Key Points

  • write_html() keeps interactivity
  • write_image() for static images
  • Install kaleido for image export
  • Use scale for higher resolution
  • PNG for slides, PDF for print
  • SVG for scalable graphics
  • HTML for web sharing

Module Complete!

You've learned all about Plotly:

  • Basic charts with Plotly Express
  • Scatter, line, bar charts
  • 3D visualizations
  • Geographic maps
  • Animations
  • Dashboards
  • Saving charts

Now you can create beautiful interactive visualizations!