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
How to control scientific notation in matplotlib?
提问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:
剧情:
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), ',')))
And then I can use int(x)/
to convert to million or thousand as I wish:
然后我可以根据int(x)/
需要转换为百万或千: