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

89 views (last 30 days)

Show older comments

Andreas Kaineder on 6 Jul 2015

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/228667-how-do-i-create-a-legend-that-explaines-scatter-plot-by-marker-size

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/228667-how-do-i-create-a-legend-that-explaines-scatter-plot-by-marker-size

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

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

  • Link

    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

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/228667-how-do-i-create-a-legend-that-explaines-scatter-plot-by-marker-size#answer_185261

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/228667-how-do-i-create-a-legend-that-explaines-scatter-plot-by-marker-size#answer_185261

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

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

  • Link

    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

  • Link

    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

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/228667-how-do-i-create-a-legend-that-explaines-scatter-plot-by-marker-size#answer_185248

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

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

3 Comments

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

  • Link

    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

  • Link

    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

  • Link

    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 :

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

Sign in to comment.

dpb on 6 Jul 2015

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/228667-how-do-i-create-a-legend-that-explaines-scatter-plot-by-marker-size#answer_185163

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/228667-how-do-i-create-a-legend-that-explaines-scatter-plot-by-marker-size#answer_185163

"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

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

  • Link

    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.


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

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

How do I create a legend that explaines scatter plot by marker size (2024)
Top Articles
Hot Dog Sensation: Big Jill's Viral Twitter Explosion at 25 - Unveiling the Jill Cannady Phenomenon! | video
Kristyn &quot;Ashley&quot; Smith on LinkedIn: If you&#39;ve worked with me, you know that empathy is a major pillar of my… | 10 comments
Honda Odyssey Questions - P0303 3 cyclinder misfire
Monkey Werx Sitrep 2022
Myud Dbq
Walmart Front Door Wreaths
gameplay:shiny_pokemon_and_luck [PokéRogue Wiki]
Two men arrested following racially motivated attack on Sanford teen's car
Myportal Udm
Leccion 4 Lesson Test
Timothy Warren Cobb Obituary
803 Castroville Road, San Antonio, TX 78237
Chris Evert Twitter
8042872020
Itawamba Ixl
Carly Carrigan Family Feud Instagram - Carly Carrigan Home Facebook : The best gifs for carly family feud.
Craigslist Scranton Pennsylvania
2022 NFL Predictions
Envy Nail Bar Memphis
Kawasaki Ninja® 500 | Motorcycle | Approachable Power
Telegram Voyeur
Omniplex Cinema Dublin - Rathmines | Cinema Listings
Arsenal news LIVE: Latest updates from the Emirates
Urbfsdreamgirl
Cambria County Most Wanted 2022
Nbc Breaking News Nyc
Does Dollar General Have Humidifiers
Couches To Curios Photos
Unblocked Games 66E
Sentara Norfolk General Visiting Hours
Aerospace Engineering | Graduate Degrees and Requirements
Look Who Got Busted New Braunfels
Sam's Club Stafford Gas Price
R Mcoc
Www.playgd.mobi Wallet
Cashtapp Atm Near Me
Cvs Pharmacy Tb Test
Dollar Tree Aktie (DLTR) • US2567461080
The Next Phase for the V-22 Osprey: Build Global Support Like C-17
Sloansmoans Many
Apphomie.com Download
Sutter Immunization Clinic Mountain View
U Arizona Phonebook
911 Active Calls Caddo
The Hardest Quests in Old School RuneScape (Ranked) – FandomSpot
Project Zomboid Sleeping Event
Craigslist Farm And Garden Lexington
C-Reactive Protein (CRP) Test Understand the Test & Your Results
Craigslist Pgh Furniture
Bòlèt New York Soir
South Florida residents must earn more than $100,000 to avoid being 'rent burdened'
Omaha World-Herald from Omaha, Nebraska
Latest Posts
Article information

Author: Kieth Sipes

Last Updated:

Views: 5478

Rating: 4.7 / 5 (47 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Kieth Sipes

Birthday: 2001-04-14

Address: Suite 492 62479 Champlin Loop, South Catrice, MS 57271

Phone: +9663362133320

Job: District Sales Analyst

Hobby: Digital arts, Dance, Ghost hunting, Worldbuilding, Kayaking, Table tennis, 3D printing

Introduction: My name is Kieth Sipes, I am a zany, rich, courageous, powerful, faithful, jolly, excited person who loves writing and wants to share my knowledge and understanding with you.