How To Add Dollar Sign on Axis Ticks in Matplotlib

Barplot with dollar sign on axis ticks
Barplot with dollar sign on axis ticks

In this tutorial, we will see examples of how to add dollar sign on axis ticks while making plots with Matplotlib in Python. While making plots to understand the relationship between currency like US dollars and other variables, it is better to show the Dollar symbol/sign on the axis ticks instead of numbers.

import matplotlib.pyplot as plt

Let us create some data to make a barplot plot with average salary in USD for different educational qualifications.

education=["Bachelor's", "Less than Bachelor's","Master's","PhD","Professional"]
salary = [110000,105000,126000,144200,95967]

Horizontal Barplots with barh() function in Matplotlib

Here we make horizontal barplot using Matplotlib pyplot’s barh() function with salary in USD on x-axis. We use Matplotlib’s setp() function to set the properties of the plot and use set() function to set the axis labels and title.

fig, ax = plt.subplots(figsize=(8, 6))
# horizontal barplot with barh()
ax.barh(education, salary)
# get axis tick labels
labels = ax.get_xticklabels()
# rotate the tick labels
plt.setp(labels,
         rotation=45, 
         horizontalalignment='right')
# set axis labels and title
ax.set(xlim=[-10, 150000], 
       xlabel='Average Salary', ylabel='Education',
       title='Educational Qualification and Salary')
plt.savefig("barplot_with_Matplotlib_Python.png")

Barplot with Matplotlib

Add Dollar Sign to Axis Ticks in Matplotlib

We can add dollar symbol to the salary values on x-axis using set_major_formatter() function in Matplotlib on the axis of interest. In this example, since we want to add dollar sign to x-axis ticks, we use xaxis.set_major_formatter() with the argument for formatting string.

fig, ax = plt.subplots(figsize=(8, 6))
# horizontal barplot with barh()
ax.barh(education, salary)
labels = ax.get_xticklabels()
plt.setp(labels, 
         rotation=45, 
         horizontalalignment='right')
ax.set(xlim=[0, 150000], 
       xlabel='Average Salary', 
       ylabel='Education',
       title='Educational Qualification and Salary')
ax.xaxis.set_major_formatter('${x:1.0f}')
plt.savefig("barplot_with_dollar_ticks_formating_on_x_axis_Matplotlib_Python.png")

Now we have dollar sign on x-axis of the barplot.

Add Dollar Sign in Barplot Matplotlib

Customize Dollar Sign on Axis Ticks in Matplotlib

In the above plot we simply formatted the x-axis using Dollar sign at the front. Often it will be more useful to shorten the axis tick labels to easily readable format, like 100K instead of $100000 or 1M instead of 10000000. Thanks Matplotlib documentation page we have a small function to customize by converting the dollar amount to the right format.

def currency(x, pos):
    """The two args are the value and tick position"""
    if x >= 1e6:
        s = '${:1.1f}M'.format(x*1e-6)
    else:
        s = '${:1.0f}K'.format(x*1e-3)
    return s

Instead of the formating the string in simple way, now we can use the above function as argument to set_major_formatter() to add dollar sign and make it more easy to read the labels.

fig, ax = plt.subplots(figsize=(8, 6))
# horizontal barplot with barh()
ax.barh(education, salary)
labels = ax.get_xticklabels()
plt.setp(labels, 
         rotation=45, 
         horizontalalignment='right')
ax.set(xlim=[0, 150000], 
       xlabel='Average Salary', 
       ylabel='Education',
       title='Educational Qualification and Salary')
ax.xaxis.set_major_formatter(currency)
plt.savefig("barplot_with_dollar_symbol_on_x_axis_ticks_Matplotlib_Python.png")
Barplot with dollar sign on axis ticks
Exit mobile version