2017-04-07 161 views
0

作爲示範切片情節後,我與範圍內的x值從10至20。matplotlib自動縮放(軸= 'Y')與set_xlim()

然後繪製的x^0至x^9我「M切片那些圖像,使得我有9片:

X =(10〜11),(11〜12)等,以(18〜19)

我想我的圖片裁剪,從而y的值總是從上到下在每個切片中傳播,但是我得到的結果是自動縮放總是縮放到整個數據集而不是當前切片。

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軸是根據y數據的全部範圍縮放的,而不僅限於 x限制內的內容。

仍然沒有解決方案,在這裏工作雖然

def autoscale_y() 

通過@DanHickstein

介紹給我:

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

從這些鏈接,我不能確定在哪裏實現@喬金頓的面具解決方案在我的for循環。

我現在在這裏提出的解決方案@bernie工作得到Y值給出X:在那個X手動

How to extract points from a graph?

也許我就能set_ylim()給出的最小和最大Y值?

這將是如此容易得多,如果不存在對定義XLIM內自動縮放爲標準matplotlib方法

回答

0

昨晚我解決我的問題,通過創建與X的作爲鍵和y的的相應列表字典的方式作爲價值觀。

作爲數據由函數y創建時發生此= X **我

在本質我創建詞典結構的僞代碼:

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

以後可以在引用所有的y值任何給定的x。從那裏,找到我的切片的左側和右側的最小值和最大值,以手動設置ylim。如果你的xlim片超過了一個x片段寬,你將不得不爲xlim中的每個x片重複該過程。在我的例子中,我的x片段正好是一段寬。

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切片,而不是整個數據集。