MATLAB putting negative sign above a number in plot text - matlab

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.

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')

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')

MATLAB: Interactive tool to "draw" a Plot?

Do you know a simple way to draw arbitrary splines or lines in a plot-figure? I've got the following figure which I created in powerpoint with more or less arbitrary spline-curve (blue) but I'd like to do the same in MATLAB now because of a better look in the final plot-output.
I'm now wondering if I've got to manually "find" data-values to draw some sort of spline (which looks roughly like the blue one below) myself or if there's maybe a tool where I can simply insert some points into a plot interactively and there's a curve fitted though it to create something similar!?
The green and red lines I can figure out myself (probably also have to plot them manually, do I)?!?
Thanks in advance!
EDIT
Okay I found a way myself doing it in MATLAB to gnerate a nice spline: Use splinetool, then either use an example or import some data and then you can interactively add and delete points until your spline looks roughly like it should. Then file->print to figure and then tools->edit plot! You can then delete everything you don't need, add title, xlabel and ylabel etc. and then export it to latex with e.g. matlab2tikz :-) Very nice stuff!!
According the purpose, print nice plots for the thesis, I have some out-of-Matlab recommendations.
1) plot everything as usual, you get a figure handle and an axes handle
h = figure( ... )
a = gca
2) use the data cursor function of the figure window and interactively insert the base points for your later splines. You can additional points by right-click.
t = linspace(0,2*pi,1000);
[x y] = deal(sin(t),cos(t))
Later you delete the "visual" part of the data tip inside Illustrator/Inkscape, if you just want to keep the anchor point of the vector graphic to snap your splines.
There is also the possibility of custom data tips: Tutorial at Matlab Central
3) I once wrote a function to nicely plot Matlab figures as vector graphic based PDFs. You can specify height, width and how much white margin around you want. You just need to pass figure and axes handle and the name:
function saveFigure( fig_handle, axes_handle, name , height , width , margin)
set(axes_handle,'LooseInset',get(gca,'TightInset'));
set(fig_handle, 'Units','centimeters','PaperUnits','centimeters')
% the last two parameters of 'Position' define the figure size
set(fig_handle,'Position',[-margin -margin width height],...
'PaperPosition',[0 0 width+margin height+margin],...
'PaperSize',[width+margin height+margin],...
'PaperPositionMode','auto',...
'InvertHardcopy', 'on',...
'Renderer','painters'... %recommended if there are no alphamaps
);
saveas(fig_handle,name,'pdf')
end
4) You get a PDF you could directly use for Latex - for use in MS Office use 'emf' (
Enhanced metafile) rather than 'pdf'. Open this file with Adobe Illustrator (preferable as it offers layers) or Inkskape (Open Source).
5) The datatips are recognized as graphical objects, you can catch them and draw a spline on them. For Illustrator I'd recommend to put the spline in another layer than the actual figure. Later you can just swap the figure and give the spline new anchor points. In Inkscape you could use the grouping function to keep everything together.
6) I'd say you save a lot of time over a only-Matlab-solution. Good look!

MATLAB, turning numbers off on plot

I'm pretty familiar with all of the axis properties for a matlab plot, but I can't seem to find any of them that actually effect displaying the numbers or not. I have a plot where numbers are pretty meaningless, they're only there to get a good visual representation of what I'm working on. So, it would be better if I could just have the numbers gone compeltely. Is there a way to do this? Thanks. (No not the tick marks or any of that, the actualy NUMBERS! =))
It's not the tick marks you're after but the tick labels. Just set them to empty lists:
set(gca,'XTickLabel', [], 'YTickLabel', [])

How to vary the line color of a matlab plot (like colormap)?

I have a 2D space in which a function value is defined (you can think of it as a manifold). Now I plotted the function value using contourf and changed the colormap to something softer than jet. So far it looks quite good.
Now I want to draw a line representing the state over time in my space. That is also possible using the the plot command. But I want some more improvements: There is an additional state that is hidden for now (value 0...50). I would like the line color to change according to this hidden state. So in a sense to apply a separate colormap to the line plotted by plot like for example in a waterfall plot.
Is this (or something similar) possible using matlab?
Thanks
If you want to either use interpolated shading or have the colours change with the colour map, then you want to plot your data as a mesh and set the edgecolor property appropriately. Note that in order to plot it as a mesh, then you will need to duplicate it so that it has a size of at least 2 in each direction.
h = mesh([X(:) X(:)], [Y(:) Y(:)], [Z(:) Z(:)], [C(:) C(:)], ...
'EdgeColor', 'interp', 'FaceColor', 'none');
You may also want to look at the MeshStyle property, if you want to plot multiple lines simultaneously.
This solution is also much nicer than the one used in cline because it only creates one graphics object, rather than n.
Have a look into the cline.m function from file exchange, I think it is exactly what you need.
I can recommend the Colored line entry from the file exchange. It has good feedback and uses the color map to define the displayed colours, I've use it sucessfully on a number of projects.