how to add numbers and labels to scatterplot bubbles?

조회 수: 7 (최근 30일)
Minka Califf
Minka Califf 2018년 6월 7일
댓글: Walter Roberson 2018년 6월 8일

I am trying to add numbers and labels to this scatterplot

clc
clear all
close all
%reading the file
[numbers text]=xlsread('homelesspopinamerica.xlsx');
%manipulating withthe data and storing it at our convenience
statenames=text(2:51,1:2);
pop=numbers(1:50,:);
states=statenames(:,2);
state=statenames(:,2);
homepop=pop(:,2);
homeperc=pop(:,4);
totpop=pop(:,3);
states=categorical(states);
%create scatter figure
figure
scatter(homeperc,totpop,homepop/20,states,'filled')
title('HOMELESSNESS IN AMERICA 2017', 'fontsize', 36);
set(gcf,'color','w');
xlabel('Percentage of Homeless Indviduals')
ylabel('Total Population (millions)')
axis([0 0.6 0 4.5e7]);
grid on
%make graph large
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);

i want to label them by total homeless population (column D in excel sheet) and by state abbreviation (column B) and have the label inside the bubbles that it fits into. how can i do this? here is an example of what i am looking for:

채택된 답변

Pawel Jastrzebski
Pawel Jastrzebski 2018년 6월 7일
There's a function called:
that is used for labeling data points in plots.
  댓글 수: 2
Minka Califf
Minka Califf 2018년 6월 8일
Hi Pawel, thanks for the info. Is there a way to use the text function to add labels only to the larger bubbles in the plot? Thank you!
Walter Roberson
Walter Roberson 2018년 6월 8일
Only call it for those locations.
[~, popsortidx] = sort(homepop, 'descend');
best10idx = popsortidx(1:10);
locx = homeperc(best10idx);
locy = totpop(best10idx);
beststates = states(best10idx);
saved_text = text; %save the variable that was badly named text
clear text %make way to use the function
text(locx, locy, beststates); %put up the text at the given locations
text = saved_text; %restore the badly-named variable text

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by