MATLAB Answers

plot repeating x values

20 views (last 30 days)
Christopher Tran Rojas
Christopher Tran Rojas on 21 Oct 2020
Commented: Ameer Hamza on 21 Oct 2020
I have 2 vectors
m =
[0.05
0.06
0.08
0.10
0.15
0.25
0.35
0.45
0.35
0.25
0.15
0.10
0.08
0.06
0.05];
and
d = [
0.0340
0.0390
0.0495
0.0565
0.0735
0.0995
0.1205
0.1465
0.1270
0.1040
0.0760
0.0590
0.0510
0.0420
0.0360];
When I use plot(m,d) I get 2 overlapping lines. How do I get the plot showing the x axis from .05 to .45 to .05 symetrically instead of overlapping?

Answers (2)

Ameer Hamza
Ameer Hamza on 21 Oct 2020
Edited: Ameer Hamza on 21 Oct 2020
For that, you will need to modify the m-vector
[~, idx] = max(m);
m(idx+1:end) = 2*m(idx)-m(idx+1:end);
ax = axes();
plot(m, d);
ax.XTick = 0.05:0.1:0.85;
ax.XTickLabel = [0.05:0.1:0.45 0.35:-0.1:0.05];
  2 Comments
Ameer Hamza
Ameer Hamza on 21 Oct 2020
Check the updated code.

Sign in to comment.


Robert U
Robert U on 21 Oct 2020
Hi Christopher Tran Rojas,
you have to replace the xTickLabel-Strings. Otherwise an increasing value vector is expected. Plus, you have to take into account that your x-axis-values are not equally distributed.
m = [0.05 0.06 0.08 0.10 0.15 0.25 0.35 0.45 0.35 0.25 0.15 0.10 0.08 0.06 0.05];
d = [0.0340 0.0390 0.0495 0.0565 0.0735 0.0995 0.1205 0.1465 0.1270 0.1040 0.0760 0.0590 0.0510 0.0420 0.0360];
plotVecX = m-max(m);
plotVecX([false diff(plotVecX)<0]) = -plotVecX([false diff(plotVecX)<0]);
fh = figure;
ah = axes(fh);
plot(ah,plotVecX,d);
ah.XMinorGrid = 'on';
ah.XGrid = 'on';
ah.YMinorGrid = 'on';
ah.YGrid = 'on';
xTickVec = ah.XTick;
xTickVec(xTickVec>0) = -xTickVec(xTickVec>0);
xTickVec = xTickVec + max(m);
ah.XTickLabel = cellfun(@num2str,num2cell(xTickVec),'UniformOutput',false)';
Kind regards,
Robert

Tags

Products

Community Treasure Hunt

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

Start Hunting!