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')
与える
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