2017-04-07 156 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切片,而不是整个数据集。