Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Changing the legend linestyle ?

395 views
Skip to first unread message

Ash

unread,
Dec 16, 2008, 12:23:02 PM12/16/08
to
Hello all !
I was wondering, if there is way to change the graphic component of each legend entry ? The reason I ask is as follows:
I am generating a plot with 10 different curves. These 10 are divided into two groups - with the LineStyle for each Group being different.
However, the last two curves to be added to this plot belong to the same group - thus when I add the following line:

legend('G1','G2')

The line style shown for both entries is the same. Is there anyway - to manually change this after the plots have been generated ? I am looking for a way to do this within the code itself. I have the LineSpec for each group before plotting.

Regards,

-A

Steven Lord

unread,
Dec 16, 2008, 12:37:25 PM12/16/08
to

"Ash" <wired.ashu....@g.m.a.i.l.com> wrote in message
news:gi8o5m$7rm$1...@fred.mathworks.com...


% Setup
x = 1:10;
h = zeros(1, 5);
axis([0 11 0 51])
linestyles = {'-', ':','-.','--',':'};
hold on

% Create 5 lines and store their handles
for k = 1:5
h(k) = plot(x, k*x, linestyles{k});
end

% Create the legend -- lines 2 and 5 should have the same line style
legend(h)

% Change line 2's LineStyle and both the plot and the legend will update
set(h(2), 'LineStyle', '-')


--
Steve Lord
sl...@mathworks.com


Kevin Neilsen

unread,
Dec 16, 2008, 12:43:03 PM12/16/08
to
the best way to do it is when you are plotting. i.e.

plot(x,y,'b*') will plot blue for b and *

plot(x,y,'r-','linewidth',4) will plot a thick red dashed line

type help plot for more options

the legend will account for these changes

Ash

unread,
Dec 16, 2008, 12:58:04 PM12/16/08
to
thanks for the responses guys..

however, I think i was not clear in my question. So here is another attempt:

consider the following:
----------------------------------------
k1=1:5;k2=6:10;
jj=rand(10,10);
for i=1:10
if(~isempty(find(k1==i)))
plot(sort(rand(10,1)),'-+');
else
plot(sort(rand(10,1)),':');
end
hold on
end

legend('S1','S2')
-----------------------------------

Running the above snippet - will show 10 curves, divided into TWO types of line styles. however the Legend shows two entries with the SAME line style. I would like to change the legend to highlight the two different types of lines being used to plot.

(As a workaround right now I am plotting the the first series from each group, adding the legend and then plotting the rest of each group - but i think there has to be a more elegant way of doing this..)

thanks again..

0 new messages