Skip to content

Speeding up Axes3D.plot_surface 4-8x #16659

@apaszke

Description

@apaszke

Hi! I've recently had to produce a lot of 3D surface plots and have noticed that the implementation of Axes3D.plot_surface is heavily suboptimal for my use case. I think that the implementation has been made so generic is that for some reason plot_surface has to deal with subsampling the input internally, which may produce polygons with boundaries of different lengths. In my case, the plot is an output of a simulation, so I want to display it in its full resolution (i.e. rstride == cstride == 1), meaning that all polygons are actually squares. This has allowed me to store all their data in a NumPy arrays instead of super long Python lists, and I've been able to speed up the whole rendering process by 4-8x by monkey patching the following methods:

  • Axes3D.plot_surface
  • Poly3DCollection.do_3d_projection
  • Poly3DCollection.get_vector
  • PolyCollection.set_verts

The question I have for you is: would you be interested in having me upstream those changes? The only concern I would have in mind is that it might split some code paths in two (one dealing with lists, the other with NumPy arrays), potentially making future maintenance more difficult. At the same time, if someone wants to draw plots without subsampling the input grid, or with strides are divisors of the grid size, then 4x speedups can be quite noticeable. In my case I've been able to reduce the rendering time for a single animation (100 plots with 200x200 points in a grid) from 2min 21s to 18s, or a single higher-resolution frame (1 plot with 800x800 points in a grid) from 1min 13s to 11s, and those do make a difference in your workflow. I am perfectly willing to wait a few seconds, but definitely not minutes.

I've also added a Poly3DCollection.update_surface method, which allowed me to reuse the existing plot object for even faster rendering of a series of plots.

As mentioned before I already have all of the code necessary, but right monkey patching means it will likely get out of sync with the future versions, and everyone else misses out on the work I've already put in. Let me know how would you like to proceed with that.

By the way I've been interested in pushing the speedups even further, but it looks like those improvements would likely require storing collections of paths describing sets of homogeneous polygons in arrays instead of Python lists and adapting the renderers. That seems like much more work, but I'd be very excited if matplotlib could produce those plots in at least near real-time speeds.

Matplotlib version

  • Operating system: Linux
  • Matplotlib version: 3.1.3
  • Matplotlib backend (print(matplotlib.get_backend())): Agg
  • Python version: 3.7

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions