plotly

Plotly Set Transparent Background Color

Plotly is one of Python’s most helpful plotting libraries and other languages. It provides versatile plotting options for various plots and data types. It also offers a great deal of customization for all kinds of plots with the use of simple functions.

In this article, we will discuss how you can set a transparent background color for your plots.

Plotly Express Set Transparent Color

Let us learn how we can set a transparent background using Plotly Express. For this tutorial, we will use a simple pie chart, as shown in the following code:

import plotly.express as px
label = ["Oracle", "MySQL", "SQL Server", "PostgreSQL", "MongoDB", "Redis", "Elasticsearch", "SQLite"]
values = [1266.89, 1206.05, 944.81, 606.56, 488.57, 177.98, 160.75, 127.43]
fig = px.pie(values=values, labels=label)
fig.show()

The previous example code creates a pie chart of the most popular database systems in the world.

The resulting figure is shown below:


To create a transparent background, we can use the update_layout() function and bass the plot_bgcolor and paper_bgcolor transparent values.

Consider the example code shown below:

fig.update_layout({
    'plot_bgcolor': 'rgba(0,0,0,0)',
    'paper_bgcolor': 'rgba(0,0,0,0)'
})
fig.show()

In the previous example code, we use the update_layout() function and pass the required properties as a dictionary.

Use the RGBA color scheme as it allows you to specify the alpha parameter when passing the color.

The previous code should return a figure as shown below:

Plotly Graph_Objects Set Transparent Background Color

One of the best ways to customize your plots is using Plotly’s graph_objects class. It allows you to specify a wide range of properties with custom values.

We can use this to create a plot with a transparent background.

For example, we can create a pie chart using the graph_objects.Pie class as shown in the code below:

import plotly.graph_objects as go
label = ["Oracle", "MySQL", "SQL Server", "PostgreSQL",
         "MongoDB", "Redis", "Elasticsearch", "SQLite"]
values = [1266.89, 1206.05, 944.81, 606.56, 488.57, 177.98, 160.75, 127.43]
fig = go.Figure(
    data=[
        go.Pie(
            values=values,
            labels=label
        )
    ]
)
fig.show()

In this case, we use the graph_objects to plot a Pie chart with the specified data.

To create a plot with a transparent background, we need to specify the plot_bgcolor and paper_bgcolor properties as a dictionary.

An example code is shown below:

layout = dict(plot_bgcolor='rgba(0,0,0,0)', paper_bgcolor='rgba(0,0,0,0)')
fig = go.Figure(
    data=[
        go.Pie(
            values=values,
            labels=label
        )
    ], layout=layout
)
fig.show()

Here, we define a variable called a dictionary holding two key-value pairs. This allows us to set the background color to transparent using the layout parameter of the go.Pie() class.

The resulting figure is shown below:


You can also use the update_layout function when working with graph_objects to update the plots.

Conclusion

In this tutorial, we explored how to set a transparent background for Plotly’s plots using Plotly Express and Plotly graph_objects. Examples were provided to understand these two methods better.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list