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')
Related
A 2D data matrix was plotted in MATLAB 2016a using contour (the first Figure below), and then I saved as the figure in the *.emf format. Next, I inserted the figure (emf) into a MS word document. And finally, the word document was converted to a pdf file.
I found that there are many white lines in the figure (when in a pdf format) as shown in the second figure below. My question is how can I remove those white lines?
The code is attached here:
path = 'C:\Users\Administrator\Desktop\';
data = importdata([path, 'lsa2.txt'], ' ', 6);
cdata = data.data;
n = 25;
contourf(cdata,n, 'LineStyle', 'none');
colormap(jet);
axis equal;
The data can be accessed here: https://www.dropbox.com/s/hzf75qiju6zsy9i/lsa2.txt?dl=0
As I mentioned in my comment, this is a bug with the way MATLAB exports graphics, as explained by Yair Altman and Dene Farrell:
I discovered that these white line artifacts happen when the painters renderer is used ... [which] is the default rendering [format engine] for vectorized (EPS/PDF) formats.
There are two separate issues with the Matlab export:
1. The main thing that everyone notices is that patches are broken up into triangles, each of which is a separate path object if inspected in illustrator.
2. Matlab sometimes adds extraneous 'cropping paths' that create an apparent white line even when there is no issue with fractured paths.
One workaround suggested there by ambramson is the following:
1. Save the figure as an .eps file (using the print command).
2. Using a text editor, change the line in the eps header from:
/f/fill ld
to:
/f{GS 1 LW S GR fill}bd
and move the line down several lines, to right below the /LW/setlinewidth ld line.
From here, your eps file should display fine on all pdf viewers.
So I want to add text to a plot that is a crystal direction. Sounds easy enough, right?
It's easy to do [110], but what if I want to do [-110]? If you are familiar with crystallography, you know that the negative sign should be above the 1. How can I do this?
These are called Miller indices. You can use LaTeX in Matlab text command, so it's pretty straight-forward:
figure();
axes();
text(0.5, 0.5, '\([1\bar{1}0]\)', 'Interpreter', 'latex');
You can even do that without LaTeX, but there's no point unless you need to e.g. customise font.
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.
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')
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.