Use LaTeX for TickValues in a elegant way
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
qilin guo
el 19 de Mzo. de 2022
Respondida: Walter Roberson
el 19 de Mzo. de 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.
0 comentarios
Respuesta aceptada
Walter Roberson
el 19 de Mzo. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Interaction Control en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!