Giter Club home page Giter Club 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

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

matlab-save-figure's Issues

bug in setSvgFileSize

Current code is as follows (saveFigure.m/setSvgFileSize, L414-5):

str = regexprep(str, sprintf('font-family:''Dialog''', 'font-family:''%s''', fontName));
str = regexprep(str, sprintf('font-family:''SansSerif''', 'font-family:''Helvetica''', fontName));

This isn't a valid us of regexprep (2 arguments), likely the following is what was intended?

str = regexprep(str, 'font-family:''Dialog''', sprintf('font-family:''%s''', fontName));
str = regexprep(str, 'font-family:''SansSerif''', sprintf('font-family:''Helvetica''', fontName));

Undefined function 'isstringlike'

Thanks for the tool!
I noticed that every time I tried to save a plot as .SVG the following error appears:

Error using saveFigure>patchSvgFile
The value of 'replaceStrokeDashArray_dashed' is invalid. Undefined function 'isstringlike' for input arguments of type
'char'.
Error in saveFigure (line 365)
patchSvgFile(svgFile, widthStr, heightStr, 1, exportFontFamily, patchSvgArgs{:});

makerow is an undefined function

Hi @djoshea, I run demo_saveFigure.m, but it does not run well, it show error in line 81.

" Undefined function 'makerow' for input arguments of type 'double' ", how do you define this function?

Missing input to convert Pdf

convertPdf requires 3 inputs, but is called (in the if block "if fileInfo.isKey('eps')" ) with only two inputs, resolution missing, producing the following error:

Not enough input arguments.

Error in saveFigure>convertPdf (line 583)
convertPath, resolution, escapePathForShell(pdfFile), resolution, escapePathForShell(file));

Error in saveFigure (line 443)
convertPdf(pdfFile, file);

dotted lines appear solid in exported pdf

Hi there,
I find the issue that dotted lines in the Matlab-plot appear as solid lines in the exported pdf-file.
The issue has been discussed a lot in the savefigure-project too. From there I took the idea to change the Matlab-Renderer (set(gcf,'Renderer','painters')) but that did not proof successful (neither did the zbuffer-Renderer.
For any hints I would be very thankful since the software looks to work very well appart from this issue (quick processing, very good looking latex-text representation)

saveFigure does not save the passed figure

Hello,
It seems that saveFigure saves the active figure, whether a figure handle is passed or not.

I'm assuming that this is because of the line print('-dsvg', '-painters', file);, which prints only the active figure.

This is a problem when opening many figures and trying to save them all at the end. A fix is to add the following somewhere before the print line:

% Make passed figure the current figure
set(0, 'CurrentFigure', hfig);

error when upsampling

on saveFigure.m line 208, it is currently:
jc.ScreenPixelsPerInch = dpi

this should be
jc.ScreenPixelsPerInch = renderDPI

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.