2017-10-13 34 views
-1

這是我的數據幀我試圖繪製:如何在matplotlib中控制科學記數法?

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

情節:

enter image description here

我會喜歡控制科學記數法。我試圖按照其他問題plt.ticklabel_format(useOffset=False)中的建議來壓制它,但是我收到此錯誤 - AttributeError: This method only works with the ScalarFormatter。理想情況下,我想用(mln)顯示我的數據。

+0

問題是什麼? – ImportanceOfBeingErnest

+0

昨天我自己解決了它(請參閱更新...) – aviss

+0

那麼,這不是問答的概念如何工作。有一個問題,還有一個或多個答案。如果您已經解決了問題,請提供解決方案作爲答案,而不是編輯問題。 – ImportanceOfBeingErnest

回答

0

添加此行有助於在一個普通的格式,但與得到的數字 '' 它看起來好多了:

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

enter image description here

And th恩,我可以使用int(x)/轉換成百萬或幾千如我所願:

enter image description here

1

既然你已經使用pandas

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

enter image description here