How do I divide my y axis into sections / exclude a region of the y axis from plotting?

I have essentially a square wave that I want to plot, but I only care about what happens at the bottom and top of the square wave. I'd like to split my y axis into two portions that display a range around the maximum of the square wave and the minimum of the square wave.
One option that I already know of is to use subplots:
subplot(2,1,1)
plot(x,y)
ylim([0.99 1.01]
subplot(2,1,2)
plot(x,y)
ylim([-1.01 -0.99])
But I would rather have the data on one axis so that it's easier visually to see what's going on. Is there a way to do this?

Answers (2)

x=0:.1:10;
y=1.01*sin(x)
[ax,h1,h2]=plotyy(x,y,x,y)
set(ax(1),'ylim',[0.99 1.01])
set(ax(2),'ylim',[-1.01 -0.99])

1 Comment

Thanks Azzi. The problem with this method is that it overlaps the y axes. I can work around it using the code below, but I was hoping for something more elegant. But this is definitely a workaround if there's no better way.
x=0:.01:10;
y=1.01*sin(x)
[ax,h1,h2]=plotyy(x(y>=0.99),y(y>=0.99),x(y<=-0.99),y(y<=-0.99))
set(ax(1),'ylim',[0.97 1.01])
set(ax(2),'ylim',[-1.01 -0.97])

Sign in to comment.

My suggestion:
x = linspace(0,10);
y1 = [x(1:50) 10-x(51:100)]; % Generate triangle
figure(1)
plot(x,y1) % Plot entire triangle
grid
yL = y1;
yH = y1;
yL(yL>1.5) = NaN; % Vanish everything > 1.5 in ‘yL’
yH(yH<3.5) = NaN; % Vanish everything < 3.5 in ‘yH’
yC = [yL; yH-2*ones(size(yH))]; % Combine ...
figure(2)
plot(x, yC)
grid
Salt to taste...

2 Comments

Thanks Star Strider. This works visually, but changes the values of the curves (i.e. the value of the triangle peak is shifted from 5 to 3).
I ended up using this shared file, < Break Y Axis >. I haven't looked into the code to see exactly how it works, but it seems to work well for what I needed.
Thanks for the suggestions!
Seems I wasn’t clear on what you wanted. I’m glad you found a solution.

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Asked:

on 24 Mar 2014

Commented:

on 25 Mar 2014

Community Treasure Hunt

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

Start Hunting!