How to plot points on a line (2024)

42 views (last 30 days)

Show older comments

jacob Mitch on 10 Oct 2019

  • Link

    Direct link to this question

    https://matlabcentral.mathworks.com/matlabcentral/answers/484658-how-to-plot-points-on-a-line

  • Link

    Direct link to this question

    https://matlabcentral.mathworks.com/matlabcentral/answers/484658-how-to-plot-points-on-a-line

Commented: jacob Mitch on 12 Oct 2019

Accepted Answer: Fabio Freschi

Open in MATLAB Online

Say I have a random sequence of such as new=randn(5,1) which produces y values of

ans =

-0.9118

0.0494

1.0780

0.3082

0.2996

How would I plot these points as a line on a graph ie x=0, y=-0.9118 x=1 y=0.0494 etc or a line connecting the y values. I have tried

plot(new)

I would then like to add points to the graph as blue dots ie onto the graph of new=randn(5,1) add blue dots at the points from code such as

datapoints=

-0.9118

0.3082 or

datapoints=-0.9118 0.3082

I hope this make sense and thanks for the help.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Fabio Freschi on 10 Oct 2019

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/484658-how-to-plot-points-on-a-line#answer_395773

Open in MATLAB Online

I suggest you to check hold on, and the LineSpec of the command plot

% your data

new = rand(5,1);

% open figure and retain current plot

figure, hold on

plot(new);

% new data points

datapoints = [-0.9118 0.3082]

% plot with blue circles

plot(datapoints,'bo')

6 Comments

Show 4 older commentsHide 4 older comments

jacob Mitch on 10 Oct 2019

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/484658-how-to-plot-points-on-a-line#comment_754917

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/484658-how-to-plot-points-on-a-line#comment_754917

Edited: jacob Mitch on 10 Oct 2019

  • test2.fig

Hiya im just having problems in that the datapoints are slightly shifted left of the minima points, I guess the y values match however the x value positions dont match. In that for ans y=-0.9118 x=1 match but y=0.3082 x=4 from ans doesnt match y=0.3082 x=2 from datapoints.

Fabio Freschi on 11 Oct 2019

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/484658-how-to-plot-points-on-a-line#comment_755048

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/484658-how-to-plot-points-on-a-line#comment_755048

you must plot using x and y vectors. Can you share the code you used to calculate datapoints?

jacob Mitch on 11 Oct 2019

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/484658-how-to-plot-points-on-a-line#comment_755180

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/484658-how-to-plot-points-on-a-line#comment_755180

Edited: jacob Mitch on 11 Oct 2019

Open in MATLAB Online

Hi Fabio my code is

function [xvalues, yvalues] = test(data)

c=1;

input=data;

x=length(input);

for z=2:x-x

elseif input(z-1)>input(z) && input(z)<input(z+1)

xvalues(c,1)=z;

yvalues(c,1)=input(z);

c=c+1

end

end

So I would like to graph the "data" points maybe as a lines connecting them then I would like to plot the minima of the graph that I get from "yvalues" ie if i had data as [3;0;3;1;3] and "yvalues" obtains the minima as [0;1], I would like the data drawn as lines connecting 3,0,3,1,3 for the y values and 3,0 as red circles at those points on the line. I would not like to use sign, find or diff functions. Thanks for your help again.

Fabio Freschi on 11 Oct 2019

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/484658-how-to-plot-points-on-a-line#comment_755254

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/484658-how-to-plot-points-on-a-line#comment_755254

Open in MATLAB Online

So it should be something like

% your data

new = rand(5,1);

% open figure and retain current plot

figure, hold on

plot(new);

% new data points

[xdatapoints,ydatapoints] = test(new);

% plot minima

plot(xdatapoints,ydatapoints,'ro'); % no line connecting minima

% plot(xdatapoints,ydatapoints,'ro-'); % line connecting minima

Note that it is not clear why the loop is for z=2:x-x. Why x-x?

jacob Mitch on 12 Oct 2019

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/484658-how-to-plot-points-on-a-line#comment_755410

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/484658-how-to-plot-points-on-a-line#comment_755410

Open in MATLAB Online

Hi there, sorry it was just meant to be

for z=2:x

if z==x

return

jacob Mitch on 12 Oct 2019

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/484658-how-to-plot-points-on-a-line#comment_755569

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/484658-how-to-plot-points-on-a-line#comment_755569

This may be late but you're a genius. Thank you!

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

MATLABGraphics2-D and 3-D PlotsLine Plots

Find more on Line Plots in Help Center and File Exchange

Tags

  • plots
  • plot
  • 3-d plots
  • 2-d plots
  • ezplot
  • linespace

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How to plot points on a line (9)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

How to plot points on a line (2024)
Top Articles
Latest Posts
Article information

Author: Van Hayes

Last Updated:

Views: 6458

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Van Hayes

Birthday: 1994-06-07

Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

Phone: +512425013758

Job: National Farming Director

Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.