• Stars
    star
    37
  • Rank 720,807 (Top 15 %)
  • Language
    MATLAB
  • License
    GNU General Publi...
  • Created over 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Cross-platform figure export from Matlab to SVG, PDF, PNG, etc. preserving transparency

SaveFigure: Matlab vector figure export

SaveFigure is a Matlab utility which provides aesthetically pleasing figure export which provides a few essential features not present in Matlab's built in figure export or any known utility on the FileExchange:

  • instant export to multiple formats, including PDF, SVG, EPS, PNG, while ensuring that all formats look identical
  • identical output on multiple platforms (Linux and MacOS currently supported, Windows support should not be difficult to add, contact me if interested)
  • preserves alpha blending and transparency on patches, lines, markers, etc.
  • sets or preserves nicely rendered fonts (specified in options)
  • preserves vector graphics

I've found the outputs to be more consistent, more faithful to the on-screen displayed figure, and more aesthetically pleasing than other excellent alternatives, including export_fig by Oliver Woodford and Yair Altman and savefig by Peder Axensten. Please note that this submission includes code copied wholesale from Juerg Schwizer, Oliver Woodford, Yair Altman, and Peder Axensten.

To achieve faithful, WYSIWYG vector reproduction of Matlab figures, we generate SVGs, then convert to PDF using Inkscape, and then to PNG and EPS if requested using ImageMagick's convert utility. I've found that convert rarely does a nice job going from SVG to PDF directly, though this could simply be a matter of setting the correct flags. So Matlab is only responsible for generating the SVG. For newer versions of Matlab (R2014a or newer), saveFigure uses Matlab's new internal SVG engine, i.e. print -dsvg, which faithfully reproduces Matlab figures as SVG. For older versions, the code for generating SVGs is essentially a nice wrapper around Juerg Schwizer's plot2svg utility, with a few minor tweaks. The advantage of this approach is that we have complete control of figure output and appearance; the disadvantage is that it requires a complete reconstruction of the figure as an SVG. Consequently, it may not perfectly reproduce the figure in all instances, but it does a fairly decent job.

Installation

SaveFigure requires ImageMagick and inkscape to be installed and accessible from the command line in order to run. The easiest way to accomplish this is to run:

Linux: sudo apt-get install inkscape imagemagick

Mac: brew cask install inkscape

brew install imagemagick

Windows: not working yet, should be doable. If you'd like to help, I'm happy to accept pull requests!

Usage

Save to specific file type:

saveFigure('foo.pdf');

saveFigure('foo.png', gcf);

Save to set of file types, return full file names:

fileNameList = saveFigure('foo', gcf, 'ext', {'pdf', 'png', 'svg', 'fig', 'eps', 'hires.png'});

If fileName has no extension, the figure will be saved in multiple formats as specified by the 'ext' parameter value pair. If no 'ext' is specified, the default list is {'pdf', 'png', 'fig'}.

Demo

From demo_saveFigure:

% Plot scatter plot with alpha blending
randseed(1); figure(1); clf;
N = 500; dx = randn(N, 1); dy = randn(N, 1);
h = plot(dx, dy, 'o', 'MarkerFaceColor', [0.6 0.6 1], 'LineWidth', 0.1, ...
  'MarkerEdgeColor', 'w', 'MarkerSize', 8);
setMarkerOpacity(h, 0.3, 0.6);
hold on
N = 500; dx = randn(N, 1) + 1; dy = randn(N, 1);
h = plot(dx, dy, 'o', 'MarkerFaceColor', [1 0.6 0.6], 'LineWidth', 0.1, ...
  'MarkerEdgeColor', 'w', 'MarkerSize', 8);
setMarkerOpacity(h, 0.3, 0.6);

xlabel('Param 1'); ylabel('Param 2'); title('SaveFigure Demo');
box off; axis equal;

saveFigure('demoScatter.png')

% Plot timeseries with translucent error regions
randseed(2);
K = 6; N = 1000; t = (0:N-1) - 100;
y = sgolayfilt(randn(N, K), 3, 99, [], 1);
ye = sgolayfilt(randn(N, K) * 0.5, 3, 99, [], 1);

