Use LaTeX for TickValues in a elegant way
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
qilin guo
le 19 Mar 2022
Réponse apportée : Walter Roberson
le 19 Mar 2022
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/932629/image.png)
0 commentaires
Réponse acceptée
Walter Roberson
le 19 Mar 2022
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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!