How to make the legend selective?
32 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Joseph Cheng
2015-4-24
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.
更多回答(1 个)
Sebastian Castro
2015-4-23
编辑:Sebastian Castro
2015-4-23
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!