Issues with legend on a plot?

6 次查看(过去 30 天)
So I'm having issues correctly displaying a legend for my figure.
I have an image I'm displaying with ground truths marked in red and text labeled 1-11. Then, I'm wanting to have a legend that displays 1 through 11 vertically with the type of ground truth to the right of it. Unfortunately, I'm getting far, far from this. I would post a screenshot, but I'm technically not allowed to which makes troubleshooting a bit cumbersome.
h1=figure(1);imagesc(abs(image));set(gca,'YDir','normal');
hold on
plot(gnd_TruthY_test,gnd_TruthX_test,'r*');
text(gnd_TruthY_test,gnd_TruthX_test,num2str(c'),'Color', [0.859375000000000,0.859375000000000,0.859375000000000],'VerticalAlignment','bottom', ...
'HorizontalAlignment','right')
legend(gt{1,5}(c))
where gt{1,5} contains strings for the ground truth types and c are the indexes that corrspond to the groundtruths in the displayed image.
When I run the above code, my image displays properly but my legend only displays the first ground truth. If I repeatedly click run then the legend eventually builds up. I'm not sure I understand the legend() use in MATLAB >.<
  6 个评论
Darren
Darren 2014-4-23
legend(num2str([1:11]','Line %d'))
Worked properly, for the most part, but I still cannot adapt this version to mine.
legend(num2str([gt{1,5}(c)'],'Line %s'))
yields,
Undefined function 'real' for input arguments of type 'cell'.
I'm guessing this is now an issue of the fact that I'm trying to pull info from my cell? This is the part of MATLAB that is the most annoying to me...
Image Analyst
Image Analyst 2014-4-23
Don't call your image variable "image" because that is the name of a built-in function.

请先登录,再进行评论。

采纳的回答

Kelly Kearney
Kelly Kearney 2014-4-23
If I understand your setup correctly, I think the problem is that you want your legend to reference the individual points of a single line object. Is this the sort of thing you're hoping for?
gnd_TruthY_test = rand(1,5);
gnd_TruthX_test = rand(1,5);
c = 1:5;
label = {'one','two','three','four','five'};
figure;
hl = plot([gnd_TruthY_test; nan(1,5)], [gnd_TruthX_test; nan(1,5)], 'r*');
ht = text(gnd_TruthY_test, gnd_TruthX_test, num2str(c'));
legend(hl, label);
If so, the trick is to plot each point to its own line object, which can be accomplished by adding the NaN-padding to each column (causing plot to treat each column as a different line; by default it ignores this convention if both x and y input are vectors).
If that's the case, note that this doesn't really link your legend labels to the text labels next to each point... but perhaps that info is already in your labels?
  2 个评论
Darren
Darren 2014-4-23
编辑:Darren 2014-4-23
I implemented this via
h1=figure(1);imagesc(abs(IM));set(gca,'YDir','normal');
hold on
hl=plot([gnd_TruthY_test'; nan(1,length(c))'], [gnd_TruthX_test';nan(1,length(c))'],'r*');
ht=text(gnd_TruthY_test',gnd_TruthX_test',num2str(c'),'Color', [0.859375000000000,0.859375000000000,0.859375000000000],'VerticalAlignment','bottom', ...
'HorizontalAlignment','right')
legend(hl, gt{1,5}(c)');
But still receiving the same problem where only the first ground truth is displaying in the legend :/
Darren
Darren 2014-4-23
Nevermind, got it, had to remove the transpose on some of my input values! Thanks!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by