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

Viewed 146

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), ' ')))
1 Answers

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