figure(2), clf; hold on;
cmap = parula(K);
for k = 1:K
    % errorshade defined below
    errorshade(t, y(:, k), ye(:, k), cmap(k, :), 'errorAlpha', 0.5, 'lineAlpha', 0.9);
end

box off; xlim([0 800]);
xlabel('Time'); ylabel('Signal'); title('SaveFigure Demo');

saveFigure('demoTimeseries.png');

Line and Marker Opacity

Included in the repo are two utilities setLineOpacity and setMarkerOpacity which will set the opacity of lines and markers in plots, respectively.

setLineOpacity(hLine, edgeAlpha)

and

setMarkerOpacity(hLine, markerFaceAlpha, markerEdgeAlpha)

In newer versions of MATLAB, this opacity setting will occur directly on the graphics handle and alter the appearance of the Matlab figure. In older versions of Matlab where these opacity settings are not supported, these settings will be stored in the UserHandle of the figure, where saveFigure will search for the setting upon saving. Thus, the opacity will not be visible in Matlab but will be reflected in the saved PDF, PNG, or EPS file.

Credit

saveFigure internally relies heavily on (and includes within it) code from:

More Repositories

1

vim-autoread

Have Vim automatically reload a file that has changed externally
Vim Script
94
star
2

neuropixel-utils

Utilities for managing Neuropixel datasets and Kilosort results
MATLAB
32
star
3

matlab-utils

Collection of miscellaneous shared utils for Matlab
MATLAB
28
star
4

matlab-barplot

Automatic bar & violin plots with groups, whiskers, significance bridges
MATLAB
25
star
5

vim-matlab-fold

Vim code folding for Matlab files
Vim Script
14
star
6

eraasr

ERAASR: An algorithm for removing electrical stimulation artifacts from multielectrode array recordings
MATLAB
12
star
7

obci

Code for real-time, low-latency processing of 2P calcium imaging data streaming from PrairieView
MATLAB
8
star
8

haptic-control

Real-time controller of Force Dimension delta.3 for reaching tasks on Chai3D
C++
7
star
9

matlab-signalProcessor

Aggregates incoming signals from simulink-serializedDataLogger
MATLAB
5
star
10

trial-data

Interfaces and utilities for analysis of neurophysiology and behavioral data
MATLAB
4
star
11

synaptome

Toolkit for Synaptome Visualization, Smith Lab Autumn Rotation
Objective-C
4
star
12

retina-tools

Matlab/Igor Pro Tools from Baccus Lab Winter Rotation
MATLAB
3
star
13

matdb

In memory relational database and database-driven analysis layer for Matlab
MATLAB
3
star
14

opsin-analysis

Analysis tools for Optogenetics
Objective-C
3
star
15

matlab-imagestack

Lightweight Matlas class which wraps multichannel ImageStacks
MATLAB
3
star
16

matlab-fex-utils

Collection of Matlab utilities uploaded to the Mathworks File Exchange
MATLAB
2
star
17

processing-snowflakes

Java
2
star
18

sphinxcontrib-matlabdomain

Temporary development clone of sphinxcontrib Matlab Domain
Python
2
star
19

dc-info-theory

Dynamic Clamp Information Theory Code
Objective-C
2
star
20

eeg-tools

EEG Analysis Code
Objective-C
2
star
21

simulink-serializedDataLogger

Receives network packets with signals encoded by simulink, writes to .mat Matlab file
C++
1
star
22

djoshea

Code for personal website
JavaScript
1
star
23

vim-dotvim

My personal .vim directory
Vim Script
1
star
24

matudp

Bus communication layer and data logger for Simulink Real Time (xPC Target)
MATLAB
1
star
25

matnote

MATLAB lab notebook generation
Python
1
star
26

matlab-course

Website for Stanford Matlab for the Biosciences course
JavaScript
1
star
27

cerebus-nev-utils

MATLAB libraries for reading Blackrock Cerebus NEV and NSX files
MATLAB
1
star
28

neuropixels-toolkit

(Under construction) Neuropixel Toolkit is a set of unified Matlab tools for manipulating Neuropixel and Kilosort datasets
1
star
29

matlab-quiver

Matlab interfaces to append text to Quiver notes
MATLAB
1
star