## Save figure SVG from Matlab or Matplotlib

Matlab or Matplotlib will save infinite resolution vector graphics SVG format, viewable in web browsers. SVG is usable by LaTeX. Here are examples of saving figures to SVG from Matlab and Matplotlib.

## Python

To save figure handle `fg`

, simply do `fg.savefig('myfig.svg')`

.

```
from pathlib import Path
from matplotlib.pyplot import figure, draw
fn = Path('~/Documents/mycoolfig.svg').expanduser()
data = [1,2,3,4]
fg = figure()
ax = fg.gca()
ax.plot(data)
draw() # necessary to render figure before saving
fg.savefig(fn, bbox_inches='tight')
```

Matplotlib 2.2 added
`pathlib`

support.

## Matlab / GNU Octave

Figures in general are saved by `saveas(fig,filename)`

.

```
data = [1,2,3,4]
fg = figure();
plot(data)
saveas(fg,'matfig.svg')
```

## SVG vs. EPS vs. PNG

- vector graphics (SVG or EPS) allow nearly infinite zooming without loss of quality–excellent for line plots and contour plots
- SVG is viewable by any web browser, and is usable from LaTeX
- EPS is more commonly used in LaTeX
- PNG is raster graphics, so has finite (blocky) resolution