Break X-axis in Matlab

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')

 Accepted Answer

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 Comments

Thank you for your help
1)First, It showed the error, What might be the problem?
Error using linkaxes (line 114)
Second input argument must be one of 'x', 'y', 'xy', or 'off'.
Error in untitled (line 31)
linkaxes([ax1 ax2], 'x ')
2) But still want the closed box ( same as attached in the png plot above) in the final plot (i.e. plot with right y axis and top x axis). Can you please help me more ? How can I do that?
3)And also I want to break the X axis ( giving symbo //) . How can I do that? can You please help me more as i am beginner in matlab?
Thank you
  1. you put it 'x'. i think you would be 'y'. in my code i dont get any error. please check my code.
  2. ....
  3. try this code. you have to adjuct the position
x = -50*pi:0.1:50*pi;
y=sin(0.1*x);
figure(10)
t = tiledlayout(1,2,'TileSpacing','compact');
bgAx = axes(t,'XTick',[],'YTick',[]);
box on;
bgAx.Layout.TileSpan = [1 2];
% Create first plot
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)
box on;
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')
axes('Position',[.49 0.07 .05 .1]);
px=[1 5];
py1=[1 2];
height=.5;
py2=py1+height;
plot(px,py1,'k','LineWidth',2);hold all;
plot(px,py2,'k','LineWidth',2);hold all;
fill([px flip(px)],[py1 flip(py2)],'w','EdgeColor','none');
box on;
axis off;
Babu Sankhi
Babu Sankhi on 1 Feb 2022
Edited: Babu Sankhi on 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 on 3 Feb 2022
Edited: Babu Sankhi on 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

Sign in to comment.

More Answers (1)

Voss
Voss on 2 Feb 2022
Edited: Voss on 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

Categories

Find more on Graphics Performance in Help Center and File Exchange

Asked:

on 31 Jan 2022

Edited:

on 3 Feb 2022

Community Treasure Hunt

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

Start Hunting!