How can I color a (circle) area in a scatterplot?

조회 수: 5 (최근 30일)
Anna Boehm
Anna Boehm 2018년 10월 1일
답변: Anna Boehm 2018년 10월 2일
In my task, I should draw n samples from a two dimensional random variable U=(U1,U2), where U1 and U2 are independent and both uniformly distributed on [-1,1].
Afterwards, I should plot the sample and mark all the samples which fulfill (U1^2 + U2^2)<1. (I already know that the outcome will be a circle with radius r=1.)
To plot the sample I used scatter(U1,U2). How can I color the samples which fulfill the condition (U1^2 + U2^2)<1?

답변 (4개)

Adam
Adam 2018년 10월 1일
doc scatter
shows a 4-argument syntax which allows you to include a vector of colours of equal length to the x and y. So you can use whatever logic you wish to make some of those colours different and the rest a standard colour. In your case just a simple logical mask can be used to insert your chosen colour into an array of your chosen default colour.
You can pass in [] as the 3rd argument if you don't care about explicitly setting the marker size.

jonas
jonas 2018년 10월 1일
편집: jonas 2018년 10월 1일
You could do something like,
id=(U1.^2+U2.^2)<1;
plot(U1(id),U2(id),'.r',U1(~id),U2(~id),'.k')

JohnGalt
JohnGalt 2018년 10월 1일
I don't have access to the scatterplot function but the technique you're looking for is called logical indexing... where you set up an index for each co-ordinate which is either true or false. if it's true plot it one colour, if it's false plot it another colour.
samples = rand(10000,2)*2 -1; % create data
x = samples(:,1);
y = samples(:,2);
mask = (x.^2 + y.^2) <1; % create logical mask (true/false)
figure; hold on; % new figure window. enable overlaying of plots
plot(x,y,'b.'); % plot a blue dot for all points
plot(x(mask),y(mask),'r.') % overwrite all points where mask==true with a red dot

Anna Boehm
Anna Boehm 2018년 10월 2일
Thanks to all, it works perfectly!

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by