2017-04-07 6 views
0

와 줄거리를 얇게 한 후, I는 10이어서 20하기 matplotlib의 자동 확장 (축선 = 'Y') set_xlim() 데모로서

범위에서 X 값과 X^9 X^0 플로팅있어 I I 9 개 슬라이스 갖도록 그 이미지 슬라이스있어 :

X = (10 ~ 11), (11 ~ 12)를 I 원하는

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

실제 응용 프로그램에서는 확률 론적 데이터에서 데이터베이스로 이러한 방식으로 100,000 개의 작은 이미지를 생성하려고합니다. 매 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

하지만 적어도 내가 배운 :

자동 크기 조절은 항상 데이터의 전체 범위를 사용하므로 x 제한 내에있는 것뿐만 아니라 y 축은 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() 

불구하고 저를주지 않는다 Joe Kington의 for 루프가 for 루프에 있습니다. 수동으로 X에서

How to extract points from a graph?

어쩌면 그때 (set_ylim 수) 주어진 최소 및 최대 Y 값 :

지금은 Y 값 주어진 X를 얻기 위해 여기 제안 @bernie 솔루션과 함께 일하고 있어요? 표준하기 matplotlib 방법 나는 X 키 등의와 y의의 각각의 목록과 사전을 만들어 지난 밤에 내 문제를 해결

답변

0

로 정의 된 xlim 내에서 오토 스케일 할 수있는 방법이 있다면이 너무 쉬울 것

값으로.

데이터 기능 (Y)에 의해 생성 될 때 발생 = X ** 나는 본질적 I 사전 구조 의사 생성있어

:

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

I 이후에 모든 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 슬라이스로 자동 확장됩니다.

관련 문제