Customize Colors in plotly Graph in Python (3 Examples)
Hi! This tutorial will show you how to set the colors of a plotly graph in the Python programming language.
Here is an overview:
Let’s jump into the Python code!
Install & Import plotly
To install and import the Python plotly library, run the lines of code below in your preferred Python programming IDE:
# install plotly pip install plotly # import plotly import plotly.express as px
With plotly installed and imported into our Python programming environment, we can now build a graph whose colors we will customize.
First, though, we need data to visualize.
Load Example Dataset
Here, we will load the tips dataset that comes pre-loaded in plotly.
So, run the lines of code below to load and preview the first 10 rows of the dataset:
df = px.data.tips() print(df.head(10)) # 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 example dataset, we can now build and customize our interactive plotly visualization.
Example 1: Build Basic Plot
In this first example, we will build a basic box plot that will visualize the tipping habits of male and female diners on particular meals.
Therefore, run the lines of code below to create the box plot:
fig = px.box(df,x = "sex", y = "tip", color = "time") fig.show()
In the above example, we have built a simple box plot to visualize male and female diners’ tipping habits.
We used the px.box() function wherein we passed the DataFrame df
and then defined the X
, Y
and color
arguments as sex
, tip
, and time
respectively.
Example 2: Customize Colors of Plot
In this next example, we will change the default colors:
fig = px.box(df, x = "sex", y = "tip", color = "time", color_discrete_map = {"Dinner": "#008000", "Lunch": "#6600ff"}) fig.show()
Here, we have customized the colors of the boxes by the time
column in the DataFrame df
.
We did this by simply passing a dictionary containing key-value pairs of time and hex color codes to the color_discrete_map =
argument in the px.box()
function.
However, there is still more customization that can be done to the graph.
Example 3: Customize Theme of Plot
In this last example, we will show how to change the theme of the plot:
fig = px.box(df, x = "sex", y = "tip", color = "time", color_discrete_map = {"Dinner": "#008000", "Lunch": "#6600ff"}, template = "plotly_dark") fig.show()
As you can see in the above example, we have changed the theme of the box plot. To do so, we introduced the template =
argument in px.box()
, and defined it as "plotly_dark"
to give a dark appearance to the box plot.
There are other examples you can play with, and you can find them here.
It is also good to mention that the customizations we have demonstrated in this tutorial can be applied to other plotly graphs too. So, feel free to experiment with them in your own projects.
Video, Further Resources & Summary
Do you need more explanations on how to customize colors in plotly graph 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 customize colors in plotly graph 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:
- plotly Map in Python (3 Examples)
- plotly Heatmap in Python (3 Examples)
- plotly Sunburst Chart in Python (4 Examples)
- plotly Area Chart in Python (5 Examples)
- plotly Pie & Donut Chart in Python (4 Examples)
- Introduction to Python Programming
This post has shown how to customize colors in plotly graph in Python. In case you have further questions, you may leave a comment below.
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.
Statistics Globe Newsletter