简体   繁体   中英

Plot looping really slow

I am writing a code to fit a gaussian over a function and if I don't plot the result (it is a datacube of ~60x60 spectra, so I am using a loop) the code works really fast.

But when I say the code to plot every graph it gets really slow, something like 2 graphs a second (when I don't plot it does like 40).

Ok, I understand it can be right to slow a lot down, but there is a code in IDL that does the exact same thing and the code runs 8~10 plots per second.

Is there a way to improve it? Or python is really slower than IDL?

Here is the plot code:

plt.plot(wavelengthset, data_datacube[minpixel:maxpixel+1, j, i], 'k-', 
         wavelengthset, gaussian(fit[0], wavelengthset), 'r-')

plt.draw()

plt.clf()

I recommend looking into removing plt.draw() and using blit . If that's insufficient, please let me know a little more about your data and the purpose of the plots.

See this answer for more info: why is plotting with Matplotlib so slow?

As the answer at the above link mentions, matplotlib is designed for quality, customizable, interactive plots. Matplotlib may be slower than the data processing tools you're familiar with in IDL, but that's not to say that another, speed-conscious Python toolkit won't be just as fast/helpful.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM