Change plotly Axis Labels in Python (Example)

 

Hi there! In this tutorial I will show you how to change the axis labels of your interactive plotly visualization in the Python programming language. It is simple and easy to do.

First, let us see what to expect in this article:

Now, let’s get started!

 

Install & Load the Python plotly Library

To install the Python plotly library, in your preferred IDE, run the following code:

pip install plotly

This will install the plotly library along with all its dependencies. Next, you will need to load the library in your IDE using the following code:

import plotly.express as px

This will import the functions that can be used to create different visualizations in plotly, including the one we will use to create the scatterplot. Now, let’s create a scatterplot in plotly.

 

Create a Scatterplot

We are going to use the tips dataset that comes with the Python plotly library. In order to load the dataset, run the following line of code:

df = px.data.tips()

You can then print out the data frame to take a look like so:

print(df)
       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
..          ...   ...     ...    ...   ...     ...   ...
239       29.03  5.92    Male     No   Sat  Dinner     3
240       27.18  2.00  Female    Yes   Sat  Dinner     2
241       22.67  2.00    Male    Yes   Sat  Dinner     2
242       17.82  1.75    Male     No   Sat  Dinner     2
243       18.78  3.00  Female     No  Thur  Dinner     2
 
[244 rows x 7 columns]

Now, we are going to create a scatterplot, where we will plot the “total_bill” column against the “tip” column, and color the points by the “sex” column. Run the code below to create the scatterplot:

fig = px.scatter(df,x = "total_bill",y = "tip",color = "sex")
fig.show()




As you can see, “total_bill”, in the X axis, is plotted against “tip”, in the Y axis, and the plot is interactive, because you can hover over the plot to see the information in each data point, and to bring up other widgets that you can also interact with.
 

Change the Axis Labels of a plotly Plot

Now that we have created the scatterplot, let’s now see how to change the axis labels to whatever we want. In the code below, we shall change the X axis label “total_bill” to “Bill in dollars ($)” and the Y axis label “tip” to “Tip in dollars ($)”.

fig = px.scatter(df,
                 x = "total_bill",
                 y = "tip",
                 color = "sex",
                 labels = dict(total_bill = "Bill in dollars ($)",tip = "Tip in dollars ($)"))
fig.show()




And that is how to change the axis labels of your plotly plot in Python. As you can see, it takes only a few lines of Python code to create an interactive scatterplot and to set its axis labels.

Thanks for reading, and I hope to see you in the next tutorial!
 

Video, Further Resources & Summary

Do you need more explanations on how to create interactive visualizations using plotly 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 create an interactive plotly bar plot.

 

The YouTube video will be added soon.

 

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

This post has shown how to change the labels of the axes of a plot in plotly using Python. 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.

Menu
Top