Home > Enterprise >  How do I format axis number format to thousands with a whitespace in matplotlib?
How do I format axis number format to thousands with a whitespace in matplotlib?

Time:10-12

To format the y-axis with comma as thousand separator I found this nice solution on StackOverflow.

fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.get_yaxis().set_major_formatter(
matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ',')))

If I want to have a whitespace as a separator instead, how would I do? Ticker doesn't like if I try:

 matplotlib.ticker.FuncFormatter(lambda x, p: format(str(x), ' ')))

CodePudding user response:

here you go

fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.get_yaxis().set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, p: '{:,}'.format(int(x)).replace(",", " ")))
  • Related