Have legend reflect multiple marker sizes
2 次查看(过去 30 天)
显示 更早的评论
Hi, I have 5 sets of data each symbolized by a different size marker on my plot. I want the legend to reflect these different sizes. However, when I create the legend, the legend does not reflect the different marker sizes if the marker sizes are above 12pts. I have tried using both scatter() and plot() and in either case, the marker size in the legend does not change. My goal is to have 5 different marker sizes reflected in the legend. I'm using matlab R2023a. Any help is appreciated! I've included an example code below as well as a screenshot of the type of output I am looking for.
x = rand(100)*100;
y = rand(100)*100;
figure
scatter(x(1:20),y(1:20),12,'o','filled','MarkerFaceColor','b','MarkerEdgeColor','b')
hold on
scatter(x(20:40),y(20:40),24,'o','filled','MarkerFaceColor','b','MarkerEdgeColor','b')
hold on
scatter(x(40:60),y(40:60),48,'o','filled','MarkerFaceColor','b','MarkerEdgeColor','b')
hold on
scatter(x(60:80),y(60:80),96,'o','filled','MarkerFaceColor','b','MarkerEdgeColor','b')
hold on
scatter(x(80:100),y(80:100),192,'o','filled','MarkerFaceColor','b','MarkerEdgeColor','b')
legend('data1','data2','data3','data4','data5')
0 个评论
采纳的回答
dpb
2023-6-22
移动:dpb
2023-6-22
The legend has been made mostly opaque in recent versions; it used to be you could get your hands on the axes upon which it was built and mung on it; that no longer seems possible, even with Yair's undocumented() function that finds hidden object handles; I didn't find anything useful for this purpose.
It would be difficult to do inside the current legend anyway owing to the space constraints; there's insufficient room for larger markers without also changing the overall legen size and line spacing, etc., ...
However, it also appears that legend simply draws a predefined marker of a given size irrespective of what the actual marker size is -- experimenting with smaller marker sizes also resuts in the same effect...
hF=figure; hAx=axes; hold on
sz=10.^linspace(log10(2),log10(12),5); % smallest visible to hypothesized 12 largest legend renders
for i=1:numel(sz),hSCDum(i)=scatter(nan,nan,sz(i)); end % a dummy set of scatter objects to label
legend("Data "+[1:numel(sz)].')
All markers are same size despite being smaller than 12 in the figure (albeit they don't show, we can prove it is so)
hSCDum.SizeData
Looks like would have to produce the desired effect manually with annotations or the like without the help of legend(). It is very frustrating that legend has been neutered as has been; while some of the newer features are nice, the inability to customize is a major lack; TMW can't think of everything a user might want.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!