0

I'm trying to plot some data in matplotlib to show the results of an experiement as follows:

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]

I wish to plot this on a logarithmic scale to make it easier to visualise and so have written the following code:

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()

However as you can see my xlabels don't appear as I'd like them to as seen below Matplotlib axis graph

If I remove the line ax.set_xticklabels(xvalues) then I get the following result, which isn't what I'd like either: Matplotlib second axis graph

I'd be very grateful for some help in plotting the correct values on the x-axis!

Thanks in advance.

4

2 に答える 2

3

目盛りの位置ではなく、目盛りのラベルのみを変更しています。使用する場合:

ax.set_xticks(xvalues)

次のようになります。

ここに画像の説明を入力

ほとんどの場合、カテゴリ ラベルのようにまったく異なるものが必要な場合にのみ、ラベルを設定 (オーバーライド) します。軸上の実際の単位に固執したい場合は、(必要に応じて)カスタムフォーマッタで目盛りの位置を設定することをお勧めします。

于 2013-02-01T11:24:04.787 に答える
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)

収量

ここに画像の説明を入力

于 2013-02-01T11:29:00.177 に答える