Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

matplotlib markers? #2

Closed
mdboom opened this issue Oct 20, 2011 · 4 comments
Closed

matplotlib markers? #2

mdboom opened this issue Oct 20, 2011 · 4 comments

Comments

@mdboom
Copy link

mdboom commented Oct 20, 2011

Have you considered using markers for this? Replacing the rasterized scatter calls in example.py with:

ax.plot(x, y, '+', markersize=2, markeredgecolor='red', antialiased=False)

runs in about 2.1s, on my machine. rasterized_scatter runs in about 1.9s, so it is slightly faster, but not by much.

The interactive performance of matplotlib markers is actually better because it isn't reallocating an image buffer on each pan and zoom.

About the only time to use scatter over marker is when the size of the symbol changes with each instance.

If rasterized output in the file is what you want, you can call "set_rasterized(True)" on the Line2D objects.

@astrofrog
Copy link
Owner

This is strange, I get much different run times:

My code

In [1]: import numpy as np

In [2]: import matplotlib.pyplot as plt

In [3]: from raster_axes import RasterAxes

In [4]: fig = plt.figure()

In [5]: ax = RasterAxes(fig, [0.1, 0.1, 0.8, 0.8])

In [6]: fig.add_axes(ax)
Out[6]: <raster_axes.RasterAxes at 0x104d04190>

In [7]: n = 1000000

In [8]: x = np.random.normal(0.5, 0.3, n)

In [9]: y = np.random.normal(0.5, 0.3, n)

In [10]: %time ax.rasterized_scatter(x, y, color='red')
CPU times: user 0.27 s, sys: 0.06 s, total: 0.33 s
Wall time: 0.33 s
Out[10]: <raster_axes.RasterizedScatter at 0x104d317d0>

In [11]: %time fig.canvas.draw()
CPU times: user 0.09 s, sys: 0.01 s, total: 0.10 s
Wall time: 0.11 s

Your code

In [1]: import numpy as np

In [2]: import matplotlib.pyplot as plt

In [3]: from raster_axes import RasterAxes

In [4]: fig = plt.figure()

In [5]: ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])

In [6]: n = 1000000

In [7]: x = np.random.normal(0.5, 0.3, n)

In [8]: y = np.random.normal(0.5, 0.3, n)

In [9]: %time ax.plot(x, y, '+', markersize=2, markeredgecolor='red', antialiased=False)
CPU times: user 0.12 s, sys: 0.04 s, total: 0.15 s
Wall time: 0.16 s
Out[9]: [<matplotlib.lines.Line2D at 0x104e45690>]

In [10]: %time fig.canvas.draw()

CPU times: user 6.31 s, sys: 0.31 s, total: 6.61 s
Wall time: 7.55 s

If look at the draw command (i.e. not including imports, etc. which would dilute the difference between the codes) which is what matters if I want to e.g. zoom in/out then using plot seems to be 70 times slower on my computer. I'm using the MacOS X backend though, could that explain the difference?

@mdboom
Copy link
Author

mdboom commented Oct 24, 2011

Ah. That probably does explain the difference. The Agg backend has an optimization where it draws the marker once as a stamp and then simply alpha-blends that raster into place for each marker instance. The MacOS X backend does not have such an optimization. You may want to try Agg. It may also be worth the moderate amount of effort to port this optimization to the Mac OS-X backend -- it's difficult for me to do this without access to a Mac, of course.

@ChrisBeaumont
Copy link

Sorry to revive this thread after ~a year. I just wanted to get @mdboom's attention :)

@mdboom, would Agg allow for a similar optimization in the MPL draw_path_collection call? That is, is it possible within Agg to draw a master marker image once, and then possibly resize and recolor that rasterized image?

I understand the resized markers might not be accurate at the pixel-level, but I'd be willing to sacrifice some aesthetics for ~50x performance boost (plot vs scatter) and the ability to color/size encode each data point.

@astrofrog
Copy link
Owner

@mdboom @ChrisBeaumont - I'm going to close this: the code here has evolved a bit since it started off, and the performance is higher than what can be done with the various backends, and allows for the colormap-coding (which ',' does not). I've however opened #5 and #6 which would be necessary in order to have a full equivalent to the c= option in scatter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants