Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Bring a curve to the front/top of a plot

Subject: Bring a curve to the front/top of a plot

From: Steve Amphlett

Date: 24 Sep, 2010 16:35:05

Message: 1 of 4

Someone at work asked this today and I could only offer an answer that kind of worked, but not for any reason I could explain.

Problem: A 2D plot has many curves on it. Given the handle of one of them, how to make it appear to be on top of the rest?

I figured that (h is a handle to one of the plotted curves):

set(h,'erasemode','xor');
set(h,'erasemode','background');

or

set(h,'erasemode','background');
set(h,'erasemode','xor');

seems to work, doesn't seem to matter which order, but both are required. I feel like I'm hacking though rather than understanding and then implementing.

Subject: Bring a curve to the front/top of a plot

From: Steven_Lord

Date: 24 Sep, 2010 16:49:15

Message: 2 of 4



"Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> wrote in message
news:i7ijvp$jlo$1@fred.mathworks.com...
> Someone at work asked this today and I could only offer an answer that
> kind of worked, but not for any reason I could explain.
>
> Problem: A 2D plot has many curves on it. Given the handle of one of
> them, how to make it appear to be on top of the rest?
>
> I figured that (h is a handle to one of the plotted curves):
>
> set(h,'erasemode','xor');
> set(h,'erasemode','background');
>
> or
>
> set(h,'erasemode','background');
> set(h,'erasemode','xor');
>
> seems to work, doesn't seem to matter which order, but both are required.
> I feel like I'm hacking though rather than understanding and then
> implementing.

Use UISTACK.

http://www.mathworks.com/help/techdoc/ref/uistack.html

% Sample data
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);

% Create two lines
h = plot(x, y1, 'k-', x, y2, 'r-');

% Make it easier to see which line is on top
set(h, 'LineWidth', 10)

% Execute these lines one at a time and see which color is on top of the
intersection
uistack(h(1), 'top')
uistack(h(2), 'top')

or you could try setting the ZData of one of the lines to be higher than the
ZData of the other lines. Personally, though, I'd use UISTACK.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Bring a curve to the front/top of a plot

From: Steve Amphlett

Date: 24 Sep, 2010 17:09:05

Message: 3 of 4

"Steven_Lord" <slord@mathworks.com> wrote in message <i7ikqb$dq9$1@fred.mathworks.com>...
>
>
> "Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> wrote in message
> news:i7ijvp$jlo$1@fred.mathworks.com...
> > Someone at work asked this today and I could only offer an answer that
> > kind of worked, but not for any reason I could explain.
> >
> > Problem: A 2D plot has many curves on it. Given the handle of one of
> > them, how to make it appear to be on top of the rest?
> >
> > I figured that (h is a handle to one of the plotted curves):
> >
> > set(h,'erasemode','xor');
> > set(h,'erasemode','background');
> >
> > or
> >
> > set(h,'erasemode','background');
> > set(h,'erasemode','xor');
> >
> > seems to work, doesn't seem to matter which order, but both are required.
> > I feel like I'm hacking though rather than understanding and then
> > implementing.
>
> Use UISTACK.
>
> http://www.mathworks.com/help/techdoc/ref/uistack.html
>
> % Sample data
> x = 0:0.1:2*pi;
> y1 = sin(x);
> y2 = cos(x);
>
> % Create two lines
> h = plot(x, y1, 'k-', x, y2, 'r-');
>
> % Make it easier to see which line is on top
> set(h, 'LineWidth', 10)
>
> % Execute these lines one at a time and see which color is on top of the
> intersection
> uistack(h(1), 'top')
> uistack(h(2), 'top')
>
> or you could try setting the ZData of one of the lines to be higher than the
> ZData of the other lines. Personally, though, I'd use UISTACK.

Thanks.

Subject: Bring a curve to the front/top of a plot

From: Patrick

Date: 25 Sep, 2012 19:24:08

Message: 4 of 4

That works....however, it's worth noting that if you use UISTACK, you should do it after you declare your legend. Otherwise it will mess with the Legend order, too. (It did for me anyways.)

For example, in this order
     h = plot(x1,y1,'r'); hold all %nominal case
     plot(x2,y2,'b') %test case
     uistack(h, 'top')
     legend('Nominal','TestResult'))
UISTACK will moves your 'top' plot to the bottom of the legend entries. Effectively, misrepresenting your data. It was intended that 'Nominal' was red, but in this example in the legend it is shown blue instead.
What if you want the top spot in the legend entries, and your data curve on top of the rest of the curves? Just be sure to call your plot to the front, AFTER you place the legend, as in this example.
     h = plot(x1,y1); hold all %nominal case
     plot(x2,y2) %test case
     legend('Nominal','TestResult'))
     uistack(h, 'top')

For me it was important to have the first legend entry also be the most prominent data displayed, and this worked for me.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us