How to set upper x-axis ticks values and locations?

조회 수: 26 (최근 30일)
Andrea Improta
Andrea Improta 2024년 6월 24일
편집: Andrea Improta 2024년 6월 24일
I am trying to set the upper x-axis (ax2) tick values and locations:
x_a = linspace(71.8131,36.9931,10);
y = linspace(0.16,1,10);
x_b = linspace(0.0163,0.0069,10)./0.1016;
figure;
ax1 = axes;
plot(ax1, x_a, y, 'k');
ax2 = axes('Position', ax1.Position, ...
'XAxisLocation', 'top', ...
'YAxisLocation', 'right', ...
'Color', 'none', ...
'XColor', '#88419d', ...
'YColor', 'none', ...
'Box', 'off');
ax2.YLim = ax1.YLim;
new_tick_locations = linspace(30, 80, 6);
new_tick_labels = arrayfun(@(val) sprintf('%.2f', val/500), new_tick_locations, 'UniformOutput', false);
set(ax2, 'XTick', new_tick_locations, 'XTickLabel', new_tick_labels);
% linkaxes([ax1 ax2], 'xy');
hold(ax2, 'on');
plot(ax2, x_b, y, 'r');
This code doesn't show the ax2 ticks at all. I need them to show and to be aligned with the lower x axis labels [30, 40, 50, 60, 70, 80]. How can I fix this? Thank you in advance.

채택된 답변

Aquatris
Aquatris 2024년 6월 24일
편집: Aquatris 2024년 6월 24일
You were changing the label name at 30 60, instead of their actual values, so here is one dirty way but I recommend you follow the tilegraph method exaplined here for cleaner and better plot:
x_a = linspace(71.8131,36.9931,10);
y = linspace(0.16,1,10);
x_b = linspace(0.0163,0.0069,10)./0.1016;
figure;
ax1 = axes;
plot(ax1, x_a, y, 'k');
ax2 = axes('Position', ax1.Position, ...
'XAxisLocation', 'top', ...
'YAxisLocation', 'right', ...
'Color', 'none', ...
'XColor', '#88419d', ...
'YColor', 'none', ...
'Box', 'off');
ax2.YLim = ax1.YLim;
new_tick_locations = linspace(30, 80, 6);
new_tick_labels = arrayfun(@(val) sprintf('%.2f', val/500), new_tick_locations, 'UniformOutput', false);
set(ax2, 'XTick', new_tick_locations/500, 'XTickLabel', new_tick_labels);
% linkaxes([ax1 ax2], 'xy');
hold(ax2, 'on');
plot(ax2, x_b, y, 'r');
ax1.Box = 'off';
ax2.XLim = new_tick_locations([1 end])/500;
  댓글 수: 3
Aquatris
Aquatris 2024년 6월 24일
편집: Aquatris 2024년 6월 24일
Then you can set the xlims to define their minimum and maximum positions:
x_a = linspace(71.8131,36.9931,10);
y = linspace(0.16,1,10);
x_b = linspace(0.0163,0.0069,10)./0.1016;
figure;
ax1 = axes;
plot(ax1, x_a, y, 'k');
ax2 = axes('Position', ax1.Position, ...
'XAxisLocation', 'top', ...
'YAxisLocation', 'right', ...
'Color', 'none', ...
'XColor', '#88419d', ...
'YColor', 'none', ...
'Box', 'off');
ax2.YLim = ax1.YLim;
new_tick_locations = linspace(30, 80, 6);
new_tick_labels = arrayfun(@(val) sprintf('%.2f', val/500), new_tick_locations, 'UniformOutput', false);
set(ax2, 'XTick', new_tick_locations/500, 'XTickLabel', new_tick_labels);
% linkaxes([ax1 ax2], 'xy');
hold(ax2, 'on');
plot(ax2, x_b, y, 'r');
ax1.Box = 'off';
%% to make sure 30 aligns with 0.06 and 80 aligns with 0.16
ax1.XLim = [30 80];
ax2.XLim = [0.06 0.16];
Andrea Improta
Andrea Improta 2024년 6월 24일
Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Pavan Sahith
Pavan Sahith 2024년 6월 24일
편집: Pavan Sahith 2024년 6월 24일
Hello Andrea,
I see that you are facing an issue with the new_tick_locations you are setting for ax2. The new_tick_locations should be within the range of ax2.XLim, which is inherited from ax1.XLim when ax2 is created.
I can see that new_tick_locations you provided (30 to 80) do not match the range of x_b values.To fix this, you need to set the XTick values for ax2 within the range of x_b. you can refer to this sample code:
x_a = linspace(71.8131, 36.9931, 10);
y = linspace(0.16, 1, 10);
x_b = linspace(0.0163, 0.0069, 10) ./ 0.1016;
figure;
ax1 = axes;
plot(ax1, x_a, y, 'k');
ax2 = axes('Position', ax1.Position, ...
'XAxisLocation', 'top', ...
'YAxisLocation', 'right', ...
'Color', 'none', ...
'XColor', '#88419d', ...
'YColor', 'none', ...
'Box', 'off');
ax2.YLim = ax1.YLim;
ax2.XLim = [min(x_b), max(x_b)]; % Set XLim for ax2 to match the range of x_b
% Set new tick locations and labels within the range of x_b
new_tick_locations = linspace(min(x_b), max(x_b), 6);
new_tick_labels = arrayfun(@(val) sprintf('%.2f', val), new_tick_locations, 'UniformOutput', false);
set(ax2, 'XTick', new_tick_locations, 'XTickLabel', new_tick_labels);
hold(ax2, 'on');
plot(ax2, x_b, y, 'r');
following similar adjustments, the ticks for ax2 can be displayed correctly.
you can go through this MathWorks documentation which will help you understand better
you can aslo refer to this similar MATLAB answer which might help
Hope this helps you moving forward
  댓글 수: 1
Andrea Improta
Andrea Improta 2024년 6월 24일
Hi, thanks for your answer. I was thinking that your solution replaces the ticks that used to be at the locations
new_tick_locations = linspace(min(x_b), max(x_b), 6);
with some new tick values (new_tick_labels). Am I wrong?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

태그

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by