Bar Chart formatting the axes with thousands separator single qoute

Question:

I would like to display a Python graph in a Power BI report. How can I format the Y axes labels. In Switzerland we have as single qoute as thousand separator. as example: 100’000.
I managed to do the formatting with a comma. But I need a single quote!!


import matplotlib.pyplot as plt
import numpy as np
import matplotlib.font_manager as font_manager
import matplotlib as mpl


labels =  dataset.cat
x = np.arange(len(labels))  # the label locatio
width = 0.35

fig, ax = plt.subplots(figsize =(45, 15))
ax2 = ax.twinx()

rects3 = ax2.bar(x + width/2 , dataset.revenue,  width,  color='#FFB700', label='revenue')
rects1 = ax.bar(x - width/2, dataset.ord, width,  color='#00A1E4',  label='east')
rects2 = ax.bar(x - width/2, dataset.bag,  width, color='#D9D9D9',  label='west', bottom=dataset.ord,)

ax.set_xticks(x)
ax.set_xticklabels(labels)

ax.yaxis.set_tick_params(pad = 5)
ax.yaxis.set_label_coords(-0.03,0.5)
ax2.yaxis.set_label_coords(1.07,0.5)
ax.xaxis.set_tick_params(pad = 7)

ax2.yaxis.set_major_formatter(mpl.ticker.StrMethodFormatter('{x:,.0f}'))

plt.show()

thank you for your ideas

Asked By: reo

||

Answers:

To sum up, using the FuncFormatter to replace the char will do the trick

ax.yaxis.set_major_formatter(mpl.ticker.FuncFormatter(lambda x, p: format(int(x), ',').replace(',',''')))
Answered By: flyakite
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.