Percentage as Axis Tick Labels in plotly Graph in Python (Example)
Hello again! It is nice to be here again with another interesting tutorial. This time, I will be taking you through how to set percentage as the axis ticks label in your plotly graph in the Python programming language. For the purpose of this tutorial, we shall create a simple line plot and then set percentage points as its Y axis ticks label.
But before we proceed, let us get a glimpse of the steps we shall take to accomplish our goal on this project:
Now that we have seen what we shall be doing in this tutorial, if you are ready, then I am ready. Let’s get started!
Install plotly and pandas Libraries
For the purpose of this tutorial, we will be creating a pandas DataFrame that we will visualize as a line plot with plotly. To install both plotly and pandas libraries, please run the lines of code below in your preferred Python IDE:
pip install plotly pip install pandas
Running the above lines of code will install both plotly and pandas libraries in your Python environment, along with their respective dependencies.
Import plotly and pandas Libraries
With plotly and pandas now installed, we will need to import both libraries to make use of their functions. Please run the below lines of code to import both plotly and pandas:
import plotly.express as px import pandas as pd
We now have access to the functions in both libraries.
Create DataFrame
We are now going to create a simple pandas DataFrame. In your IDE, run the following lines of code:
df = pd.DataFrame({"Year":[2015,2016,2017,2018,2019,2020], "Growth":[75,60,85,90,57,80]})
You can take a look at the DataFrame we have just created by running the following code:
print(df)
# Year Growth #0 2015 75 #1 2016 60 #2 2017 85 #3 2018 90 #4 2019 57 #5 2020 80
Build Line Plot
Let us now build a simple line plot using the plotly library. To do so, run the code below:
fig = px.line(df,x = "Year",y = "Growth") fig.show()
Cool! We have just created a simple line plot.
Set Percentage as Axis Ticks Label
In the Y axis of our plot, we have “Growth”, whose values are percentages and should be labeled as such. Therefore, we are going to correct that by setting percentages as the Y axis tick label to show that the values on that axis are percentages.
fig = px.line(df,x = "Year",y = "Growth") fig.update_layout(yaxis_ticksuffix = "%") fig.show()
As you will notice in the plot above, we have labeled the “Growth” values on the Y axis as percentages, which makes it a lot easier for a user to tell that those are growth percentages over time. All we had to do was define the yaxis_ticksuffix = "%"
argument in the fig.update_layout()
function, which is super simple to do, as you have just seen.
By properly labeling axis ticks in graphical visualizations, we can better communicate information to others, which makes it crucial that axis ticks are properly labeled.
You can play with other kinds of DataFrames, such as car speed. You can plot the cars against their respective speed and label the speed values on the Y axis as “MPH”.
So there you have it! We have set percentages as the axis tick labels of the Y axis of our line plot in plotly. If you have learned something valuable in this tutorial, then be sure to check out other tutorials on the Statistics Globe website, and I will see you in the next one.
Video, Further Resources & Summary
Do you need more explanations on how to set percentages as the axis tick labels of your plot in plotly? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain how to set percentages as plot axis tick labels in some more detail.
The YouTube video will be added soon.
Furthermore, you could have a look at some other tutorials on Statistics Globe:
- Change plotly Axis Labels in Python (Example)
- How to Draw a plotly Boxplot in Python (Example)
- How to Draw a plotly Scatterplot in Python (Example)
- Customize Legend of a plotly Graph in Python (Example)
- Format Title of plotly Graph in Python (Example)
- Introduction to Python
This post has shown how to set percentages as the axis tick labels of a plot in plotly using 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