Home > Net >  Formatting Currency in Matplotlib
Formatting Currency in Matplotlib

Time:11-25

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}']

CodePudding user response:

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

  • Related