# Speed up slow scatter and line plots in MATLAB

## Alec Jacobson

## February 04, 2014

Matlab seems to default to its CPU renderer `'painters'`

when creating new figures with scattered dots or lines. So

```
plot3(X,Y,Z);
```

where `X`

and so on are very large can take a long time (say 1 minute).

Whereas first setting the renderer to the GPU renderer `'OpenGL'`

is much faster.

```
set(gcf,'Renderer','OpenGL');
plot3(X,Y,Z);
```

for the same large `X`

takes 6 seconds.

Awkwardly, plotting a bunch of (disconnected) line segments from vertices in `N`

and edge indices in `E`

in matlab using something like:

```
plot3([N(E(:,1),1) N(E(:,2),1)]',[N(E(:,1),2) N(E(:,2),2)]',[N(E(:,1),3) N(E(:,2),3)]','k');
```

or

```
line([N(E(:,1),1) N(E(:,2),1)]',[N(E(:,1),2) N(E(:,2),2)]',[N(E(:,1),3) N(E(:,2),3)]');
```

is much slower to render and interact with than to just draw a bunch of degenerate triangles using:

```
trisurf([E E(:,1)],N(:,1),N(:,2),N(:,3))
```

This has the added bonus that it seems to throw the renderer automatically in to 'OpenGL' mode.