2012-09-12 76 views
0

開啓「對數」刻度時,x軸上的數字消失。未記錄在對數刻度圖上的x軸數

import numpy as np 
import matplotlib.pyplot as plt 

plt.plot(range(1,10) , range(1,10) , color='#aaaaff') 
plt.xscale('log') 
plt.xticks(range(1,10)) 
plt.show() 

如果我註釋掉xscale x軸上的數字將被打印出來。有人可以解釋爲什麼這不能在log刻度軸上工作,以及如何實現?

編輯:

簡化的代碼,以便它可以在沒有數據文件中使用。同一件事:沒有數字!

回答

2

通過日誌軸默認打勾位置是在基地10因此,在上述情況下,唯一可能的xtick是在1

如果你改變了xticks

plt.xticks([1,10]) 

你得到蜱110

你也可以玩ticker,它可以讓你做很多事情。例如,

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.ticker import LogLocator 

fig = plt.figure(1, [5,4]) 
ax = fig.add_subplot(111) 

ax.plot(range(1,100) , range(1,100) , color='#aaaaff') 
ax.set_xscale('log') 
# This is default, so play with it 
ax.xaxis.set_major_locator(LogLocator(base = 10.0)) 
plt.show() 

enter image description here

如果你使用:

ax.xaxis.set_major_locator(LogLocator(base = 100.0)) 

那麼你得到

enter image description here

+0

好,太好了!現在,我怎樣才能將ticks(和數字)設置爲2,4,8,16?我在問,因爲'ax.xaxis'沒有'xticks'! – ritter

+0

使用'ax.set_xscale('log',basex = 2)'。 – imsc