Git Product home page Git Product logo

matlab-save-figure's Introduction

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:

matlab-save-figure's People

Contributors

djoshea avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.