Reputation: 33944
I have some code that basically does this:
for i = 1:length(ReliabilityStruct)
if (FailureFlags(i) == 0)
plot(X(i), Y(i), '.b');
elseif (FailureFlags(i) == 1)
plot(X(i), Y(i), 'or');
elseif (FailureFlags(i) == 2)
plot(X(i), Y(i), 'og');
elseif (FailureFlags(i) == 3)
plot(X(i), Y(i), 'ok');
else
fprintf('\nUnknown Flag, check Data\n')
return
end
end
drawnow;
legend('Recovery', '1', '2', '3');
So my aim is to make a graph that has different symbols for different flags. See below:
As you can see, the Legend doesn't exactly fit the data. How can you change each of the legend entries to fix this up? Alternatively, is there a better way to approach this?
Upvotes: 0
Views: 186
Reputation: 1760
Remember that you can refer to the usage for any function using help
command. For your case, the help legend
will give you usage examples like following.
legend(H,string1,string2,string3, ...) puts a legend on the plot
containing the handles in the vector H using the specified strings as
labels for the corresponding handles.
Thus, you can get the plot handler by assign the plot to a variable, such as p1=plot(X(i), Y(i), '.b');
. And then draw the legend by invoke the command with handlers as first parameter, like legend([p1], 'something')
.
Upvotes: 1
Reputation: 2599
I think you can use something like this (added bonus is that you avoid loops!):
ind = FailureFlags==0;
plot(X(ind), Y(ind), '.b');
ind = FailureFlags==1;
plot(X(ind), Y(ind), 'or');
ind = FailureFlags==2;
plot(X(ind), Y(ind), 'og');
ind = FailureFlags==3;
plot(X(ind), Y(ind), 'ok');
legend('Recovery', '1', '2', '3');
Upvotes: 3
Reputation: 390
Give this a try. In your loop make an assignment for each plot as follows:
p1=plot(X(i), Y(i), '.b');
elseif (FailureFlags(i) == 1)
p2=plot(X(i), Y(i), 'or');
elseif (FailureFlags(i) == 2)
p3=plot(X(i), Y(i), 'og');
elseif (FailureFlags(i) == 3)
p4=plot(X(i), Y(i), 'ok');
Then you can use legend for the specific things:
legend([p1 p2],'stuff','morestuff')
Upvotes: 1