17

I am using matplotlib to scatter plot a 3D matrix of points. I am using the following code:

import pylab as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

my_data = np.random.rand(6500,3)  # toy 3D points

fig = plt.figure()
ax = Axes3D(plt.gcf())
ax.scatter(my_data[:,0],my_data[:,1],my_data[:,2])
plt.show()

It works, so it opens a window where I can see my points.

However if I try to rotate the plot with the mouse (clicking and dragging it) it rotates REALLY slow.

I think 6500 points are not a lot for such a slow and laggy rotation, so I'm wondering if there is any pre-configuration to be done to speed it up.

Note: I tried to use Matlab and I can rotate a way bigger scatter plot without any lag, so it's not a computer limitation.

Can someone run this code and see if also experiences the slow rotation?

EDIT: Using the System monitor I can see that when rotating the points, only one CPU is used, so matplotlib is not parallelizing the process.

My computer specs:

  • Intel(R) Core(TM) i5-3550 CPU @ 3.30GHz
  • Graphic Card: Xeon E3-1200 v2/3rd Gen Core processor Graphics Controller
  • Memory: 10GB RAM
  • HDD: Samsung SSD 500 GB

running Ubuntu 16.10

  • 2
    I'm afraid that matplotlib was not designed with speed in mind, and slow performance in your case is not a specific quirk of your local installation - it's the way mplot3d was written. If you want to use fast 3d scatterplots with python you can check out `plotly.py` lib - it's very fast, although its python capabilities are somewhat limiting. – Vlas Sokolov Apr 03 '17 at 15:25
  • 3
    Echo what @VlasSokolov says - matplotlib is not designed for speedy interactive plotting, especially in 3D (which is not a real full-fledged 3D capability, more just for convenience). You might also look into mayavi for 3D plotting. Also, as a Python tip - I would recommend against `import pylab as plt`. `plt` is typically used for `matplotlib.pyplot` *not* `pylab`. Any examples you may have found that do so are old. Also, using pylab in a script at all is not recommended as good practice - use matplotlib and numpy separately. – Ajean Apr 03 '17 at 15:56
  • See https://stackoverflow.com/questions/8955869/why-is-plotting-with-matplotlib-so-slow – thomaskeefe Jul 18 '17 at 18:03

1 Answers1

16

(revised 2021)

Matplotlib was not really designed to be interactive. Plotly (among a few others) is a plotting package that is rather feature complete and uses a webGL backend for scatter3D that will render in your browser (and is blazing fast). More on why you might want to consider it as a go-to replacement at the bottom:

# pip install plotly
import numpy as np
import plotly.graph_objects as go

my_data = np.random.rand(6500,3)  # toy 3D points
marker_data = go.Scatter3d(
    x=my_data[:,0], 
    y=my_data[:,1], 
    z=my_data[:,2], 
    marker=go.scatter3d.Marker(size=3), 
    opacity=0.8, 
    mode='markers'
)
fig=go.Figure(data=marker_data)
fig.show()

3D scatter plot in plotly

I use plotly as a replacement for matplotlib because:

  • it has great coverage (bar, scatter, line, hist, etc.)
  • intuitive interactive components that are highly configurable
  • portability: it can generate html plots which are easily viewed across a network(!) or saved as static file formats
  • it has a clearer api than matplotlib (for full config, jsonish)
  • it integrates strongly with pandas (you can use it as the backend)
  • it is well supported and adoption is increasing
anon01
  • 10,618
  • 8
  • 35
  • 58
  • This is nice! I had to drastically reduce the marker size to get anything like your plot, though. – Mohan Jan 26 '21 at 14:35