Change dot by comma in pie chart matplotlib

Viewed 200

I'm trying to do a pie chart with the following code:

import plotly.graph_objects as go
import matplotlib.pyplot as plt

import matplotlib.ticker as mtick
import locale
locale.setlocale(locale.LC_NUMERIC, "de_DE")
plt.rcParams['axes.formatter.use_locale'] = True

labs =  np.array(['Desiguais', 'Dinâmicos', 'Equitativos', 'Em Transição', 'Vulneráveis'])
d_pop = np.array([43.64, 33.96, 9.84, 8, 4.55])
cols =["#C03A51FF", "#E65D2FFF", "#FA9008FF", "#F9CB35FF", "#FCFFA4FF"]
explode = np.ones(len(cols))/20

fig1, ax1 = plt.subplots(figsize=(10,8))
ax1.pie(d_pop, explode=explode, labels=labs, colors=cols, autopct='%1.1f%%',
        shadow=True, startangle=90, pctdistance=0.85,  textprops={'fontsize': 14});

I'm obtained the following pie chart:enter image description here

But I need replace dot by comma in labels, example: 4.6% replace by 4,6%.

I appreciate any help.

2 Answers

The issue is that the format-string in the autopct keyword argument doesn't take locale into account. One option would be to replace this with a callable, for example a lambda function:

autopct=lambda x: locale.format_string('%1.1f%%', x)

Which gives the required labels:

enter image description here

Rather than considering locale, one option is just to replace the . with ,.

_,_,autotexts = ax1.pie(d_pop, ...)

for autotext in autotexts:
    autotext.set_text(autotext.get_text().replace('.', ','))
Related