converting a contourf() plot with grid on with matlab2tikz script - matlab

When I convert a counterf() plot with matlab2tikz it won't show me the grid. Even though I have set the grid on. In the MATLAB plot I can see the grid, but in the compiled Latex pdf I can't see it.
My MATLAB code:
contourf(v);
colorbar;
title('el. Potential V(x,y)');
grid on;
xlabel('x-Achse');
ylabel('y-Achse');
matlab2tikz('pic1.tikz');
in the MATLAB plot I see this:
and in the compiled LaTeX PDF I see this:

At the moment (Apr. 2015), this behavior is not a feature in matlab2tikz yet. According to this bug report, it is known to the developers and as it seems they plan on adding this feature / removing this bug in the near future.
So as for now, you can either wait for the addition of this feature to matlab2tikz, add it to matlab2tikz yourself and create a pull-request on Github, or manually add the grid to the generated TikZ file.

Related

How can I make my plots look prettier (including fonts, label, etc )?

I am trying to obtain very nice plots for my presentation below is a code that I used to plot
clear all
clc
close all
syms v
omegat= -2:0.000001:2;
Nt=32;
gainfuc = (1/Nt)*exp(1i*pi*omegat*(Nt-1)/2).*sin(pi*Nt*omegat/2)./sin(pi*omegat/2);
gainfuc(omegat == 0) = 1;
G = (omegat < 2/Nt).*(omegat > -2/Nt);
plot(omegat,abs(gainfuc))
syms t
hold on
grid on
plot(omegat,G,'r')
ylabel('G_t(y)','FontSize',16,'FontWeight','bold')
xlabel('y','FontSize',16,'FontWeight','bold')
My question is quite simple, any ideas to make this plot nicer (font, grids, etc...) so that it would look nice in presentations?
Update
I have obtained the following figure after the changes recommended in the answer below
In Matlab 2014b a new graphic engine got introduced, it immediately looks more pretty.
New default colormap presents data more accurately, making it easier to interpret. New default line colors, fonts, and styles with anti-aliased graphics and fonts improve the clarity and aesthetics of MATLAB visualizations.
In Matlab 2014a you can also activate the new graphics engine by following these instructions.
In earlier versions the hack is may also possible, but I haven't tested it. It is most likely quite buggy. For 2014a I use it for almost a year now and it works like a charm. I couldn't find any differences to the final release of HG2 in 2014b.
Make sure that smoothing is set to 'on'
h = gcf;
h.GraphicsSmoothing = 'on'
I also used the standard LateX font CMU Serif Roman to spice everything up. Enter this lines at the beginning of your code after installing the fonts (open source).
set(0,'defaultAxesFontName', 'CMU Serif Roman')
set(0,'defaultAxesFontSize', 12)
General recommendations:
Use vector graphic renders: set(gcf, 'renderer', 'painters')
Specify the resolution when saving your plots, especially for pixel graphics: print('-dpng','-r600','PeaksSurface') (still use the vector renderer!)
The vector format for MS Office is .emf and is also supported by Matlab
May use: set(gcf,'InvertHardcopy','off')

Matlab: How to avoid artefacts in filled contour plots

I am trying to export filled contour plots from Matlab as vector graphics to include in a Latex file. My current methodology is:
contourf(x,y,v_mag,20), axis([0,width,0,height]),daspect('manual') ;
grid off
colormap jet
h = colorbar;
caxis([0 v_lid])
h.Label.String = 'Velocity Magnitude (m/s)';
set(gcf,'renderer','painters')
export_fig('-painters', '-transparent', 'pdf', 'filename.pdf');
The problem with this method is that it produces artefacts (the white lines) which look like the following:
I understand that these white lines are the polygons defining the shaded areas which have invisible edges, and don't quite overlap (according to here). The problem is caused by the pdf viewer itself which tries to smooth the lines displayed on the screen (according to here). My problem is that most people viewing the document will not know this and will not know how to prevent the viewer doing this. So my questions is:
Is it possible to create a vector graphic of a filled contour plot from Matlab without these artefacts?
Eps produces the same problems. I have tried to use the SVG function but have not had any luck. I am trying to avoid using raster graphics due to the pixelation caused by zooming in. Any advice would be much appreciated.
EDIT - Additional info - Using Matlab v.2014b and Ghostscript v.9.15
This is an extremely frustrating issue for which there seems to be no solution (or even, few attempts at a solution), and it has been many years now. In summary, Matlab cannot cope with outputting artefact-free contour or surface plots (anything with complicated meshes or transparencies).
I can suggest a simple workaround that will work in most cases, where the colours or details of the underlying contour plot do not need to be preserved perfectly.
Output a version of the figure without lines in png format with high enough resolution.
Output a version of the figure without colours in pdf format. This should be free of any artefacts. If your figure it complicated and has many transparencies, you may need to output multiple versions building up the 'levels'.
Use Adobe Illustrator (or some equivalent) to perform a vectorized trace of the raster image. You may lose some detail here, but for simple contour plots with limited details, it will be converted easily to vectorized form.
Overlay the two images within Illustrator. Output in vector format.
This also allows you to use things like Illustrator's ability to compress pdfs.
If you don't want to toy with vectorizing the raster output, you can also simply replace steps 3-4 and combine a raster colour image with a vectorized line image. This would work well for complicated contour plots giving you crisp lines, but the ability to compress the underlying colours.
Eventually, MatLab 2013b doesn't have this problem. Furthermore the files it produces has much less volume. That is because MatLab 2013b composes vectorized image of big overlapping figures, while MatLab 2014b makes that awful meshing.
Here the first file was got with 2013b and the second with MatLab 2014b (I highlighted one of the polygons with red stroke to show the difference). The volumes differ in approximately 22 times (38 Kb vs. 844 Kb).
So it is not the viewer problem, it's how the image is exported from MatLab.
The issue is also discussed here Triangular split patches with painters renderer in MATLAB 2014b and above, but still no direct solution.

