0

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()
Alex Riley
  • 169,130
  • 45
  • 262
  • 238

1 Answers1

0

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!

Community
  • 1
  • 1
Jan Van Bruggen
  • 314
  • 2
  • 8
  • I am trying to understand the blitting thing, and I have a question blit is a package or it is a function used by other packages?? For example, PyQtGraph uses blitting?? Or blit is a command I need to use?? – Luis Gabriel Dahmer Hahn Feb 23 '15 at 14:12
  • BTW, if blit is a command, can you please give me an example on how to use it?? – Luis Gabriel Dahmer Hahn Feb 23 '15 at 14:36
  • It's an optional argument in matplotlib that means only the changed parts should be redrawn. It might just be for animation, and I'm not sure if it's going to help you. Again, I don't know what you're trying to do with the plots. Here's a great answer about it: http://stackoverflow.com/questions/14844223/python-matplotlib-blit-to-axes-or-sides-of-the-figure – Jan Van Bruggen Feb 23 '15 at 14:36
  • Here's an example of someone using it with an animation: https://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/ – Jan Van Bruggen Feb 23 '15 at 14:37