Romain Jouin
Romain Jouin

Reputation: 4848

python - pandas - setting x ticks labels

I have a Dataframe =

from collections import OrderedDict
dico = OrderedDict({"Cisco" :54496.923851069776,
"Citrix" :75164.2973859488,
"Datacore/veritas/docker/quest" :7138.499540816414,
"Dell / EMC" : 34836.42983441935,
"HPE": 40265.33070005489,
"IBM Hard Ware / IBM services" : 220724.89293359307,
"Microsoft cloud" : 3159.7624999999994,
"Netapp":48898.21721115539,
"Nutanix / Lenovo DCG":38761.815197677075,
"Oracle/Microfocus":100877.21884162886,
"Other brands":13825.151033348895,
"VM Ware":21267.66907692287,
"Veeam / Redhat":5006.715599405339})

That I can plot :

df = pd.DataFrame(list(dico.values()))
df.index = dico.keys()
ax = df.sort(0).plot.barh()

enter image description here

but I want to format the xtick labels :

ax = df.sort_values(0).plot.barh()

new_labels = [str(pow(10,i-1))+"€" if i>0 else str(i) for i, tick_label in enumerate(ax.get_xticklabels())]
print(new_labels)
ax.set_xticklabels(new_labels)

Giving :

['0', '1€', '10€', '100€', '1000€', '10000€'] [enter image description here]2

Why don't I get 20 000 in the list of the new labels ? Why the 10 000 it self is not displayed ?

Upvotes: 0

Views: 310

Answers (1)

Sheldore
Sheldore

Reputation: 39072

You don't get 20000 because you are creating powers of 10 as pow(10,i-1). It is mathematically not possibly from this equation. Moreover, 10000 is not displayed because you just use ax.set_xticklabels to reset the labels of the already existing xticks. Since you have only 5 major ticks in your first plot, you only create 5 labels as 0, 1, 10, 100, 1000 as per your definition.

To get what you want, just replace the last three lines of your code (after plotting) by:

locs = ax.get_xticks()
labels = [ '{}{}'.format(int(i), '\u20ac') for i in locs]
ax.set_xticklabels(labels)

Output

enter image description here

Upvotes: 2

Related Questions