Export plotly Graph as PNG, JPEG & HTML in Python (3 Examples)


Hi! This tutorial will show you how to export plotly graphs as PNG, JPEG, and HTML files in the Python programming language.

Here is an overview:

Let’s jump into the Python code!


Install & Import plotly

Here, we will install and import Python’s plotly library so that we can have access to its plot-building functions.

We will also need to install kaleido, which is the engine that will be used to generate static images from plotly graphs. If you do not install keleido in your environment, the system will throw an error.

So, in your Python programming IDE, run the lines of code below to install and import plotly into your Python programming environment:

# install plotly
pip install plotly==5.3.1
pip install -U kaleido
# import plotly
import plotly.express as px

You would notice that we installed plotly version 5.3.1. This is because that version of plotly is compatible with the kaleido engine.

So, with plotly installed and imported, we can now use it to create interactive visualizations. But first, we need data to visualize.

Create Example Dataset

We will make use of the tips dataset, which comes pre-loaded in plotly. To load and preview the first 10 rows of the dataset, run the lines of code below:

# load dataset
df = px.data.tips()
# preview dataset
#   total_bill   tip     sex smoker  day    time  size
#0       16.99  1.01  Female     No  Sun  Dinner     2
#1       10.34  1.66    Male     No  Sun  Dinner     3
#2       21.01  3.50    Male     No  Sun  Dinner     3
#3       23.68  3.31    Male     No  Sun  Dinner     2
#4       24.59  3.61  Female     No  Sun  Dinner     4
#5       25.29  4.71    Male     No  Sun  Dinner     4
#6        8.77  2.00    Male     No  Sun  Dinner     2
#7       26.88  3.12    Male     No  Sun  Dinner     4
#8       15.04  1.96    Male     No  Sun  Dinner     2
#9       14.78  3.23    Male     No  Sun  Dinner     2

Now that we have loaded our dataset, we can build our interactive plotly visualization.

Build Plot

We will build a simple box plot to visualize the tipping habits of male and female diners on different days of the week.

Therefore, run the code below to build the box plot:

fig = px.box(df, x = "sex", y = "tip", color = "day")

In the above example, to create the box plot, we simply passed the DataFrame to the px.box() method and set the x = argument to sex and the y = argument to tip. We also colored the boxes by day.

With the visualization created, let us now examine how to export it in different formats.

Example 1: Export plotly Graph as PNG

In this first example, we will export the box plot as a PNG file:

import os


static png box plot


Here, we, first of all, created an empty directory or folder called “files” where we will store our plot images using os.mkdir() method. Then, we wrote the box plot as a PNG file to that directory using the fig.write_image() method.

You can now check the directory, and there you will find the PNG file.


Example 2: Export plotly Graph as JPEG

In this second example, we will export the box plot as JPEG file:



Static jpeg box plot


You would notice that the code is virtually the same as in the previous example. The only difference is that this time, we used the .jpg extension in the file name of the plot we exported to the folder.

That extension ensures that the plot is exported as a JPEG file.

Example 3: Export plotly Graph as HTML

In this final example, we will export the graph as a HTML file:


Now, if you check inside the directory, you should also see the HTML file in there.

Video, Further Resources & Summary

Do you need more explanations on how to export a plotly graph as a PNG, JPEG, and HTML file in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain how to export a plotly graph as a PNG, JPEG, and HTML file in Python.


The YouTube video will be added soon.


You can check out these other articles for more detailed examples and videos of these popular charts in plotly using the Python programming language:

This post has shown how to export a plotly graph as a PNG, JPEG, and HTML file in Python. In case you have further questions, you may leave a comment below.


R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.