Scatter and line problem

1 vue (au cours des 30 derniers jours)
Krasimir Terziev
Krasimir Terziev le 12 Mar 2020
Hello
i have a little problem with this two functions : scatter and line
Here is a simple code that i write:
x=[16.3,7.3,3.3,13.3,2.3,6.3,12.3,16.3,5.3,12.3,7.3,19.3];
y=[6.7,17.7,7.7,18.7,6.7,13.7,4.7,17.7,17.7,9.7,9.7,16.7];
figure(1)
scatter(x,y,'o')
grid
These are four things i need to find out :
1. i cant find out how to connect a random points of this data ?
EXAMPLE : i need a line between 1st data (x=16.3, y=6.7) and (randomly hmmm) 8th date (x=12.3, y=9.7), then i want to connect the 8th and 6th data point and etc....
2. How to make a bigger circle around the "o" simbol with a radius that i want ?
3. How can i measure the line between the 1st data and the 8th data?
EXAMPLE: i can do this manualy whit Pythagorean theorem but i need to find a function to do it and a way to write above the line the length.
4. how can i write a random text above all data point ?
EXAMPLE: above point 1 ( 1st data x=16.3, y=6.7) i want to write a Village One (for example) !
THANKS YOU ALL ! WISH YOU BEST !

Réponse acceptée

the cyclist
the cyclist le 12 Mar 2020
Modifié(e) : the cyclist le 12 Mar 2020
This code illustrates everything you asked about.
% Fix the random number seed, for reproducibility
rng default
% Data
x=[16.3,7.3,3.3,13.3,2.3,6.3,12.3,16.3,5.3,12.3,7.3,19.3];
y=[6.7,17.7,7.7,18.7,6.7,13.7,4.7,17.7,17.7,9.7,9.7,16.7];
% Choose random point to connect to point 1.
rn = randi(8);
% Distance to that point
d = pdist([x(1) y(1); ...
x(rn) y(rn)]);
figure(1)
% Scatter plot (with larger marker)
scatter(x,y,'o','SizeData',500);
% Connect point 1 to the randomly selected one
line([x(1) x(rn)],[y(1) y(rn)]);
% Write the distance
text(15,5,sprintf('d = %7.3f',d))
% Add the grid
grid
The only thing I did "manually" was hard-code the values where the text appears, so that it is near the drawn line. Instead, one would want to calculate the position of that text via the locations of the two points, and putting it near the halfway point.
Note also that I used the sprintf function to convert a numeric value to text, before writing it. If you just have some known text, you can just do
text(x,y,'Known text')
  1 commentaire
Krasimir Terziev
Krasimir Terziev le 12 Mar 2020
Thanks you ! That was all i need ! Wish you all best

Connectez-vous pour commenter.

Plus de réponses (0)

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!

Translated by