89 views (last 30 days)
Show older comments
Andreas Kaineder on 6 Jul 2015
Commented: vadim girardeau on 7 Feb 2024
Accepted Answer: Joseph Cheng
- legend bubble chart.PNG
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 Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Ali on 29 Oct 2017
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/228667-how-do-i-create-a-legend-that-explaines-scatter-plot-by-marker-size#comment_498506
Open in MATLAB Online
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.
Sign in to answer this question.
Accepted Answer
Joseph Cheng on 7 Jul 2015
Edited: Joseph Cheng on 7 Jul 2015
Open in MATLAB Online
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 Show NoneHide None
Show NoneHide None
Andreas Kaineder on 7 Jul 2015
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/228667-how-do-i-create-a-legend-that-explaines-scatter-plot-by-marker-size#comment_296783
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!
Joseph Cheng on 7 Jul 2015
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/228667-how-do-i-create-a-legend-that-explaines-scatter-plot-by-marker-size#comment_296798
i would hope you did, i just did that to create dummy data.
Sign in to comment.
More Answers (2)
Mike Garrity on 7 Jul 2015
Open in MATLAB Online
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 Show 1 older commentHide 1 older comment
Show 1 older commentHide 1 older comment
Andreas Kaineder on 7 Jul 2015
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/228667-how-do-i-create-a-legend-that-explaines-scatter-plot-by-marker-size#comment_296728
thanks a lot for your input! unfortunately I can't separate the plots like you did, because I have hundreds of different values in the array a. the legend only needs to show the size of the minimum, q1, q2, q3 and the maximum of the data.
seems to be tricky...
Mike Garrity on 7 Jul 2015
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/228667-how-do-i-create-a-legend-that-explaines-scatter-plot-by-marker-size#comment_296805
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 on 7 Feb 2024
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/228667-how-do-i-create-a-legend-that-explaines-scatter-plot-by-marker-size#comment_3056966
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 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 Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
dpb on 7 Jul 2015
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/228667-how-do-i-create-a-legend-that-explaines-scatter-plot-by-marker-size#comment_296724
"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.
Sign in to answer this question.
See Also
Categories
MATLABGraphicsFormatting and AnnotationLabels and AnnotationsLegend
Find more on Legend in Help Center and File Exchange
Tags
- legend
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office