plot repeating x values

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?

回答(2 个)

Ameer Hamza
Ameer Hamza 2020-10-21
编辑:Ameer Hamza 2020-10-21
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 个评论

Thank You, but the x axis is showing 0 to .9 instead of 0 up to .45 and back to 0.
[~, idx] = max(m);
m(idx+1:end) = 2*m(idx)-m(idx+1:end);
plot(m,d,'-x')
grid on
Check the updated code.

请先登录,再进行评论。

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

类别

帮助中心File Exchange 中查找有关 Annotations 的更多信息

产品

标签

Community Treasure Hunt

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

Start Hunting!

Translated by