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!
Related
I would like to be able to make a plot with the x axis scaled as the log of the log. These types of plots are coming up rather frequently in my ME courses and I've been trying to figure out how to do it, and find that there doesn't seem to be a clean builtin way to do it either in python or in Matlab. Both support semilog (linear vs log(x)) and log-log (log(y) vs log(x)) plots, but I'm not seeing any way to get a semiloglog (linear vs log(log(x))) or log-loglog (log(y) vs log(log(x))) plot.
Anyone have any ideas on this? I could do some conversions by simply applying log(log(xvalues)) to my data, but drawing the grid, tick marks, and labels gets kinda tricky, so I was hoping someone might have already built a library for this.
I need to create a plot with matplotlib/pyplot in a very specific way. I want the curve to never go up again, instead I want these horizontal sections with the little overstanding line, like shown in the picture:
Source: https://scikit-learn.org/stable/auto_examples/model_selection/plot_precision_recall.html#sphx-glr-auto-examples-model-selection-plot-precision-recall-py
My problem is I don't know how this type of line-style is even called so I don't know what to search for. It is often used for PR-Curves in deep learning.
Does anyone know how this is called and maybe also how to plot a graph in this style with pyplot?
This is my current approach by just plotting with the plot function. As you can see there are these rising edges which I want to get rid of.
plt.plot(x, y, linewidth=3)
The example you cited from scikit-learn uses drawstyle='steps-post' (this demo illustrates what it does), so try
plt.plot(x, y, drawstyle='steps-post')
I'm just wondering if it exists an equivalent to the Matlab figure window in Python where we can modify plots directly from the figure window, or add some features (text, box , arrow, and so on), or make curve fitting, etc.
Matplotlib is good, but it is not as high-level as the Matlab figure. We need to code everything and if we want to modify plots, we need to modify the code directly (except for some basic stuffs like modifing the line color)
With matplotlib, you will indeed remain in the "code it all" workflow. This is not directly the answer you expect but the matplotlib documentation recently gained a very instructive figure that will probably help you if you stay with matplotlib: http://matplotlib.org/examples/showcase/anatomy.html shows the "anatomy" of the figure with all the proper designations for the parts of the figure.
Overall, I could always find examples of what I needed in their excellent gallery http://matplotlib.org/gallery.html
In my opinion, you'll save time by coding these customizations instead of doing them by hand. You may indeed feel otherwise but if not there is a ton of examples of matplotlib code on SO, in their docs and a large community of people around it :-)
Here it appears that matplotlib's specgram returns 4 variables including the last which is a plot:
http://matplotlib.org/examples/pylab_examples/specgram_demo.html
But here it seems there is only 3 variables returned in the tuple:
https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/mlab.py#L478
Where is the missing code to generate the specgram plot? Perhaps I am just confused on the difference between pylab and matplotlib. Either way, I can't find the source.
You're confusing the function that computes the data to be plotted with the function that plots the data.
mlab.specgram just computes the data, while the axes method specgram plots it.
Have a look at: https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/axes/_axes.py#L5786
ipython is very useful for things like this. method_name? will display the relevant documentation and the location of the source file, while method_name?? will display the relevant code, as well.
Understanding where the source for a matplotlib function is can be a bit confusing. Basically, anything in matplotlib.pyplot is auto-generated. Essentially all of the plotting methods are actually methods of the Axes object.
Hopefully that gets you started. If no one else gives a better answer, I'll elaborate more in a bit, when I have more time.
I'm writing a thousand plots to a PDF using matplotlib. I've already optimized the plotting code, ie. reusing figures/axes/lines and just changing the y data.
The bulk of the remaining time is spent in save_figure.
R, in comparison, seems to output a plot to PDF about 2x faster. Plots will all zero data seem to be even faster in R, while they're the same speed in Python.
I've set pdf.compression = 0, which makes a small improvement.
Tried rasterizing the data, it made no difference to plotting speed (although it used a ton of RAM).
Is there anything else I can try to speed up the matplotlib with PDF backend, or are there any alternative backends I should consider? I'm trying to beat R.
Thanks!
Have to tried pyreport from Gael Varoquaux? You call it on your script, it then collects all calls to pylab.show(), makes a png of each and then creates a PDF from it.
It uses Latex in the end, so you'll need this. But I expect this might be faster, as PDF creation is delegated to Latex.