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')

采纳的回答

dpb
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
ans = 2
ans = 3.1302
ans = 4.8990
ans = 7.6673
ans = 12.0000
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.
  1 个评论
Eileen Lukens
Eileen Lukens 2023-6-23
Thanks, that is good to know. I appreciate the help! I'll try and find a different way of representing my data.

请先登录,再进行评论。

更多回答(0 个)

类别

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

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by