Break X-axis in Matlab

266 ビュー (過去 30 日間)
Babu Sankhi
Babu Sankhi 2022 年 1 月 31 日
編集済み: Babu Sankhi 2022 年 2 月 3 日
Hi All,
I have data in extreme ends (i.e. -100 to -70) and ( ( I.e. 70 to 100) . So I want to break x-axis ( eg. from -70 to 70 ) while plotting in a single plot . It would be great if you let me know how can I do it, which function can I use for that? Actually I want to plot like this;
thank you
N=(1:100)
Y=(1:2:200);
figure (1)
plot (N,Y,'ro')

採用された回答

Arif Hoq
Arif Hoq 2022 年 2 月 1 日
I think you are looking for this:
x = -50*pi:0.1:50*pi;
y=10*sin(0.1*x);
figure(10)
t = tiledlayout(1,2,'TileSpacing','compact');
bgAx = axes(t,'XTick',[],'YTick',[],'Box','off');
bgAx.Layout.TileSpan = [1 2];
ax1 = axes(t);
plot(ax1,x,y)
xline(ax1,15,':');
ax1.Box = 'off';
xlim(ax1,[-100 -70])
xlabel(ax1, 'First Interval')
% Create second plot
ax2 = axes(t);
ax2.Layout.Tile = 2;
plot(ax2,x,y)
xline(ax2,45,':');
ax2.YAxis.Visible = 'off';
ax2.Box = 'off';
xlim(ax2,[70 100])
xlabel(ax2,'Second Interval')
% Link the axes
linkaxes([ax1 ax2], 'y')
title(t,'Attenuated Cosine Function')
  4 件のコメント
Babu Sankhi
Babu Sankhi 2022 年 2 月 1 日
編集済み: Babu Sankhi 2022 年 2 月 1 日
Hi Arif,
It makes sense, But actually it does not look that much as nice as I attached in the png in my question. Actually I am looking for; I should make a single plot for the data from -20 to 20 ( not like two plots you have done so far) then break the axis ( eg. from -14 to 14). Is there any way to break axis after single plot?
Thank you for your great help.
Babu Sankhi
Babu Sankhi 2022 年 2 月 3 日
編集済み: Babu Sankhi 2022 年 2 月 3 日
Hi Arif,
Your second answer worked best for me . But only the problem is that How can I lessen the the distance between two plots ( so that they look like they are from same set of data and apppear very small break). See in your above plot there is wide gap beetween -70 and 70. I want to decrease that gap ,how is it possible?
thank you

サインインしてコメントする。

その他の回答 (1 件)

Voss
Voss 2022 年 2 月 2 日
編集済み: Voss 2022 年 2 月 2 日
Maybe an approach like this would work:
x = -20:0.1:20;
xtl = [-18 -16 -14 14 16 18];
xlim = [-19 -13; 13 19];
gap_size = 0.05;
% y = randn(size(x));
y = NaN(size(x));
y(x < 0) = 200*exp(-(x(x < 0)+16).^2/5);
y(x >= 0) = 200*exp(-(x(x >= 0)-16).^2/5);
y2 = y+randn(size(y))*10;
[xt,idx1,idx2] = x_transform(x,xlim,gap_size);
yt = [y(idx1) NaN y(idx2)];
y2t = [y2(idx1) NaN y2(idx2)];
ax = gca();
line( ...
'Parent',ax, ...
'XData',xt, ...
'YData',y2t, ...
'LineWidth',2, ...
'Color','r', ...
'Marker','o', ...
'LineStyle','none');
line( ...
'Parent',ax, ...
'XData',xt, ...
'YData',yt, ...
'LineWidth',2, ...
'Color','b');
x_text = x_transform([-16 16],xlim,gap_size);
text( ...
'Parent',ax, ...
'Position',[x_text(1) 1.1*max(y(idx1))], ...
'String','Stokes', ...
'VerticalAlignment','bottom', ...
'HorizontalAlignment','center', ...
'FontSize',20);
text( ...
'Parent',ax, ...
'Position',[x_text(end) 1.1*max(y(idx2))], ...
'String','anti-Stokes', ...
'VerticalAlignment','bottom', ...
'HorizontalAlignment','center', ...
'FontSize',20);
xtlt = x_transform(xtl,xlim,gap_size);
set(ax, ...
'Box','on', ...
'LineWidth',2, ...
'XLim',[0 1], ...
'XTick',xtlt(~isnan(xtlt)), ...
'XTickLabel',xtl);
yl = get(ax,'YLim');
set(ax,'YLimMode','manual');
patch( ...
'Parent',ax, ...
'XData',0.5+gap_size/2*[-1 0 1 0], ...
'YData',yl(1)+(yl(2)-yl(1))*[-1 1 1 -1]/30, ...
'FaceColor','w', ...
'EdgeColor','none', ...
'Clipping','off');
line( ...
'Parent',ax, ...
'XData',0.5+gap_size/2*[-1 0 NaN 1 0], ...
'YData',yl(1)+(yl(2)-yl(1))*[-1 1 NaN 1 -1]/30, ...
'Color','k', ...
'LineWidth',2, ...
'Clipping','off');
set([get(ax,'YAxis') get(ax,'XAxis')],'FontSize',20);
set( ...
get(ax,'YLabel'), ...
'String','Intensity (a.u.)', ...
'FontSize',20);
function [x,idx1,idx2] = x_transform(x,xlim,gap_size)
idx1 = x <= xlim(1,2);
idx2 = x >= xlim(2,1);
w = (1-gap_size)/2;
x = [ ...
(x(idx1)-xlim(1,1))/(xlim(1,2)-xlim(1,1))*w NaN ...
(x(idx2)-xlim(2,1))/(xlim(2,2)-xlim(2,1))*w+1-w ...
];
end

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by