如何在matplotlib中控制科学计数法? [英] How to control scientific notation in matplotlib?

查看:357
本文介绍了如何在matplotlib中控制科学计数法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要绘制的数据框:

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()

这是我的绘制方式:

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()

剧情:

我想控制科学计数法.如其他问题plt.ticklabel_format(useOffset=False)所建议,我试图通过此行抑制它,但我收到此错误-AttributeError: This method only works with the ScalarFormatter.理想情况下,我想以(mln)显示我的数据.

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).

推荐答案

添加此行有助于获取纯格式的数字,但带有','看起来更好:

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

然后我可以根据需要使用int(x)/转换为百万或千:

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

这篇关于如何在matplotlib中控制科学计数法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