필터 지우기
필터 지우기

Scatter and line problem

조회 수: 2 (최근 30일)
Krasimir Terziev
Krasimir Terziev 2020년 3월 12일
댓글: Krasimir Terziev 2020년 3월 12일
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 !

채택된 답변

the cyclist
the cyclist 2020년 3월 12일
편집: the cyclist 2020년 3월 12일
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
Krasimir Terziev
Krasimir Terziev 2020년 3월 12일
Thanks you ! That was all i need ! Wish you all best

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by