9

This is my data frame I'm trying to plot:

my_dic = {'stats': {'apr': 23083904,
                       'may': 16786816,
                       'june': 26197936,
                     }}
my_df = pd.DataFrame(my_dic)
my_df.head()

This is how I plot it:

ax = my_df['stats'].plot(kind='bar',  legend=False)
ax.set_xlabel("Month", fontsize=12)
ax.set_ylabel("Stats", fontsize=12)
ax.ticklabel_format(useOffset=False) #AttributeError: This method only works with the ScalarFormatter.
plt.show()

The plot:

enter image description here

I'd like to control the scientific notation. I tried to suppress it by this line as was suggested in other questions plt.ticklabel_format(useOffset=False) but I get this error back - AttributeError: This method only works with the ScalarFormatter. Ideally, I'd like to show my data in (mln).

aviss
  • 1,797
  • 4
  • 24
  • 47

2 Answers2

14

Adding this line helps to get numbers in a plain format but with ',' which looks much nicer:

ax.get_yaxis().set_major_formatter(
    matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ',')))

enter image description here

And then I can use int(x)/ to convert to million or thousand as I wish:

enter image description here

Community
  • 1
  • 1
aviss
  • 1,797
  • 4
  • 24
  • 47
  • 1
    Can you confirm if this works for logarithmic plots? It throws no errors for me but will not format any of the numbers. – akozi Jan 22 '19 at 13:02
13

Since you already using pandas

import matplotlib.pyplot as plt
my_df.plot(kind='bar')
plt.ticklabel_format(style='plain', axis='y')

enter image description here

BENY
  • 296,997
  • 19
  • 147
  • 204
  • 1
    hi, I've just tried this for a subplot (1,2) and it works for the second plot (ax[1]) but not for the first plot ax[0]. any ideas? – roastbeeef Dec 19 '18 at 09:23