Use LaTeX for TickValues in a elegant way
1 次查看(过去 30 天)
显示 更早的评论
Dear MATLAB users,
I would like to use LaTeX style numbers (especially the minus signs) for TickValues. I find this is possible but I am afraid that I am using a wrong way.Here are the minimum working example.
close all; clc;
x = linspace(-2,2,101);
y = [-x.^2; x-1];
fig = figure;
% In my real codes, I am using a for-loop to execute axes statements
% I like to use axes rather than subplot because I could control the
% size of the plot region in a precise way as you can see here.
ax1 = axes(fig, 'Unit', 'Centimeter', 'Position', [2,2,5,5]); hold on;
plot(x, y(1,:), 'ko-'); % and many plot statements go here
hold off;
ax = axes(fig, 'Unit', 'Centimeter', 'Position', [8,2,5,5]); hold on;
plot(x, y(2,:), 'ko-'); % and many plot statements go here
hold off;
% Here I would like to gather all handles of axes together
ax = findall(gcf, 'type', 'axes');
% By the way, I don't know why the handles are returned in a reversed oder,
% which means that the handle of the first axes is the last element of ax.
ax = fliplr(ax');
% This is my real question: How to set ax(1).YAxis.TickLabels in a simpler way but the results are the same?
% (e.g., is it possible to use ax(1).YAxis.TickLabels = -4:1:0 ?)
ax(1).YAxis.TickValues = -4:1:0;
ax(1).YAxis.TickLabels = {'$-4$','$-3$','$-2$','$-1$','$0$'};
ax(1).YAxis.TickLabelInterpreter = 'LaTeX';
Here are the results. Please note that the difference on the style of the TickValues between left and right subfigures.
Best regards,
Qilin.
0 个评论
采纳的回答
Walter Roberson
2022-3-19
Use TickLabelFormat: that way if you resize or pan the axes, the new labels will automatically be formatted properly.
x = linspace(-2,2,101);
y = [-x.^2; x-1];
fig = figure;
% In my real codes, I am using a for-loop to execute axes statements
% I like to use axes rather than subplot because I could control the
% size of the plot region in a precise way as you can see here.
ax1 = axes(fig, 'Unit', 'Centimeter', 'Position', [2,2,5,5]); hold on;
plot(x, y(1,:), 'ko-'); % and many plot statements go here
hold off;
ax = axes(fig, 'Unit', 'Centimeter', 'Position', [8,2,5,5]); hold on;
plot(x, y(2,:), 'ko-'); % and many plot statements go here
hold off;
% Here I would like to gather all handles of axes together
ax = findall(gcf, 'type', 'axes');
% By the way, I don't know why the handles are returned in a reversed oder,
% which means that the handle of the first axes is the last element of ax.
ax = fliplr(ax');
% This is my real question: How to set ax(1).YAxis.TickLabels in a simpler way but the results are the same?
% (e.g., is it possible to use ax(1).YAxis.TickLabels = -4:1:0 ?)
ax(1).YAxis.TickValues = -4:1:0;
ax(1).YAxis.TickLabels = {'$-4$','$-3$','$-2$','$-1$','$0$'};
ax(1).YAxis.TickLabelInterpreter = 'LaTeX';
ax(2).YAxis.TickValues = -3:1:1;
ax(2).YAxis.TickLabelFormat = '$%g$';
ax(2).YAxis.TickLabelInterpreter = 'LaTeX'
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interaction Control 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!