Adding labels to plots with LaTeX syntax

回答(2 个)

Take a look at the text function with the latex interpreter. Here's a working example:
plot(0:.1:pi,sin(0:.1:pi))
text(pi/2,.9,'$$t = \frac{\pi}{2}$$', ...
'Interpreter','latex', ...
'FontSize',14, ...
'HorizontalAlignment','center')
There's also the annotation function if you want to add other shapes but that's a bit more complex.
In the text call, specify 'Interpreter','latex', however the more interesting part of this was (finally) figuring out how to correctly place the annotation objects.
This combines both of them —
x = linspace(-2, 1, 250);
y = 3.5*exp(-(x+1).^2*150);
figure
plot(x, y, '--k')
ylim([-1 4])
yline(0, '-k')
text(0, 3.5, '$\it{t=0}$', 'Interpreter','latex', 'FontSize',15, 'Horiz','center')
text(-1.5, 1.75,'$\alpha\_$', 'Interpreter','latex', 'FontSize',15, 'Horiz','center')
xs = @(x,pos,xlim) pos(3)*(x-min(xl))/diff(xl)+pos(1);
ys = @(y,pos,ylim) pos(4)*(y-min(yl))/diff(yl)+pos(2);
xl = xlim;
yl = ylim;
pos = gca().Position;
xs = @(x,pos,xlim) pos(3)*(x-min(xl))/diff(xl)+pos(1); % 'x' Annotation Position Function
ys = @(y,pos,ylim) pos(4)*(y-min(yl))/diff(yl)+pos(2); % 'y' Annotation Position Function
% Qy = ys([2.5 3.5],pos,ylim)
% Qx = xs([1 1]*-1.5,pos,xlim)
annotation('arrow', xs([-1.5 -1.5],pos,xlim), ys([1.5 0],pos,ylim))
annotation('arrow', xs([-1.5 -1.5],pos,xlim), ys([2.0 3.5],pos,ylim))
I am not certain what the other curves represent, so I did not draw them.
.

类别

帮助中心File Exchange 中查找有关 Axis Labels 的更多信息

产品

版本

R2023b

提问:

2023-9-18

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by