2013-02-01 99 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