2017-04-07 21 views
0

とプロットをスライスした後、私は10から次に20matplotlibののオートスケール(軸= 'Y')set_xlim()デモンストレーションとして

の範囲のX値とX^9を介してX^0をプロットしていI私は私の画像がそのようにyの値をトリミングしたい

(18〜19)に

X =(10〜11)、(11〜12)等:「私は9つのスライスを有するようにこれらの画像をスライスm個常に各スライスの上から下に広がっていますが、私が得ているのは、現在のスライスではなく、オートスケールが常にフルデータセットにスケールすることです。

import matplotlib.pyplot as plt 
import numpy as np 


# create some test data 
for i in range(10): 
    x = np.arange(10,20) 
    y = x**i 
    plt.plot(x,y,c='red',marker='.',ms=2) 

# get all x values in chart and reduce to a sorted list set 
xd = [] 
for n in range(len(plt.gca().get_lines())): 
     line = plt.gca().get_lines()[n] 
     xd.append((line.get_xdata()).tolist()) 
xd = [item for sublist in xd for item in sublist] 
xd = sorted(list(set(xd))) 

# attempt to plot slices of x with autoscaled y 
ax = plt.gca() 
for i in range(len(xd)-1): 
    ax.set_xlim([xd[i],xd[i+1]]) 
    ax.axes.autoscale(enable=True,axis='y', tight=True) 
    plt.pause(1) #timing 
    #uncommenting the next line will create nine tiny (6kb) image files 
    #plt.savefig(('image_%s.png' % i), bbox_inches=0, dpi=48) 

私の実際のアプリケーションでは、この方法で100kの小さな画像を確率的データから生成しようとしています。すべてのxについて、2〜200のy値がある。次に、OpenCVを使用して、履歴データベースの中に最適なイメージに新しいイメージをマッチングさせます。

OpenCVの各画像でy値が上から下に伸びて、良い一致が見つかることが重要です。

それは私のxの値は、常に(int型になります場合に役立ちます)を入力し、等間隔

ETA:私はここに解決策の一部を実装しようとしましたが、何の進展なされていない:

Matplotlib - fixing x axis scale and autoscale y axis

Matplotlib scale y axis based on manually zoomed x axis

が、少なくとも私は学んだ:

自動スケーリングは常にデータの全範囲を使用するため、y軸は x-limitsの範囲だけでなく、yデータの全範囲でスケーリングされた です。私は、実装がどこか不明だけど、それらのリンクから

h = np.max(y_displayed) - np.min(y_displayed) 
ValueError: zero-size array to reduction operation maximum which has no identity 

@:ここで働く

まだ解決策は@DanHickstein

が提示し

def autoscale_y() 

かかわらず、私を与えません私のforループのJoe Kingtonのマスクソリューション。

How to extract points from a graph?

多分、私は手動でXの最小値と最大Y値を与えられた)(set_ylimことができます。

私は今@bernie溶液で働いているが与えられたYの値がX得るためにここで提案しましたか?標準matplotlibの方法Iは、Xキーなどのとyのそれぞれのリストと辞書を作成することで、最後の夜、私の問題を解決し

答えて

0

として定義XLIM以内に自動スケーリングするための方法があった場合、これはそんなに容易になるだろう

値として。

データは本質的に= X ** I

関数yによって作成され、これは私が辞書構造の擬似コードを作成してい発生:

data[x0] = [x0y1,x0y2,x0y3....] 
data[x1] = [x1y1,x1y2,x1y3....] 
data[x2] = [x2y1,x2y2,x2y3....] 
etc. 

Iは、後にすべてのY値を参照することができ任意のx。そこから、私のスライスの左側と右側の最小値と最大値を見つけて、ylimを手動で設定します。あなたのxlimスライスが1つ以上のxセグメント幅であった場合は、xlim内のそれぞれのxスライスのプロセスを繰り返す必要があります。私の場合、私のxスライスはちょうど1つのセグメント幅です。

import matplotlib.pyplot as plt 
import numpy as np 

# move my range function out of my data creation loop 
x = np.arange(10,20,1) 

# create a dictionary of my data with x values as keys 
data = {} 
for i in range(len(x)): 
    data[x[i]]=[] 

# create some test data 
for i in range(10): 
    y = x**i 
    plt.plot(x,y,c='red',marker='.',ms=2) 

    # store my y data to my data dictionary as its created 
    xx = x[-len(y):] 
    for j in range(len(xx)): 
     data[xx[j]].append(y[j]) 

# get all x values in chart and reduce to a sorted list set 
xd = [] 
for n in range(len(plt.gca().get_lines())): 
     line = plt.gca().get_lines()[n] 
     xd.append((line.get_xdata()).tolist()) 
xd = [item for sublist in xd for item in sublist] 
xd = sorted(list(set(xd))) 

# attempt to plot slices of x with autoscaled y 
ax = plt.gca() 
for i in range(len(xd)-1): 
    ax.set_xlim([xd[i],xd[i+1]]) 

    # reference my min and max y values by left and right borders of x slice 
    ymin = min(min(data[xd[i]]), min(data[xd[i+1]])) 
    ymax = max(max(data[xd[i]]), max(data[xd[i+1]])) 
    # manually set y limits 
    ax.set_ylim([ymin,ymax]) 

    #eliminate my autoscale call 
    #ax.axes.autoscale(enable=True,axis='y', tight=True) 
    plt.pause(1) #timing 

ここでプロットすると、yはデータセット全体ではなくxスライスに自動的にスケールされます。

関連する問題