Colorbar not appearing in (surface) plot - LaTeX interpreter issue

As the title states, the colorbar in surface plots does not appear when the default interpreter is set to 'latex'. This occurs in MATLAB 2012b and 2013a and on two different machines.
Precisely, the colorbar object is created, can be clicked when editing the plot, can be edited using the interactive colorbar editor but is not visible at all.
It does not appear when saved as a figure and reopened, saved as a PNG, exported in .eps format or saved as a .pdf.
After searching around, I found the following post from 2011, concerning MATLAB 7:
http://mathforum.org/kb/message.jspa?messageID=7518470
Specifically, the interpreter appears to be at fault, when it is set to 'latex', the colorbar will not display. When set to the default, it does.
Here is the smallest demonstrating example.
set(0,'defaulttextinterpreter','none');
figure;
surf(peaks(100)); colorbar
set(0,'defaulttextinterpreter','latex');
figure
surf(peaks(100)); colorbar
The two figures are identical except that the colorbar is visible only in the first figure.
I use a lot of special characters and sub/superscripts in my plots so in startup.m I set the default interpreter to 'latex'. I could surround all calls to colorbar with:
set(0,'defaulttextinterpreter','none');
colorbar;
set(0,'defaulttextinterpreter','latex');
But this is probably the least elegant solution possible. Can anyone shed some light on this issue which appears to be extant for over 5 years and multiple editions of MATLAB?
This behaviour is gone in Matlab R2014b, which uses an entirely new graphics engine, hg2. The plots look different (most of the time in a better way), but instead of old, documented bugs, there are now new, undocumented bugs...
Earlier versions of Matlab support somewhat experimental stages of hg2. You can enable these by running Matlab with the switch "-hgVersion 2". You can do this, for example, by editing the Desktop shortcut to point to something like "C:\Program Files\MATLAB\R2013b\bin\matlab.exe" -hgVersion 2.
Unfortunately, with the new graphics engine being the default in Matlab2014b, the old bugs are less likely to be fixed in the future. I wish I could help you in a better way, but the workaround you posted seems like a good solution, especially if you wrap it in a function called robust_colorbar or so.
I can reproduce the problem on my system (R2010b, Windows Vista 32 bits) . It seems to be solved by changing the 'Renderer' property of the figure from the default 'OpenGL' to either 'painters' or 'zbuffer'. So, you can either change the renderer when creating the figure:
set(0, 'defaulttextinterpreter', 'latex');
figure('Renderer', 'zbuffer') %// this line changed
surf(peaks(100)); colorbar
or change the default renderer to be used for all figures (so you don't need to change it in every figure):
set(0, 'DefaultFigureRenderer', 'zbuffer'); %// this line added
set(0, 'defaulttextinterpreter', 'latex');
figure
surf(peaks(100)); colorbar
Using a renderer other than 'OpenGL' may affect features such as transparency or drawing speed. Here's some information about pros and cons of each renderer.

Jagged outline using Matlab 2014b

I am plotting some maps using Matlab that use mapshow to plot the country border from a shapefile. I then export them to both a PDF and EPS format using the export_fig package. This worked completely fine using Matlab 2014a, but I have just upgraded to Matlab 2014b to take advantage of something else that has improved, and now my country border is all jagged. The border only looks jagged on the saved versions of the file. If I zoom in on the figure window, the outline isn't like that.
Here are the snippets of code that are important. It is a custom shapefile, so I don't know how to put it on here so people can replicate it.
This bit reads in the shapefile and plots it. The display type is 'polygon' if that is relevent, hence getting rid of the 'FaceColor' so I can see what I am plotting underneath (the green bits in the background of the images, plotted using pcolor).
thaiborder=shaperead('Thailandborder');
mapshow(thaiborder,'FaceColor','none');
This bit is how I am exporting the figure.
export_fig test.eps -r600 -painters
export_fig test.pdf -r600 -painters
This is the version with a smooth border from Matlab 2014a:
This is roughly the same area of the image, with the jagged border from Matlab 2014b:
Does anyone know why these differences are occurring? I want the border to be like it is in the first image, but I need the "improved" functionality of Matlab 2014b for another thing in the same image. What do I need to change?
Edit to add: I have been in contact with the creator of export_fig and he thinks it is caused by Matlab now using mitred joins rather than round ones. Apparently I have to write to MathWorks to complain. I didn't put this as an answer because someone else may be able to provide a solution for me.
Matlab acknowledged that this is known bug. For me the first fix worked.
The issue of jagged lines on the figures while exporting in the vector format is a known bug in MATLAB R2014b. It is associated with the combination of linejoins and meterlimits used in vector format.
To work around this issue, use the attached function fixeps to post process the EPS file.
You can use one of the following ways to call this fixeps function.
fixeps('input.eps','output.eps','LJ') % Will change the linejoins to round
fixeps('input.eps','output.eps','ML') % Will correct the miterlimit
function fixeps(inname,outname,fixmode)
if nargin==2
fixmode = 'LJ';
end
fi = fopen(inname,'r');
fo = fopen(outname,'w');
tline = fgets(fi);
while ischar(tline)
if (strcmp(tline,['10.0 ML' 10])) % Replace 10.0 miterlimit
switch (fixmode)
case 'LJ'
fwrite(fo,['1 LJ' 10]); % With round linejoin
case 'ML'
fwrite(fo,['2.5 ML' 10]); % With smaller miterlimit
end
else
fwrite(fo,tline);
end
tline = fgets(fi);
end
fclose(fo);
fclose(fi);
I had a similar problem that I found to be caused by the 'MarkerSize' option. It seems that in version 2014b it inherits the units of the figure. For example, if I have a figure in centimeters and I ask for ('MarkerSize', 10), the 10 will not be interpreted as points (as in 2014a) but as cm. I fixed this by changing the figure units to pt.

How to render MATLAB figure legend texts as LaTeX?

Although I have rendered my legend text with LaTeX in MATLAB as follows
set(myLegend, 'fontsize', 8, 'interpreter','latex', 'Position', [0.67, 0.12, 0.3, 0.01]);
I still feel that the texts are rather different from my main texts. For example, the texts seem so ugly, because the letters are too far apart. Plus, the strikes are so thin.
How can I make them look exactly the same as the caption below?
You can actually export your Matlab figure by using Matlab2tikz. Once you get your Matlab figure, just insert it in your LaTeX file by \input{myfigure.tex}.
Using Matlab2tikz is really easy, as stated in the README file:
The workflow is as follows.
a. Place the matlab2tikz scripts (contents of src/ folder) in a directory
where MATLAB can find it (the current directory, for example).
b. Make sure that your LaTeX installation includes the packages
TikZ (aka PGF, >=2.00) and
Pgfplots (>=1.3).
Generate your plot in MATLAB.
Invoke matlab2tikz by Matlab matlab2tikz(); or matlab2tikz('myfile.tex');
The script accepts numerous options; check them out by invoking the help, help matlab2tikz Sometimes, MATLAB makes it hard to create matching LaTeX plots by keeping invisible objects around or stretches the plots too far beyond the bounding box. Use Matlab cleanfigure;matlab2tikz('myfile.tex');` to first clean the figure of unwanted entities, and then convert it to TeX.
Add the contents of myfile.tex into your LaTeX source code; a
convenient way of doing so is to use \input{/path/to/myfile.tex}.
Also make sure that at the header of your document the Pgfplots package
is included
This should be in your .tex file:
\documentclass{article}
\usepackage{pgfplots}
% and optionally (as of Pgfplots 1.3):
\pgfplotsset{compat=newest}
\pgfplotsset{plot coordinates/math parser=false}
\newlength\figureheight
\newlength\figurewidth
\begin{document}
\input{myfile.tex}
\end{document}
This is how I normally embed latex in legends,
l = legend('$\alpha$', '$\dot{\alpha}$', '$x$', '$\dot{x}$');
set(l, 'interpreter', 'latex', 'location', 'northwest', 'FontSize', 15)
For some reason, possibly a bug, it only works when when you set the interpreter via the object's setter.
To do the above caption try,
l = legend('(b) $t=9:00 \Delta t$
set(l, 'interpreter', 'latex')