2017-10-13 118 views
2

比方說,我有以下代碼Matplotlib禁用指數符號

import matplotlib.pyplot as plt 
plt.figure() 
plt.semilogy([0,1],[20,90]) 
plt.show() 

它創建如下圖所示:

enter image description here

我想禁止在y科學記數法(所以我想有20,30,40,60,而不是2x10^1等)

我已經看了this線程,並且我嘗試添加

import matplotlib.ticker 
plt.gca().get_yaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter()) 
plt.gca().get_yaxis().get_major_formatter().set_scientific(False) 
plt.gca().get_yaxis().get_major_formatter().set_useOffset(False) 

但它不會影響結果數字。我正在使用python 3.5.3和matplotlib 2.1.0。我錯過了什麼?

回答

1

由於y軸上的刻度在不到十年的時間內,它們是次要刻度,而不是主要刻度。因此,您需要將次要格式化程序設置爲ScalarFormatter

plt.gca().yaxis.set_minor_formatter(matplotlib.ticker.ScalarFormatter())

完整的示例:

import matplotlib.pyplot as plt 
import matplotlib.ticker 

plt.figure() 
plt.semilogy([0,1],[20,90]) 
plt.gca().yaxis.set_minor_formatter(matplotlib.ticker.ScalarFormatter()) 
plt.show() 

enter image description here

+0

Aaaaah,這就是我失蹤了。非常感謝! :) – arriopolis