Customize Layout of plotly Graph in Python (3 Examples)
Hi! This tutorial will show you how to modify the layout of a plotly graph in the Python programming language.
Here is a quick overview:
Let’s get into the Python code!
Install & Import plotly
To install and import the Python plotly package, run the lines of code below in your preferred Python programming IDE:
# install plotly pip install plotly # import plotly import plotly.express as px
Now that we have installed and imported plotly into our Python programming environment, we can access its plot-building functions.
However, we need a sample dataset.
Create Sample Dataset
We will make use of the popular tips dataset that is shipped with the Python plotly package.
To load and preview the first 10 rows of the dataset, run the lines of code below:
# load dataset df = px.data.tips() # preview first 10 rows 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
With the sample dataset loaded, we can now build a basic plot whose layout we will customize in this tutorial.
Example 1: Build Plotly Plot
In this first example, we will build a basic histogram plot that will look at the total tips given by male and female diners on different days of the week.
fig = px.histogram(df, x = "day", y = "tip", color = "sex") fig.show()
In the above example, we simply passed the DataFrame df
to the px.histogram() function and defined the x and y arguments as day
and tip
respectively.
Then we colored the graph by sex
and displayed it using fig.show().
Let us now make some adjustments to the plot to enhance its appearance.
Example 2: Style Plotly Plot
In this second example, we will add some styling to the plot in order to improve its appearance:
fig = px.histogram(df, x = "day", y = "tip", color = "sex", title = "Sum of Tips Given by Male & Female Diners Each Day", labels = {"sex": "Gender", "day": "Day of the Week"}, category_orders = {"day":["Thurs", "Fri", "Sat", "Sun"],"sex":["Male", "Female"]}, color_discrete_map = {"Male":"#0174c3","Female":"#7ebaeb"}, template = "simple_white") fig.show()
As you can see, the plot now looks more visually appealing as we have made some adjustments to its layout.
First, we added a title to the plot, then we changed the labelling of the plot’s x axis and legend by passing a dictionary of the old and replacement labels as key-value pairs to the labels =
argument.
Next, we reordered the categories of the labels by passing a dictionary containing the new order of the x axis labels and legend to the category_orders =
argument.
Then we again passed a dictionary, wherein we assigned discrete colors to the plot, to the color_discrete_map =
argument.
Finally, we gave the plot a white background by defining the template =
as simple_white
.
Example 3: Update Plotly Plot Layout
In this last example, we will update the plot’s layout to further customize its appearance:
fig = px.histogram(df, x = "day", y = "tip", color = "sex", title = "Sum of Tips Given by Male & Female Diners Each Day", labels = {"sex": "Gender","day":"Day of the Week"}, category_orders = {"day":["Thurs","Fri","Sat","Sun"],"sex":["Male","Female"]}, color_discrete_map = {"Male":"#0174c3","Female":"#7ebaeb"}, template = "simple_white") fig.update_yaxes(tickprefix = "$", showgrid = True) fig.update_layout( font_family = "Sitka Small", legend = dict(title = None, orientation = "h", y = 1, yanchor="bottom", x = 0.5, xanchor ="center") ) fig.show()
Here, we used the update_yaxes() method to add the dollar ($
) tick prefix to the y axis values and to display the plot grid with showgrid = True
.
Then, we updated the plot layout using the update_layout() method wherein we set the plot font family as Sitka Small
and changed the orientation of the plot’s legend to horizontal and situated it at the top of the plot using its x and y coordinates.
Now the plot looks totally different from the basic plot we built in the first example. You can play around with the parameters to add even more customization to your plot.
Video, Further Resources & Summary
Do you need more explanations on how to customize the layout of a 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 in some more detail how to customize the layout of a 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:
- How to Draw a plotly Barplot in Python (Example)
- plotly Table in Python (4 Examples)
- plotly Bubble Chart in Python (3 Examples)
- plotly Contour Plot in Python (4 Examples)
- plotly Map in Python (3 Examples)
- Introduction to Python Programming
This post has shown how to customize the layout of a plotly graph in Python. I hope you enjoyed reading this tutorial! 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.