Break X-axis in Matlab
Show older comments
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
More Answers (1)
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
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


