Clear Filters
Clear Filters

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

12 views (last 30 days)
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.

Accepted Answer

Aquatris
Aquatris on 24 Jun 2024
Edited: Aquatris on 24 Jun 2024
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 Comments
Aquatris
Aquatris on 24 Jun 2024
Edited: Aquatris on 24 Jun 2024
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];

Sign in to comment.

More Answers (1)

Pavan Sahith
Pavan Sahith on 24 Jun 2024
Edited: Pavan Sahith on 24 Jun 2024
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 Comment
Andrea Improta
Andrea Improta on 24 Jun 2024
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?

Sign in to comment.

Tags

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!