Transparent plotly Graph Background in Python (Example)

 

Hi there folks! You are welcome to another interesting tutorial on the Statistics Globe blog. In this tutorial, I will show you how to make the background of your plotly graph transparent using the Python programming language. It is very simple and easy to do, as you will soon find out.

But first, let us see the steps we will take to accomplish our goal:

Now that we have gotten a small preview of what we shall be doing, let’s dive right into it!

 

Install Python plotly Library

To use the Python plotly library, we must first install it. To do so, please run the line of code below in your preferred Python coding IDE.

pip install plotly

The above line of code will install the plotly library in your Python environment, which means we can now load and use its functions.
 

Load Python plotly Library

Having installed the Python plotly library, let us now load it and all its dependencies so that we can have access to all its plot building functions, including the one we will use to build a basic pie chart in this tutorial. Please run the line of code below in your IDE:

import plotly.express as px

 

Draw a plotly Pie Chart

Let us now draw a basic pie chart. We will make use of the pandas library to create a DataFrame, which we will make into a pie chart. You can install and load the pandas library in the same way we installed and loaded the plotly library. Run the lines of code below in your IDE:

pip install pandas
import pandas as pd

We are going to make a DataFrame of car brands and their prices, which we shall visualize as a pie chart. Run the lines of code below in your IDE:

df = pd.DataFrame({"Cars":["Toyota","Mazda","Kia","Mercedes","Volkwagen","Audi","Chevrolet"],
                   "Price":[250,530,300,850,400,670,750]})

You can take a look at the DataFrame by running:

print(df)
#       Cars    Price
#0     Toyota    250
#1      Mazda    530
#2        Kia    300
#3   Mercedes    850
#4  Volkwagen    400
#5       Audi    670
#6  Chevrolet    750

We can now visualize the DataFrame as a pie chart with plotly. Run the lines of code below to create a pie chart:

fig = px.pie(df, labels = "Cars",values = "Price",color = "Cars")
fig.update_layout(paper_bgcolor = "lightblue")
fig.show()

When you hover over the pie chart, you can see the car brands and their respective prices displayed as the plot’s tooltip. And you can also see that we have colored our pie chart background light blue. We will now make the background of our pie chart transparent, which is our objective in this tutorial.
 

Make Pie Chart Background Transparent

To make the pie chart background transparent, we will define a couple of arguments in the update_layout() function, as you will find in the code below:

fig = px.pie(df, labels = "Cars",values = "Price",color = "Cars")
fig.update_layout(paper_bgcolor = "rgba(0,0,0,0)",
                  plot_bgcolor = "rgba(0,0,0,0)")
fig.show()

To make the background of our pie chart transparent, we needed to define paper_bgcolor = "rgba(0,0,0,0)" and plot_bgcolor = "rgba(0,0,0,0)"in the update_layout().

In “rgba(0,0,0,0)”, a stands for alpha; so setting it to 0 makes the chart background transparent.

With that, we have made the background of our plotly pie chart transparent. You can apply this newfound knowledge to other kinds of visualizations made in plotly as well to add some customization to your plot.

I hope you enjoyed this tutorial, and I will see you soon in the next one!
 

Video, Further Resources & Summary

Do you need more explanations on how to make the background of plotly charts transparent using Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to make the background of plotly charts transparent using the Python programming language.

 

The YouTube video will be added soon.

 

Furthermore, you could have a look at some other tutorials on Statistics Globe:

This post has shown how to how to make the background of a plotly chart transparent using the Python programming language. 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.

Top