python - matplotlib flip x and y -
i had question on how matplotlib worked. basically, want flip x , y in image this person asked.
however, not want resort transposing array before sending it.
i assume result in loss of performance. here reasoning. guess matplotlib tries copy image numpy matplotlib iterating on rapidly varying index in both (where rapidly varying index here assumed index leads accessing contiguous elements in physical memory). if transpose array, 1 of 2 cases can happen:
- what matplotlib thinks rapidly varying index in memory no longer true , no longer accessing numpy array in memory contiguous fashion, resulting in slower readout. (i.e. numpy changes it's "view" matrix)
- the numpy array copied new array rapidly varying index transposed. reading matplotlib fast @ cost of copying new matrix memory.
both of these cases not ideal in case achieve reasonably high refresh rates. arrays loaded images stored on hard drive stored in fashion.
if assumption true, there method have matplotlib change rapidly varying index case of image? useful feature believe.
i hope have communicated line of reasoning. want make sure every read , write in chain memory contiguous, hard drive numpy array matplotlib. believe finding (or offering in future) option matplotlib reverse method of ordering save time.
i open other solutions. if there's obvious have missed, i'd hear :-)
edit: comments. believe right in should have done testing before hand. have interesting result. transpose seems faster non-transpose. since numpy reference view array, possible matplotlib uses advantage cleverly decide whether or not transpose last minute? results suggests so, or code has flaw (which possible). seriously, if that's true, friggin' job matplotlib developers! :-)
here figures:
figure 1 : transposed image (blue) , non transposed image (green). each data point time taken draw 10 frames in sequence. repeated 100 times. did not use machine duration of these experiments.
figure 2 : zoom of same figure
the code (it's crude, learning , limited time spend on this. ran first 1 1000 frames, closed window, uncommented second animation, commented first , ran again 1000 frames plotted two.):
` matplotlib.pyplot import * matplotlib import animation time import time import pylab import numpy np data = np.random.random((10,1000,1000)) ion() fig = figure(0) im = imshow(data[0,:,:]) data[0,:,:] *= 0 time1 = time() time0 = time() timingarraynot = np.zeros(100) timingarrayt = np.zeros(100) def animatenot(i): global time0,time1,timingarraynot if(i%10==0): time0 = time() if(i%10 == 9): time1 = time() #print("time 10 frames: {}".format(time1-time0)) timingarraynot[i/10] = time1-time0 im.set_data(data[i%10,:,:]) if(i == 1000): return -1 return im def animatet(i): global time0,time1,timingarrayt if(i%10==0): time0 = time() if(i%10 == 9): time1 = time() #print("time 10 frames: {}".format(time1-time0)) timingarrayt[i/10] = time1-time0 im.set_data(data[i%10,:,:].t) if(i == 1000): return -1 return im #anim = animation.funcanimation(fig, animatet,interval=0) anim = animation.funcanimation(fig, animatenot,interval=0) `
Comments
Post a Comment