Problem with XTick: location of XTick doesn't match data.
3 views (last 30 days)
Show older comments
Xiaohan Du
on 23 May 2018
Commented: Xiaohan Du
on 25 May 2018
Hi all,
I have the following script to plot 2 curves with 2 x-axis on top and bottom:
clear; clc; clf;
errxOri = [4 6 8 10 13 14 18 20 24 34 39];
errOriMax = [0.5279 0.37978 0.30287 0.19383 0.13164 0.11567 0.093856 ...
0.080826 0.05997 0.046702 0.033662];
errxPro = [4 7 8 10 12 14 22 26 30 33 38];
errProMax = [0.5279 0.38693 0.21168 0.16633 0.1322 0.1168 0.10034...
0.068223 0.060327 0.047501 0.032004];
figure(1)
ha1 = semilogy(errxOri, errOriMax, 'b-o', 'MarkerSize', 10, 'lineWidth', 3);
hold on
ha2 = semilogy(errxPro, errProMax, 'r-+', 'MarkerSize', 10, 'lineWidth', 3);
ax1 = gca;
ax1.XColor = 'b';
ax1.XTick = errxOri;
set(gca,'fontsize',20)
ax2 = axes(...
'Position', ax1.Position,...
'XAxisLocation', 'top',...
'Color', 'none',...
'YTick', [],...
'XLim', [0 39],...
'XTick', errxPro);
ax2.XColor = 'r';
ax2.XTick = errxPro;
set(gca,'fontsize',20)
grid on
But running it gives the following plot:

As you can see it does most of the job, just the xticks on top doesn't match the data (red cross), you can see the red cross is slightly offset from the vertical grid line. Why does this happen? How to fix it?
Many thanks!
0 Comments
Accepted Answer
Walter Roberson
on 23 May 2018
You do not specify the xlim for the first axes and the two axes are not linked.
More Answers (0)
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!