MATLAB: How to make the legend selective

figurelegendMATLAB

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.

Best Answer

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.