Break X-axis in Matlab

254 Ansichten (letzte 30 Tage)
Babu Sankhi
Babu Sankhi am 31 Jan. 2022
Bearbeitet: Babu Sankhi am 3 Feb. 2022
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')

Akzeptierte Antwort

Arif Hoq
Arif Hoq am 1 Feb. 2022
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 Kommentare
Babu Sankhi
Babu Sankhi am 1 Feb. 2022
Bearbeitet: Babu Sankhi am 1 Feb. 2022
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 am 3 Feb. 2022
Bearbeitet: Babu Sankhi am 3 Feb. 2022
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Voss
Voss am 2 Feb. 2022
Bearbeitet: Voss am 2 Feb. 2022
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

Kategorien

Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by