Use LaTeX for TickValues in a elegant way

2 次查看(过去 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.

采纳的回答

Walter Roberson
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 个)

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by