Legend not showing markers
16 次查看(过去 30 天)
显示 更早的评论
I have a figure with a plot and an inset. I want a shared legend for the main plot and the inset axes. The following code creates the effect I want, except that the legend entries that correspond to the data in the inset figure only show errorbars in the plot, not the markers.
figure; hold on
data1 = [0. .5 1. 2. 4. 16.];
data2 = [0. .2 1.5 2.2 3.5 15.];
error = [0.5 .6 .7 .8 .9 1.];
aa = gca;
f1 = errorbar(data1,error,'ok','MarkerFaceColor','k')
f2 = fplot(@(x) x^2, [0. 6.], 'k-')
bb = axes('Position',[.2 .5 .3 .3]); hold on
f3 = errorbar(data1,error,'or','MarkerFaceColor','k')
f4 = errorbar(data2,error,'^b','MarkerFaceColor','k')
box on
legend(aa,[f1 f2 f3 f4],{'data1A','fplot','data1B','data2'},'Location','southeast')
Any ideas on what's causing this, or how to fix it? Alternatively another approach that gives the same desired result is fine.
0 个评论
回答(2 个)
Kelly Kearney
2018-2-19
I've found that legends can sometimes benefit from taking a deep breath. Not sure exactly why, but it seems that since the introduction of HG2 (R2016b), legends sometimes start to do their analysis of the labeled objects before those objects are fully rendered, and therefore they can miss some details. I've never taken the time to really dig into this bug, because the solution is pretty straightforward: add a pause. I added the following to your code:
pause(0.1);
legend(aa, [f1 f2 f3 f4],{'data1A','fplot','data1B','data2'},'Location','southeast')
Without the pause, I get the bar-and-whiskers legend markers only. With the small pause, the markers suddenly appear correctly.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!