I've got some performance problems with matplotlib, and would like to
ask if you know any way I can make it faster.
If there is no such way, I have to decide to (a) either enhance matplotlib
or (b) write my own plotting-library.
(I'm currently using matplotlib to plot data "live" on the screen, including
animation, scrolling, zoom+pam, custom scales (to zoom out some part of the
plot), and multiple X-/Y-Axes. I therefore already wrote some wrappers around
matplotlib to implement some of these features.)
- I have a figure containing some plots (lines).
- About every second I "update" the plot:
- Add a few points to the lines.
(=add point to an array and call set_data(array))
- Scroll the plot, so that the latest point is on the right of the plot.
Older points disappear on the left side of the plot.
(=set_xaxis() + draw())
- I'm using GtkAgg, incl. animation, (re)storing the background, drawing
the artists and blit.
(canvas.restore_region(...), ax.draw_artist(...), canvas.blit(bbox))
This works as long as the plot only contains a few points,
although 2 figures + 5-10 lines per figure and an update every 0.5 s
already consumes about 10-20% CPU (on a 1.4 GHz Pentium).
By the way: Is this speed normal, or is matplotlib usually faster?
But as soon as the plot contains *many* points (several 10000 up to
several 100000), the plotting becomes terribly slow -- up to 30s
per update and more!
Do you know any way to make this faster?
My ideas are:
- Since I only add points to the *right* of the lines, I could reduce the
number of points, by first removing all points which are outside of
the current visible plotting-window from my array, and then calling
set_data() with the reduced array.
- This unfortunately wouldn't reduce the number of points in very dense
plots. It would be possible to (a) replace several points which all
result in the same plotted point by a single point or (b) cache the
plotted points e.g. on pixel-level. I think (b) would probably require
to write a new backend.