Photo by Stein Egil Liland on Pexels

Formatting data for matplotlib axes

A simple but useful feature for data visualization in plots is to format the axes accoring to the units of the attributes you are working with, which could just be metric units, currency units or similar. Matplotlib makes this fairly easy actually, without having to modify your data, by importing the matplotlib.ticker class.

from matplotlib import ticker
import matplotlib.pyplot as plt
fig, axes = plt.subplots()
...
scale = 1e6  # or any other scaling factor
ticks = ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x/scale))
axes.yaxis.set_major_formatter(ticks)

The issue you need to watch out for is that the lambda function in the example above takes two arguments, because the matplotlib.ticker.FuncFormatter “take[s] two inputs (a tick value x and a position pos), and return[s] a string containing the corresponding tick label”, as described in the documents.

In a similar manner instead of using a lambda function, I also made a short function to add a prefix for numbers that are over a million, here specifically for dollar amounts, which is then fed to the same matplotlib.ticker.FuncFormatter class.

import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
fig, axes = plt.subplots()

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

formatter = FuncFormatter(currency)
axes.xaxis.set_major_formatter(formatter)