• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

How To Add Dollar Sign on Axis Ticks in Matplotlib

datavizpyr · May 24, 2021 ·

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
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
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
Barplot with dollar sign on axis ticks

Related posts:

How To Adjust Axes Label Position in Matplotlib?How To Adjust Positions of Axis Labels in Matplotlib? Combine Two plots into one in SeabornHow to Combine Two Seaborn plots with Shared y-axis Connect Paired Points with Lines in MatplotlibHow To Connect Paired Data Points with Lines in Scatter Plot with Matplotlib Matplotlib Scatter plot with legendHow to Add Legend to Scatterplot Colored by a Variable with Matplotlib in Python

Filed Under: add dollar sign Matplotlib, Matplotlib, Python Tagged With: Matplotlib, Python

Primary Sidebar

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version