簡體   English   中英

如何在matplotlib中控制科學計數法?

[英]How to control scientific notation in 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()

劇情:

在此處輸入圖片說明

我想控制科學計數法。 我試圖按照其他問題plt.ticklabel_format(useOffset=False)建議通過此行來抑制它,但是我得到了此錯誤AttributeError: This method only works with the ScalarFormatter 理想情況下,我想以(mln)顯示數據。

由於您已經在使用pandas

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

在此處輸入圖片說明

添加此行有助於獲取純格式的數字,但帶有','看起來更好:

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

在此處輸入圖片說明

然后,我可以根據需要使用int(x)/轉換為百萬或千:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM