How can I color a (circle) area in a scatterplot?
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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?
0 commentaires
Réponses (4)
Adam
le 1 Oct 2018
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.
0 commentaires
JohnGalt
le 1 Oct 2018
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
0 commentaires
Voir également
Catégories
En savoir plus sur Scatter Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!