pandas 如何在matplotlib中控制科学记数法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46735745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:38:04  来源:igfitidea点击:

How to control scientific notation in matplotlib?

python-2.7pandasmatplotlib

提问by aviss

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

我想控制科学记数法。我试图按照其他问题中的建议通过这一行来抑制它,plt.ticklabel_format(useOffset=False)但我又得到了这个错误 - AttributeError: This method only works with the ScalarFormatter。理想情况下,我想以 (mln) 显示我的数据。

采纳答案by aviss

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:

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

enter image description here

在此处输入图片说明

回答by YOBEN_S

Since you already using pandas

既然你已经在使用 pandas

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

enter image description here

在此处输入图片说明