python - matplotlib autoscale(axis='y') after slicing plot with set_xlim() -
as demonstration, i'm plotting x^0 through x^9 x values in range 10 20.
then i'm slicing images have 9 slices:
x = (10 11), (11 12) etc. (18 19)
i want images cropped y values spread top bottom in each slice, i'm getting autoscale scales full dataset rather current slice.
import matplotlib.pyplot plt import numpy np # create test data in range(10): x = np.arange(10,20) y = x**i plt.plot(x,y,c='red',marker='.',ms=2) # x values in chart , reduce sorted list set xd = [] n in range(len(plt.gca().get_lines())): line = plt.gca().get_lines()[n] xd.append((line.get_xdata()).tolist()) xd = [item sublist in xd item in sublist] xd = sorted(list(set(xd))) # attempt plot slices of x autoscaled y ax = plt.gca() 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 next line create 9 tiny (6kb) image files #plt.savefig(('image_%s.png' % i), bbox_inches=0, dpi=48) in actual application, i'm attempting generate 100k tiny images in manner database stochastic data. every x there between 2 , 200 y values. i'm using opencv image match new images best fit amongst historical database.
its critical y values stretched top bottom in each image opencv find match.
if helps x values int() type , equally spaced
eta: i've attempted implement of solutions here have made no progress:
matplotlib - fixing x axis scale , autoscale y axis
matplotlib scale y axis based on manually zoomed x axis
but @ least i've learned:
autoscaling uses full range of data, y-axis scaled full extent of y-data, not what's within x-limits.
still no solution works here though
def autoscale_y() presented @danhickstein
gives me:
h = np.max(y_displayed) - np.min(y_displayed) valueerror: zero-size array reduction operation maximum has no identity from links, i'm unsure implement @joe kington's mask solution in loops.
i'm working @bernie solution proposed here y values given x:
maybe can set_ylim() given min , max y values @ x manually?
this easier if there way autoscale within defined xlim standard matplotlib method
i solved issue last night creating dictionary x's keys , respective list of y's values.
this occurs data created function y=x**i
in essence i'm creating dictionary structure pseudocode:
data[x0] = [x0y1,x0y2,x0y3....] data[x1] = [x1y1,x1y2,x1y3....] data[x2] = [x2y1,x2y2,x2y3....] etc. i can later reference y values @ given x. there, find min , max y value left , right side of slice manually set ylim. if xlim slice more 1 x segment wide you'd have repeat process each respective x slice within xlim. in instance, x slices 1 segment wide.
import matplotlib.pyplot plt import numpy np # move range function out of data creation loop x = np.arange(10,20,1) # create dictionary of data x values keys data = {} in range(len(x)): data[x[i]]=[] # create test data in range(10): y = x**i plt.plot(x,y,c='red',marker='.',ms=2) # store y data data dictionary created xx = x[-len(y):] j in range(len(xx)): data[xx[j]].append(y[j]) # x values in chart , reduce sorted list set xd = [] n in range(len(plt.gca().get_lines())): line = plt.gca().get_lines()[n] xd.append((line.get_xdata()).tolist()) xd = [item sublist in xd item in sublist] xd = sorted(list(set(xd))) # attempt plot slices of x autoscaled y ax = plt.gca() in range(len(xd)-1): ax.set_xlim([xd[i],xd[i+1]]) # reference min , max y values left , 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 autoscale call #ax.axes.autoscale(enable=true,axis='y', tight=true) plt.pause(1) #timing now when plots, y autoscaled x slice, not entire dataset.
Comments
Post a Comment