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:
Plots with both lines and markers

Subject: Plots with both lines and markers

From: drgz

Date: 2 Dec, 2009 20:05:19

Message: 1 of 6

Hello,

just curious about something. I'm trying to plot several data vectors in a single plot and separate the different vectors by using plot/line markers.

The thing is that I'd like to have a good resolution on my plot, i.e. as few straight lines between the data points (no interpolating), but without ending up with i.e. 1000 line markers directly next to each other, making the plot a thick line of i.e. square line markers.

As for now, the best way to do this as I know of is to first plot the data vector as a "continuous" line (regular plot), and in the same figure plot just a few line markers of the same data vector with a convenient step between each marker, and for the plot this looks exactly as I want it to look.

The code for such a plot would be

plot(x, f(x), '-r', x(1:dx:end), f(x(1:dx:end), 'sr').

But as soon as I want to add a legend, due to plotting several vectors in the same figure, the legend sample will either be the line, or just the line marker used.

This scenario is illustrated in the PDF I've uploaded here:

http://dump.no/files/1ece075f1871/test.pdf

As you can see in the upper figure the line markers are separated nicely, and I still get the smooth line showing the data. But for the legend, I can't find a way to get the two legend samples combined, as in the lower figure, where I've used the code

plot(x(1:dx:end), f(x(1:dx:end)). '-rs' )

to plot the data.

So to my first question: is there any way to combine the plot in the upper figure with the legend from the lower one?

For only one set of data, the upper figure would of course do fine, as there wouldn't be any need for the legend, but when plotting several sets of data, it would definitely look better if I could have legend samples as in the lower figure (IMO). I assume this isn't possible, but who knows, I might be lucky.

Best regards,

drgz

Subject: Plots with both lines and markers

From: Rune Allnor

Date: 2 Dec, 2009 20:28:06

Message: 2 of 6

On 2 Des, 21:05, "drgz " <syre...@hotmail.com> wrote:

> So to my first question: is there any way to combine the plot in the upper figure with the legend from the lower one?

Plot an invisible line with the markerstyle you emulated,
and use this style in the legend:

LL = plot(rand(1,2),rand(1,2),'-rfs','visible','off');
legend(LL,'Text');

Rune

Subject: Plots with both lines and markers

From: drgz

Date: 2 Dec, 2009 20:40:05

Message: 3 of 6

Rune Allnor <allnor@tele.ntnu.no> wrote in message <194d482b-4cea-49c1-9f25-5ff5e134f3e8@m16g2000yqc.googlegroups.com>...
> On 2 Des, 21:05, "drgz " <syre...@hotmail.com> wrote:
>
> > So to my first question: is there any way to combine the plot in the upper figure with the legend from the lower one?
>
> Plot an invisible line with the markerstyle you emulated,
> and use this style in the legend:
>
> LL = plot(rand(1,2),rand(1,2),'-rfs','visible','off');
> legend(LL,'Text');
>
> Rune

Thanks a lot! Exactly what I was looking for, works like a charm.

drgz

Subject: Plots with both lines and markers

From: lily

Date: 6 Jan, 2010 03:21:07

Message: 4 of 6

What you want is to insert legend for only the markers lines of a graphic?

clf;
x=-5:0.1:5;
dx=2;
h=plot(x, sin(x), '-r', x(1:dx:end), sin(x(1:dx:end)), 'sb');
%% get the handles of all the lines in the graphic

legend(h(2),'marker');
%% h(2) means the handle of second line in the graphic, then legend it

Subject: Plots with both lines and markers

From: lily

Date: 6 Jan, 2010 03:24:23

Message: 5 of 6

What you want is to insert legend for only the markers lines of a graphic?

clf;
x=-5:0.1:5;
dx=2;
h=plot(x, sin(x), '-r', x(1:dx:end), sin(x(1:dx:end)), 'sb');
%% get the handles of all the lines in the graphic

legend(h(2),'marker');
%% h(2) means the handle of second line in the graphic, then legend it

Subject: Plots with both lines and markers

From: drgz

Date: 18 Jan, 2010 18:20:23

Message: 6 of 6

Read Rune Allnor message to see what I wanted - his suggestion was exactly what I was looking for.

"lily " <mediocrity111@hotmail.com> wrote in message <hi0vp7$3ac$1@fred.mathworks.com>...
> What you want is to insert legend for only the markers lines of a graphic?
>
> clf;
> x=-5:0.1:5;
> dx=2;
> h=plot(x, sin(x), '-r', x(1:dx:end), sin(x(1:dx:end)), 'sb');
> %% get the handles of all the lines in the graphic
>
> legend(h(2),'marker');
> %% h(2) means the handle of second line in the graphic, then legend it

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