Reputation: 386
My goal: use text
command to put text near an xtick
, but not via xticklabel
.
Example: Suppose I have the following code
figure
plot([1:10],[1:10])
set(gca, 'XTick',[3])
set(gca, 'XTickLabel',{''})
Now, I would like to write something like (this is not a working code)
ticklocs = get(gca,'xtick',locations);
text(locations(1),locations(2), 'my cool text here');
I know that that I can put the text in the XTickLabel
format, but it does not enable me all the text flexibility that text
allows me.
If you do know how to put Latex in the xticklabel
, it is also interesting, but an off topic for this post (just comment).
Upvotes: 1
Views: 497
Reputation: 65430
You can get the xtick
property which will give you the x value of the tick mark. You can then use the axes ylim
property to determine the minimum y
value which will be it's rough y
location. You can then apply padding as necessary to these values and use text
to display text wherever.
ax = axes();
% Get the upper and lower y limits
ylims = get(ax, 'ylim');
% Get the x positions of the tick marks
xticks = get(ax, 'xtick');
% For each tick we're going to use padding between -10% of the ylimits to 10%
padding = linspace(-0.1, 0.1, numel(xticks));
for k = 1:numel(padding)
% Compute the y value based upon the desired padding and the lower y limit
yvalue = ylims(1) - diff(ylims) * padding(k);
% Display the text
text(xticks(k), yvalue, sprintf('Label %d', k), 'HorizontalAlignment', 'center');
hold on
end
That being said, since R2015a, you can put LaTeX directly in your xtick labels and use the TickLabelInterpreter
property of the axes
to specify that you'd like to use the LaTeX interpreter.
axes('Xtick', [0 0.5 1], ...
'XTickLabel', {'10\circ', 'x^{1}', 'Y_2'}, ...
'TickLabelInterpreter', 'latex')
Upvotes: 1