How to Save or Export a Figure in Plotly Python?

Spread the love

In this post you will learn how to save static and interactive figures or images in Plotly Python.

Static Image export in Plotly Python –

Plotly allows you to save static images of your plots. You can Save images to your local computer, or embed it inside your Jupyter notebooks as a static image.

Install Dependencies –

Before we save any figure in plotly python, we need to install the kaleido library. Which we can do using pip. Open the terminal on mac or command Prompt on windows and run the following code.

pip install -U kaleido

You can also install kaleido using conda

conda install -c conda-forge python-kaleido

Create a Figure –

Now, Let’s create a sample figure to work with.

# import libraries
import pandas as pd
import as px

# read data
url = ""
df = pd.read_csv(url)

# create a figure
fig = px.histogram(df, x="Runs", color='Nationality')

Save a Figure –

The function is used to write an image to a file or file-like python object. You can also use the .write_image graph object figure method.

Let’s first create an output directory to store our images.

import os
if not os.path.exists("images"):

Save a Figure in PNG –

To save an image to a png file, run the following code


Save a Figure in Jpeg –


Save a Figure in SVG –


Save a Figure in PDF –


Change Image dimension and Scale –

In addition to the image format, the write_image functions provide arguments to specify the image width and height in logical pixels. They also provide a scale parameter that can be used to increase (scale > 1) or decrease (scale < 1) the physical resolution of the resulting image.

fig.write_image('images/hist1.jpeg', width=600, height=350, scale=2)

Interactive HTML export in Plotly Python –

You can save any plotly figure in HTML file using the write_html method. These HTML files can be opened in any web browser to access the fully interactive figure.

To save a figure in HTML file, run the following code


Related Posts –

  1. How to install plotly python with pip?
  2. How to create a Line Chart with Plotly Python?
  3. How to create Scatter plot in Plotly Python?
  4. How to create a Bar Chart in Plotly Python?
  5. How to create Horizontal Bar Chart in Plotly Python?
  6. How to create a Histogram in plotly python?
  7. How to Create a Box Plot in Plotly Python?
  8. How to create a Pie Chart in Plotly Python?
  9. How to create a Dot Plot in Plotly Python?
  10. How to Create Heatmap with Plotly Python?
  11. How to Create a Violin Plot in Plotly Python?
  12. How to Create Subplots in Plotly Python?
  13. How to Create a Bubble Chart in Plotly Python?
  14. How to Create a Gantt Chart in Plotly Python?
  15. How to Create an Area Chart in Plotly Python?
  16. How to Create Tables in Plotly Python?
  17. How to Create a Sunburst Chart in Plotly Python?
  18. How to Create a Sankey Diagram in Plotly Python?
  19. How to Create a Treemap Charts in Plotly Python?
  20. How to Create Error Bars in Plotly Python?
  21. How to Create Distplots in Plotly Python?

Rating: 1 out of 5.

Leave a Reply