
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 plotly.express as px
# read data
url = "https://raw.githubusercontent.com/bprasad26/lwd/master/data/batting.csv"
df = pd.read_csv(url)
# create a figure
fig = px.histogram(df, x="Runs", color='Nationality')
fig.show()

Save a Figure –
The plotly.io.write_image
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"):
os.mkdir("images")
Save a Figure in PNG –
To save an image to a png file, run the following code
fig.write_image("images/fig1.png")
Save a Figure in Jpeg –
fig.write_image("images/fig1.jpeg")
Save a Figure in SVG –
fig.write_image("images/fig1.svg")
Save a Figure in PDF –
fig.write_image("images/fig1.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
fig.write_html("images/fig1.html")
Related Posts –
- How to install plotly python with pip?
- How to create a Line Chart with Plotly Python?
- How to create Scatter plot in Plotly Python?
- How to create a Bar Chart in Plotly Python?
- How to create Horizontal Bar Chart in Plotly Python?
- How to create a Histogram in plotly python?
- How to Create a Box Plot in Plotly Python?
- How to create a Pie Chart in Plotly Python?
- How to create a Dot Plot in Plotly Python?
- How to Create Heatmap with Plotly Python?
- How to Create a Violin Plot in Plotly Python?
- How to Create Subplots in Plotly Python?
- How to Create a Bubble Chart in Plotly Python?
- How to Create a Gantt Chart in Plotly Python?
- How to Create an Area Chart in Plotly Python?
- How to Create Tables in Plotly Python?
- How to Create a Sunburst Chart in Plotly Python?
- How to Create a Sankey Diagram in Plotly Python?
- How to Create a Treemap Charts in Plotly Python?
- How to Create Error Bars in Plotly Python?
- How to Create Distplots in Plotly Python?