MATLAB eps export doesn't include legend box - matlab

I am trying to export figures from MATLAB in publication quality for importing into LaTeX documents.
I am able to set all the graphics properties I would like, however when I export my figure as a .eps, the box around the legend disappears.
MWE below (I'm using MATLAB 2014b):
figure
plot(1:4)
legend('A line!')
print('LineTest','-depsc2','-tiff')
In LaTeX:
\usepackage{graphicx}
\usepackage{epstopdf} %converting to PDF
\begin{document}
\includegraphics[width=\textwidth]{LineTest}
\end{document}
This gives the following result, which has no legend box:

It works fine for me with using Matlab 2016a and your TeX document. The following TeX document also works, try it, maybe it is an issue with your TeX compiler:
\documentclass[a4paper]{article}
\usepackage[pdftex]{graphicx}
\begin{document}
\includegraphics[width=\textwidth]{LineTest.eps}
\end{document}
Running epstopdf manually on the EPS image also gives a PDF with the legend in a box. Can you run it from the command line and see whether the problem is still there for you?
FYI my version of pdflatex:
pdfTeX 3.14159265-2.6-1.40.16 (TeX Live 2015/Debian)
kpathsea version 6.2.1
Copyright 2015 Peter Breitenlohner (eTeX)/Han The Thanh (pdfTeX).
There is NO warranty. Redistribution of this software is
covered by the terms of both the pdfTeX copyright and
the Lesser GNU General Public License.
For more information about these matters, see the file
named COPYING and the pdfTeX source.
Primary author of pdfTeX: Peter Breitenlohner (eTeX)/Han The Thanh (pdfTeX).
Compiled with libpng 1.6.17; using libpng 1.6.17
Compiled with zlib 1.2.8; using zlib 1.2.8
Compiled with poppler version 0.41.0

I had a similar issue. I was saving two figures as eps files and using them in Latex. The figures were saved automatically using:
print(filename, '-depsc2', '-tiff')
I was using TexStudio and Miktex 2.9 with the command:
\includegraphics[width=\textwidth]{filename.eps}
The first file did not have the bounding box around the legend but the second file did.
The work around I found was to manually save the figure (Figure → Save As...) as filename.eps. The bounding box then showed up in TexStudio.

Related

exporting svg image from matlab surface plot

I need some to produce some publication-quality figures. I first export the figures from matlab in .svg format, and then I do some post-processing in inkscape. I am no problem with figures generated using plot or scatter, but when I export figures generated using surf (in view(2)), I run into problems. If I use plot or scatter, I am able to ungroup and process various parts like the title, axes, scattered points, lines, etc. in inkscape. For surf, however, matlab just exports one single figures with all various parts grouped into one single unit. I can't separate individual part, and when I zoom very close I can actually see the bitmap resolution for the axes and titles (if I use plot, the titles and axes have 'infinite' resolution when I zoom very close). I am fine with the surface plot having finite resolution, but I need to at least be able to process the axes and titles (which I currently cannot do). What should I do so that I can 'separate' the title and axes from the main plot, just like figures generated from plot and scatter?
I stumbled across this question, since I encountered the same problem.
As mentioned by #vindarmagnus, it is possible to use tikz and get rather nice results. However, tikz experiences problems with large data sets in my experience as present when using surf etc..
Solution, that worked for me:
Change the renderer to painters and the exported .svg file will retain its vectorgraphic properties when opened e.g. in inkscape:
figure('Renderer','Painters');
I used to use Inkscape for my scientific publications as well, but I found that a lot of the time you can get better results with pgfplots in latex, together with the matlab2tikz matlabscript. There’s a ton of resources about this online, but here’s how my workflow would look adopted to your surf situation. I have macOSX with latex, matlab and matlab2tikz installed. Will work with little to no modifications on linux.
In Matlab:
surf(peaks(25))
matlab2tikz('plot.tikz’)
Then I have the following bash-script (just a script in the same folder as the image, which is executed by mere double-click). (Needs to be chmod-ed as an executable for that).
#!/bin/bash
cd ~/Desktop
rm *.eps
cat > plot.tex << EOF
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{max space between ticks=50}
\pgfplotsset{scaled ticks = false}
\pgfplotsset{compat=1.6}
\pgfplotsset{xticklabel style={/pgf/number format/fixed}}
\pgfplotsset{yticklabel style={/pgf/number format/fixed}}
\begin{document}
\input{plot.tikz}
\end{document}
EOF
pdflatex plot.tex
pdf2ps plot.pdf
ps2eps plot.ps
Note that the row cd ~/Desktop above should be changed as to reflect which folder the script is supposed to be run from (a bit crappy, but needed since Finder doesn’t properly pass along the folder from a program is executed, afaik).
This yields high-quality images in eps or pdf or what you like, with a ton of settings for axes and ticks etc. And it all uses native latex fonts.
Edit:
Recently I’ve begun to use patch() in matlab and then export it to tikz in the same manner as above, with great results. That’s another suggestion!
You can use also:
set(gcf,'Renderer','Painters')

Saving Matlab/Octave plots to pdf/png format from online IDE?

I'm currently using the Matlab/Octave online IDE at http://www.tutorialspoint.com/execute_matlab_online.php and I'm trying to save a graph that I'm plotting to .pdf format. I've done the following:
- octave
- x = [ 1: 10 ]
- y = x
- plot(x,y)
- print -dpdf graph.pdf
I then refresh the the files and folders to the left, double click on the graph.pdf file and am always greeted with a black filled rectangle. I've then attempted to run the following:
- axis("off")
- print -dpdf graph2.pdf
And the graph appears to save to pdf correctly. However, without the axes. Could you please assist me in finding a solution?
P.S. I have already attempted octave --force-gui and graphics_toolkit('gnu_toolkit')/graphics_toolkit('fltk') with no luck.
At the site hosted, the only graphics toolkit available is gnuplot, which support in octave was mostly dropped several versions ago.
gnuplot has the bug you've described. In the link, people got plotting working by rolling back to previous gnuplot versions.
Guess the only way of getting printing working on the site would be signalling about the issue to its administration.
Also, note that octave crudely depicts the copy of the plot in terminal window. That might suffice for correctness check of the plotting code itself.
Alternatively, you can save octave graphics objects with hgsave command, those then can be loaded in octave on another computer to obtain same plot as one that would be drawn there. (Although, at this point there might be no reason for using online interpreter then.)

MatLab Eps Print Webdings interpreter

So recently I decided to not use the standard markers that Matlab provides and use my own via: a set of fonts including Webdings or WingDings. I make a standard scatter plot, and plot the text (in wingdings), using the text command, over the locations of the markers.
When I save these plots as png files, they print perfectly. They also appear perfectly on my screen after plotting.
However when I save these plots as eps files, the webdings are turning into their original letters 'l' or 'w'. It also looks like it's plotting them in a Courier font, but my default is Helvetica.
I've read through this previous post, but my question differs in that he is looking to use Latex as the interpreter and to include fonts, whereas I don't want to use Latex as the interpreter. However, it seems like my default interpreter (not sure what that is), isn't doing the job when converting to EPS.
Example:
imageR='w'
text(xf2,yf2,imageR,'fontName',font,'FontSize',fontR,'HorizontalAl','left','color','w')
I figured it out....took me way too long.
Ghostscript / Postscript only export with a few fonts when you are exporting eps files.
This link was incredibly helpful.
I just switched from wingdings/webdings to ZapfDinbats...Practically the same thing...

iPython nbconvert and latex: use .eps instead of .png for plots

I have an iPython notebook that contains an inline plot (i.e. it contains the command plot(x,y)). When I issue the command ipython nbconvert --to latex --post PDF --SphinxTransformer.author='Myself' MyNotebook.ipynb the resulting .PDF file contains the figure, but it has been exported to .PNG, so it doesn't look very good (pixelated). How can I tell nbconvert to export all plots/figures to .EPS instead?
Thank you
First of all the notebook is not responsible for creating plots, but matplotlib and this allows to render your plots as eps, pdf, svg, etc. in great quality to be included in publications.
I agree that the default inline plot format, i.e. png is not optimal to be used for publications due to several reasons. As given in the github issue you posted in your answer, the inline backend can be configured to use svg instead of png by calling
%config InlineBackend.figure_format = 'svg'
in a code cell.
With this, the newly created plots will be vectorial, (as Matt said, already rendered pngs will not be converted!). These svgs are embedded in the notebook (svgs can be rendered by modern browsers) and are subsequently converted to pdfs by nbconvert. These pdfs fulfil the requirements of publication ready plots. However, be aware that the svgs can be really huge (compared with pngs) and may slow down the notebook handling significantly.
Your initial question was about eps graphics. As said above, matplotlib can render eps, hence, you can always do something like savefig('plot1.eps') to create the desired figures. That's actually the way I create my publication figures (png in notebook, eps in paper).
Let's assume we would get IPython to generate eps files (embedded but not renderable in the browser). The tex file generated by nbconvert is designed to work with pdflatex. If fed with eps files pdflatex would convert these to pdf to be included in the final document. So basically it does the same as nbconvert currently does with the svgs. Thus, there is no benefit.
Finally, I want to point out that, even though the tex files generated by nbconvert look great (especially the ones created using the master branch), IMHO there is no way to use these files without touching (e.g. adding captions, scaling images, ...). Therefore, you could always include the eps files at this step into your papers.
NBconvert does not run your code. So if you haven't plotted with SVG matplotlib backend it is not possible.
If you did so, then you need to write a nbconvert preprocessor that does svg-> eps and extend the relevant template to know how to embed EPS.
Evidently this is a missing feature in nbconvert. There is not currently a way to get publication quality plots in your final PDF: https://github.com/ipython/ipython/issues/3603
https://github.com/ipython/ipython/issues/3693

Ghostscript converting Postscript to PNG is over-saturated

I'm trying to use Ghostscript and/or ImageMagick to convert each page of a Postscript document into PNG images. The problem is that both produce images that are way too saturated (I think that's the right terminology).
Here are the commands I'm trying:
gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -dGraphicsAlphaBits=4 -sOutputFile=page_%02d.png brochure.ps
convert brochure.ps im_page_%02d.png
This is the input Postscript file (brochure.ps from above)
Here's a couple of the output images I'm getting:
Page 1
Page 6
As you can see (especially on the page with the big green map of New Hampshire), the colors of the output PNGs are too bright/saturated. How can I prevent the colors from being changed so much and get a more accurate conversion?
Preview in OS X 10.6 automatically does a very accurate conversion to PNG when you open a Postscript file in it. This leads me to believe there is just something screwy with the way ghostscript converts ps->png (I'm fairly confident ImageMagick is just a wrapper for ghostscript for this operation). Is there a tool besides ghostscript I should be using instead?
Note: As pipitas points out below, the visible difference of colors varies by OS. It is very obvious in OS X 10.6, but apparently not very noticeable in Windows XP.
You are right in assuming ImageMagick just being a wrapper for Ghostscript when converting from PostScript or PDF to an image format.
I think, this problem can only be solved to anybody's satisfaction once the efforts to add support for ICC profile handling and color management (currently underway) are completed for Ghostscript (design document as PDF). That point in time is close, however. If I understand recent commits to http://svn.ghostscript.com/trunk/ correctly, the next release (which will be dubbed 9.00 and out hopefully in August) will include support for color management via LittleCMS. Yay!
OSX 10.4 and up provide sips (scriptable image processing system) and it works well with PDF format. Perhaps it can be a temporary solution until Ghostscript supports color management.