How do I create a legend that explaines scatter plot by marker size

74 views (last 30 days)
I am creating a bubble chart (scatter plot with different marker sizes). This works fine but I would need a legend to show the range of the parameter that defines the size of the marker.
The code for the plot looks like this:
scatter(r1,r2,a);
a is an array, that determines the size of the markers.
So I am looking for a legend like the one I attached.
Appreciate any help, thanks a lot!
  1 Comment
Ali
Ali on 29 Oct 2017
if true
--------------------------------------------------- code start
This is an example for your case Nelly
Input is "Input_Data", two dimension matrix
Marker_Counter=1;
figure6=figure;
Markers = {'+','o','*','x','v','d','^','s','>','<'};
for i=1:10:size(Input_Data,1)
TPR=Input_Data(i:i+9,7);
FPR=Input_Data(i:i+9,8);
plot(FPR,TPR,strcat('-',Markers{Marker_Counter}));
Marker_Counter=Marker_Counter+1;
hold on
end
plot([0.5 1],[0.5 1],'--');
legend('Minpts = 100','Minpts = 200','Minpts = 300','Minpts = 400','Minpts = 500','Minpts = 600','Minpts = 700','Minpts = 800','Minpts = 900','Minpts = 1000','','Location','SouthEast');
xlabel('FPR or (1-Specificity)','FontSize',12,'FontWeight','bold'); ylabel('TPR or Spensitivity)','FontSize',12,'FontWeight','bold');
title('ROC Space');
close(gcf);
-------------------------------------------- code end
end
--------------------------------------- picture link preview
<</matlabcentral/answers/uploaded_files/92608/untitled.bmp>>

Sign in to comment.

Accepted Answer

Joseph Cheng
Joseph Cheng on 7 Jul 2015
Edited: Joseph Cheng on 7 Jul 2015
since you're using scatter one way around this is by doing this:
r1 = randi(10,1,10);
r2 = randi(10,1,10);
a = 30*randi(5,1,10);
bubsizes = unique(a)';
legentry=cell(size(bubsizes));
figure,hold on
for ind = 1:numel(bubsizes)
bubleg(ind) = plot(0,0,'ro','markersize',sqrt(bubsizes(ind)),'MarkerFaceColor','red');
set(bubleg(ind),'visible','off')
legentry{ind} = num2str(bubsizes(ind));
end
h = scatter(r1,r2,a,'r','MarkerFaceColor','red')
legend(legentry)
I couldn't find a quick solution to the scatter plot marker area to plot's "markersize" but the sqrt() of the scatterplot marker area visually looks to be about the same.
  2 Comments
Andreas Kaineder
Andreas Kaineder on 7 Jul 2015
great!
I adapted your code a bit to only show 5 sizes in the legend by:
bubsizes = [min(a) quantile(a,[0.25, 0.5, 0.75]) max(a)];
thanks a lot for your help!

Sign in to comment.

More Answers (2)

Mike Garrity
Mike Garrity on 7 Jul 2015
You need a separate object for each legend entry. Something like this:
rng default
x = randn(1,100);
y = randn(1,100);
a = randi(5,[1 100]);
plot(x(a==1),y(a==1),'o','MarkerSize',5 , ...
'MarkerFaceColor','red','MarkerEdgeColor','black')
hold on
plot(x(a==2),y(a==2),'o','MarkerSize',10, ...
'MarkerFaceColor','red','MarkerEdgeColor','black')
plot(x(a==3),y(a==3),'o','MarkerSize',15, ...
'MarkerFaceColor','red','MarkerEdgeColor','black')
plot(x(a==4),y(a==4),'o','MarkerSize',20, ...
'MarkerFaceColor','red','MarkerEdgeColor','black')
plot(x(a==5),y(a==5),'o','MarkerSize',25, ...
'MarkerFaceColor','red','MarkerEdgeColor','black')
legend show
  3 Comments
Mike Garrity
Mike Garrity on 7 Jul 2015
In that case I would create invisible scatter objects to stand in for each of the "size buckets" you want. Set those Visible='off' and leave the "real" scatter out of the legend.
vadim girardeau
vadim girardeau on 7 Feb 2024
Hello Mike,
In matlab 2023b it does not work anymore, the size is like autoupdate for the legend, do you know how to fix it ? Thanks a lot :

Sign in to comment.


dpb
dpb on 6 Jul 2015
"Use the handles, Luke..." :)
If you save the handles to the scatter points, you can write a legend for those specific points using the handles and the desired text (in your example that will be the num2str output for the given value in the format desired).
The default legend marker is the same size for each; if you save the object handles (second optional output) from legend, there are two sets of them, a text and a patch object. The first N are the text; the second set of N are the patches. Set the 'markersize' argument for those to match the size desired.
It looks like you may have to futz with the spacing within the legend box as well; it seems to not reflect a size based on the marker size.
Lotta' work, but doable...there's a lot of flexibility available in HG but there's a lot of stuff that just seems excessively needing of user futzing, too, unfortunately.
  1 Comment
dpb
dpb on 7 Jul 2015
"t looks like you may have to futz with the spacing within the legend box as well; it seems to not reflect a size based on the marker size."
This would seem worth an official enhancement request filing to TMW at the www.mathworks.com site; a legend should reflect the markers and be "smart" enough to not put them on top of each other as Mike's plot illustrates (and is the end result with doing it directly on the original legend handles as I suggested, too; I just didn't attach the sample figure but the end result is identical).

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!