How to make the legend selective?
37 views (last 30 days)
Show older comments
I have a bunch of algorithms I am testing for statistical significance. I wrote a script to generate and save plots which I can publish in my thesis using Latex.
The problem is shown in the figures below-
In the first figure since the first three quantities satisfy significance criteria the legend is all nice and good, but in the second figure since the third one is not significant the legend uses the marker style being used for non significant data points.
How can I correct it such that the color and marker styles remain consistent in the legend. It is important that this is a generalized solution since I am always adding data and the levels of significance may change.
0 Comments
Accepted Answer
Joseph Cheng
on 24 Apr 2015
So... the problem i see is that the characteristics of the markers are linked directly to the style of the plotted points. So far i cannot find a way to directly edit the legend handles to change the marker color. However through knowing how legend generates the items you can "trick" it. From my guess your implementation of the legend is to just go legend('series 1','series 2','blah blah blah... ',...,'location', 'southoutside');
well when that happens you're grabbing the first 3 plotted objects. so knowing that why don't we plot specific points outside the plotted area? such as the code below does. I colored the legend markers magenta just to show that i have full control over what happens in the legend. Also if you un-comment the threshold point at 5 you'll see that the first marker would be then the ---- and everything has been shifted over.
clc, clear all; close all;
x = 1:10
y = randi(10,1,10);
hfig = figure;
hold on
% % plot([0 10],[5 5],'k--');
markers ='oxdoxdoxdoxdoxdoxdoxd';
umarkers = unique(markers,'stable');
for ind = 1:length(umarkers)
plot(-10,-10,['m' umarkers(ind)])
end
for ind = 1:10
if y(ind) <5
plot(x(ind),y(ind),[markers(ind) 'r']);
else
plot(x(ind),y(ind),[markers(ind) 'b']);
end
end
ylim([0 10])
xlim([0 10]);
legend('circle','X-mark','diamond');
plot([0 10],[5 5],'k--');
If i think of a cleaner way to do this i'll repost it.
More Answers (1)
Sebastian Castro
on 23 Apr 2015
Edited: Sebastian Castro
on 23 Apr 2015
You can specify the graphics objects you want to add to your legend in the "entries" argument of the legend function.
Here is a simple example where I create 3 plots but only legend the 1st and 3rd. Try it out on your system!
% Create a figure window and hold on
figure
hold on;
% Create the plots
X1 = plot(1:10);
X2 = plot(10:-1:0,'r-');
X3 = plot(1:0.5:5,'g-');
% Add a legend to only X1 and X3
legend([X1 X3],'Entry1','Entry3')
Hope this helps.
- Sebastian
See Also
Categories
Find more on Legend in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!