Search code examples
matplotlibtextformat

Formatting Currency in Matplotlib


ax.bar_label(rects2, padding=3, fmt='$%.2f')

Able to get the '$' sign and two decimal places, but can't seem to add the separator.

Tried:

labels=[f'${x:,.2f}']

Solution

  • The code below is illustrating how to format numbers as currency labels with thousands separator with 2 decimals:

    import matplotlib.pyplot as plt
    fig, ax = plt.subplots()
    langs = ['A', 'B', 'C', 'D', 'E']
    students = [2300,1700,3500,2900,1200]
    bars = ax.bar(langs,students)
    ax.bar_label(bars, labels=[f'${x:,.2f}' for x in bars.datavalues])
    plt.show()
    

    Output:

    enter image description here