6

私が現在持っているのはこれです:

x = [3.0, 4.0, 5.0, 5.0, 6.0, 7.0, 9.0, 9.0, 9.0, 11.0]
y = [6.0, 5.0, 4.0, 2.5, 3.0, 2.0, 1.0, 2.0, 2.5, 2.5]

これにより、次のグラフが生成されます。

ここに画像の説明を入力

私が望むのは、軸に等しいスケーリングを持たせることです。したがって、7 と 9 と 9 と 11 の間にこのような大きなギャップがあるのではなく、他のすべてと同じように等しいスペースになります。次のようになります。

ここに画像の説明を入力

グラフから 8 と 10 を削除するために、ティックを使用しました。関連するコードは次のとおりです。

ax=fig.add_subplot(111, ylabel="speed")
ax.plot(x, y, 'bo')
ax.set_xticks(x) 

matplotlib ページの例には、私が望むものはありません。私はドキュメントを調べてきましたが、「スケーリング」に関連するものはすべて、私がやりたいことをしていません。

これはできますか?

4

1 に答える 1

4

OP への私のコメントに加えて、1 から n までの自然数に対してプロットできます。ここで、n はデータ セット内の一意の横座標値の数です。次に、x ticklabels をこれらの一意の値に設定できます。これを実装する際に私が抱えていた唯一の問題は、繰り返される横座標の値を処理することです。この一般的なものを維持しようとするために、私は次のことを思いつきました

from collections import Counter # Requires Python > 2.7

# Test abscissa values
x = [3.0, 4.0, 5.0, 5.0, 6.0, 7.0, 9.0, 9.0, 9.0, 11.0]

# Count of the number of occurances of each unique `x` value
xcount = Counter(x)

# Generate a list of unique x values in the range [0..len(set(x))]

nonRepetitive_x = list(set(x)) #making a set eliminates duplicates
nonRepetitive_x.sort()         #sets aren't ordered, so a sort must be made
x_normalised = [_ for i, xx in enumerate(set(nonRepetitive_x)) for _ in xcount[xx]*[i]]    

この時点でprint x_normalised

[0, 1, 2, 2, 3, 4, 5, 5, 5, 6]

yしたがって、に対してプロットx_normalisedする

from matplotlib.figure import Figure
fig=Figure()
ax=fig.add_subplot(111)

y = [6.0, 5.0, 4.0, 2.5, 3.0, 2.0, 1.0, 2.0, 2.5, 2.5]

ax.plot(x_normalised, y, 'bo')

与える

matplotlib を使用してプロットされたソリューションの結果

set_xticklabels最後に、使用して元の x データの実際の値を反映するように x 軸の目盛りラベルを変更できます。

ax.set_xticklabels(nonRepetitive_x)

編集OPで目的の出力のように見える最終プロットを取得するには、使用できます

x1,x2,y1,y2 = ax.axis()
x1 = min(x_normalised) - 1 
x2 = max(x_normalised) + 1
ax.axis((x1,x2,(y1-1),(y2+1))) 

#If the above is done, then before set_xticklabels, 
#one has to add a first and last value. eg:

nonRepetitive_x.insert(0,x[0]-1) #for the first tick on the left of the graph
nonRepetitive_x.append(x[-1]+1) #for the last tick on the right of the graph 
于 2012-08-20T22:21:09.307 に答える