# MatLab plots with legends.

• Feb 28th 2009, 11:47 AM
Mush
MatLab plots with legends.
Hello.

Is there any way to add a legend to a diagram such that the information int he legend is actually independent of the information on the graph? Such that the information in the legend describes a property of a group of objects, rather than a specific object?

For example, I have written a program which takes numerous complex numbers, z1, z2, z3, z4...zn and inserts them into an iterative method whereby they converge to either 1, -1, i, -i. I have then plotted each point, z1...zn, as a star.

However, each point is colour coded according to which number it converges to in the iteration. So I may have 3 points that converge to 1, and hence are plotted in green, and 2 points that converge to i, and hence are plotted in red. So what I want my legend to show is

* converges to 1
* converges to -1
* converges to i
* converges to -i

So as you see, the legend refers not to specific points, but a collection of points. And I want my legend to show, for example, the 4th option, even if no number in the iteration ACTUALLY converge to -i.

So how do I do that?!
• Feb 28th 2009, 12:00 PM
Moo
hello,

I'm sorry I know only Scilab, which is the free version of Matlab, so maybe it's very similar (especially the commands)

Suppose you have two plots
plot2d(s,N,4);
plot2d(s,N1,6);

4 and 6 are the colours.

legends(['Legend corresponding to colour 4','Legend corresponding to colour 6'],[4,6])

in scilab, you just have to click on the window where the graph is in order to place the legends. Dunno how it is in Matlab
• Feb 28th 2009, 12:18 PM
Mush
Quote:

Originally Posted by Moo
hello,

I'm sorry I know only Scilab, which is the free version of Matlab, so maybe it's very similar (especially the commands)

Suppose you have two plots
plot2d(s,N,4);
plot2d(s,N1,6);

4 and 6 are the colours.

legends(['Legend corresponding to colour 4','Legend corresponding to colour 6'],[4,6])

in scilab, you just have to click on the window where the graph is in order to place the legends. Dunno how it is in Matlab

Hmm this doesn't seem to work!

My code looks something like:

Code:

``` plot(z1,'*r') plot(z2,'*y') plot(z3,'*b') plot(z4,'*g')```
Where z1...z4 are the complex numbers to be plotted, * indicates that they be plotted as stars, and r/y/b/g denotes the colours.

So according to your example, my legend code should be:

Code:

` legend(['Converges to 1','Converges to -1','Converges to i','Converges to -i'],['r','y','b','g'])`
Yes?

This produces a legend with only 2 entries, as follows:

* Converges to 1 Converges to -1 Converges to i Converges to -i
* rybg

Not quite what I was after! =[
• Feb 28th 2009, 01:08 PM
Mush
Anyone else have any ideas!?

Help!