Connecting the point selectively

  • Thread starter Thread starter hokhani
  • Start date Start date
  • Tags Tags
    Point
Click For Summary
SUMMARY

The discussion focuses on selectively connecting points in a MATLAB plot to maintain the separation of two discrete regions. The user initially plots points using the sine function and seeks to avoid connecting the rightmost point of the left region to the leftmost point of the right region. The solution involves splitting the data into two halves and plotting them separately, using the syntax 'plot(X(1:N), Y(1:N), '-*', X(N+1:end), Y(N+1:end), '-*')' to achieve the desired visual effect. Additionally, specifying a color for both plots can ensure consistency in appearance.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of basic plotting functions in MATLAB
  • Knowledge of sine functions and their graphical representation
  • Ability to manipulate arrays and indexing in MATLAB
NEXT STEPS
  • Explore MATLAB's plotting options and customization techniques
  • Learn about MATLAB's array manipulation functions for advanced data handling
  • Investigate the use of color specifications in MATLAB plots
  • Study the implications of data segmentation in graphical representations
USEFUL FOR

Data analysts, MATLAB users, and anyone interested in advanced plotting techniques to visually separate data regions in graphical outputs.

hokhani
Messages
601
Reaction score
22
Running the Matlab code below two discrete regions appear; one in left and the other in right.
Code:
x=20:5:800;
xpr=x*pi/180;
y=sin(xpr);
n=find(y<.2);
plot(x(n),y(n),'*')
I would like to connect the points of each region together and not to connect the rightmost point of the left region to the leftmost point of the right region so that the two regions remain separated. What should I do? I appreciate any help.
 
Physics news on Phys.org
Code:
x=20:5:800;
xpr=x*pi/180;
y=sin(xpr);
n=find(y<.2);

X = xpr(n);
Y = y(n);
N = length(X)/2;
plot(X(1:N), Y(1:N), '-*', X(N+1:end), Y(N+1:end), '-*')

Since you scaled x to xpr, I plotted xpr since it is used to calculate y. But essentially all I did was break X and Y in half, then plot the first and second halves separately in the call to plot. The '-*' ensures that plot draws the point and connects with a line. The result is below. Note that since the plots are separate, they get different colors. You can get around that by specifying the same color for both, for example by saying '-*b' to make both plots blue.

sin_plots.png
 
  • Like
Likes   Reactions: hokhani

Similar threads

  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
3K