2013-02-01 100 views
0

我想要繪製在matplotlib一些數據顯示的實驗性功能的結果如下:Matplotlib X-標籤數座標

xvalues = [2, 4, 8, 16, 32, 64, 128, 256] 
yvalues = [400139397.517, 339303459.4277, 296846508.2103, 271801897.1163, 
      295153640.7553, 323820220.6226, 372099806.9102, 466940449.0719] 

我想這繪製在對數刻度,使其更容易可視化等方面都寫了下面的代碼:

import matplotlib.pyplot as plt 

def plot_energy(xvalues, yvalues): 
    fig = plt.figure() 
    ax = fig.add_subplot(1,1,1) 

    ax.scatter(xvalues, yvalues) 
    ax.plot(xvalues, yvalues) 

    ax.set_xscale('log') 

    ax.set_xticklabels(xvalues) 

    ax.set_xlabel('RUU size') 
    ax.set_title("Energy consumption") 
    ax.set_ylabel('Energy per instruction (nJ)') 
    plt.show() 

但是,你可以看到我的xlabels不會出現,因爲我想他們所看到以下 Matplotlib axis graph

如果我刪除線ax.set_xticklabels(xvalues)然後我得到了下面的結果,這不是我想無論是什麼: Matplotlib second axis graph

我是在x軸上繪製出正確的價值觀一定的幫助非常感謝!

在此先感謝。

回答

3

你只是在改變刻度的標籤,而不是刻度的位置。如果你使用:

ax.set_xticks(xvalues) 

它看起來像:

enter image description here

大多數時候,你只希望,如果你想要的東西,像類別標籤完全不同的設置(覆蓋)標籤的時間。如果您想要堅持軸上的實際單位,最好使用自定義格式器(如果需要)設置勾號位置。

2
import matplotlib.pyplot as plt 
import matplotlib.ticker as ticker 

def plot_energy(xvalues, yvalues): 
    fig = plt.figure() 
    ax = fig.add_subplot(1,1,1) 

    ax.scatter(xvalues, yvalues) 
    ax.semilogx(xvalues, yvalues, basex = 2) 
    ax.xaxis.set_major_formatter(ticker.ScalarFormatter()) 
    ax.set_xlabel('RUU size') 
    ax.set_title("Energy consumption") 
    ax.set_ylabel('Energy per instruction (nJ)') 
    plt.show() 

xvalues = [2, 4, 8, 16, 32, 64, 128, 256] 
yvalues = [400139397.517, 339303459.4277, 296846508.2103, 271801897.1163, 
      295153640.7553, 323820220.6226, 372099806.9102, 466940449.0719] 
plot_energy(xvalues, yvalues) 

產生

enter image description here