Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making Matplotlib run faster

Snippet:

ax = Axes3D(self.fig)

u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)

x = self.prop * np.outer(np.cos(u), np.sin(v))
y = self.prop * np.outer(np.sin(u), np.sin(v))
z = self.prop * np.outer(np.ones(np.size(u)), np.cos(v))

t = ax.plot_surface(x, y, z, rstride=6, cstride=6,color='lightgreen',linewidth=0)
self.canvas.draw()

The above snippet graphs a sphere in tkinter using matplotlib. I've found that higher rstride and cstride values allow the graph to have a bit better performance. However, they give the sphere a weird ribbed shape. I was wondering what other things could be adjusted in the above code to help improve performance.

like image 736
rectangletangle Avatar asked Nov 15 '10 04:11

rectangletangle


People also ask

Why does matplotlib take so long?

It's your bottleneck. In your case, you don't need to re-draw things like the axes boundaries, tick labels, etc. 2) In your case, there are a lot of subplots with a lot of tick labels. These take a long time to draw.

Is there anything better than matplotlib?

Plotly has several advantages over matplotlib. One of the main advantages is that only a few lines of codes are necessary to create aesthetically pleasing, interactive plots. The interactivity also offers a number of advantages over static matplotlib plots: Saves time when initially exploring your dataset.

Is Pyqtgraph faster than matplotlib?

matplotlib: For plotting, pyqtgraph is not nearly as complete/mature as matplotlib, but runs much faster. Matplotlib is more aimed toward making publication-quality graphics, whereas pyqtgraph is intended for use in data acquisition and analysis applications.


2 Answers

Really, the problem is more within plot_surface. There are a lot of things that can be done to improve it. For instance, the shading takes a lot of time and just by changing one line:

colors = [color * (0.5 + norm(v) * 0.5) for v in shade]

to

colors = np.outer(0.5+norm(shade)*0.5,color)

within one of the functions used by plot_surface, I got a reduction of about 28% in the overall runtime. Why? The norm function (well, class kind of) is set-up for vectorization, but wasn't being used in that manner. I know that there are many such things within these functions that aren't very optimal. Changing the two lines:

for rs in np.arange(0, rows-1, rstride):
    for cs in np.arange(0, cols-1, cstride):

to

for rs in xrange(0,rows-1,rstride):
    for cs in xrange(0,cols-1,cstride):

in the plot_surface function itself gives another substantial improvement - now we're down 33% from the original runtime.

From what I've seen, the code isn't really written for efficiency so much as to just get it working from what I can tell - there are plenty of places where things which could be made to be more vectorized using Numpy and aren't. I'm afraid that what really is needed is some optimization of the matplotlib functions.

like image 181
Justin Peel Avatar answered Oct 12 '22 23:10

Justin Peel


At this point is the visualization package that has the bottleneck. The number of points are defined and constant.

Try if using psyco can speed up it (is only 32bit though).

like image 45
fabrizioM Avatar answered Oct 13 '22 01:10

fabrizioM