Matlab plot is drawing an individual line to each data point (2024)

40 次查看(过去 30 天)

显示 更早的评论

Alexandr 2024-6-24,1:46

  • 链接

    此问题的直接链接

    https://ww2.mathworks.cn/matlabcentral/answers/2131276-matlab-plot-is-drawing-an-individual-line-to-each-data-point

  • 链接

    此问题的直接链接

    https://ww2.mathworks.cn/matlabcentral/answers/2131276-matlab-plot-is-drawing-an-individual-line-to-each-data-point

编辑: Aquatris 2024-6-24,9:07

采纳的回答: Aquatris

So im writing a matlab script that creates a 3 by 100 matrix x and a 1 by 100 matrix n. When I try to plot it, matlab draws an individual line segment from the origin to each data point, rather than just connecting the data points. From what I found online, it's due to the plot function being inside a for loop, but after I moved it outside the loop it kept doing the same thing. Can somebody help.Matlab plot is drawing an individual line to each data point (2)

Here's what the plot looks like

0 个评论

显示 -2更早的评论隐藏 -2更早的评论

请先登录,再进行评论。

请先登录,再回答此问题。

采纳的回答

Aquatris 2024-6-24,8:34

  • 链接

    此回答的直接链接

    https://ww2.mathworks.cn/matlabcentral/answers/2131276-matlab-plot-is-drawing-an-individual-line-to-each-data-point#answer_1476181

编辑:Aquatris 2024-6-24,9:07

You have a lot of 0s in your nc and x vector, hence you go back and forth to 0 position. You can see this if you use a data point in one of the lines and move along the data using keyboard arrow keys.

The reason is because your if statement is not always true, so you do not fill your nc and x vector in every run, so it fills it as default 0 for nc and you already prefilled x with 0s. You can either use an idx variable to find non zero entries as the code below, or change the way you store the values. I have no idea what you are trying to do so I am not sure if this is actually what you want.

%% EXAMPLE 1

n = 2; % n can only be a prime number

x = zeros(3,100); % initialize array with rows as cases for p

p = [80/100,85/100,88/100];

nc = x(1,:);

while (n < 100)

if isprime(n)

for i = 1:3

for j = n:-1:1

x(i,n) = (x(i,n) + j)^p(i);

nc(n) = n;

end

end

n = n+1;

else

n = n+1;

end

end

idx = nc~=0; % find non zero nc entries

plot(nc(idx),x(:,idx),'--o')

grid on

legend('Location','best')

xlabel('n')

ylabel('x_n')

title({'Lab1_A'})

Matlab plot is drawing an individual line to each data point (4)

0 个评论

显示 -2更早的评论隐藏 -2更早的评论

请先登录,再进行评论。

更多回答(1 个)

Animesh 2024-6-24,2:39

  • 链接

    此回答的直接链接

    https://ww2.mathworks.cn/matlabcentral/answers/2131276-matlab-plot-is-drawing-an-individual-line-to-each-data-point#answer_1476021

Hi Alexandr,

It sounds like you might be using the plot function incorrectly in MATLAB. To plot a continuous line connecting your data points, you need to provide the x and y coordinates of your data points as vectors. Here’s a basic example of how to do it correctly:

% Sample data points

x = [1, 2, 3, 4, 5];

y = [2, 4, 6, 8, 10];

% Plotting the data points with a continuous line

figure;

plot(x, y, '-o'); % '-o' creates a line with circle markers at data points

xlabel('X-axis label');

ylabel('Y-axis label');

title('Plot of Data Points');

grid on;

In this example:

  • x and y are vectors containing the coordinates of the data points.
  • The '-o' argument specifies that MATLAB should draw a line connecting the data points and place a circle marker at each data point.

If you want to plot multiple sets of data on the same graph, you can do so by including additional pairs of x and y vectors in the plot function call:

% Additional data points

x2 = [1, 2, 3, 4, 5];

y2 = [3, 6, 9, 12, 15];

% Plotting multiple sets of data points

figure;

plot(x, y, '-o', x2, y2, '-x'); % '-x' creates a line with x markers at data points

xlabel('X-axis label');

ylabel('Y-axis label');

title('Plot of Multiple Data Sets');

legend('Data Set 1', 'Data Set 2');

grid on;

Make sure that your x and y vectors are of the same length, as MATLAB requires this to plot the points correctly.

For more information on plot function, please consider going through the documentation: https://in.mathworks.com/help/matlab/ref/plot.html

1 个评论

显示 -1更早的评论隐藏 -1更早的评论

Alexandr 2024-6-24,3:21

此评论的直接链接

https://ww2.mathworks.cn/matlabcentral/answers/2131276-matlab-plot-is-drawing-an-individual-line-to-each-data-point#comment_3193966

  • 链接

    此评论的直接链接

    https://ww2.mathworks.cn/matlabcentral/answers/2131276-matlab-plot-is-drawing-an-individual-line-to-each-data-point#comment_3193966

Thank you for your answer Animesh, however my code is correct based on what you replied here. I am attaching my code with this reply, so you could take a closer look. Thanks again!

n = 2; % n can only be a prime number

x = zeros(3,100); % initialize array with rows as cases for p

p = [80/100,85/100,88/100];

nc = x(1,:);

while (n < 100)

if isprime(n)

for i = 1:3

for j = n:-1:1

x(i,n) = (x(i,n) + j)^p(i);

nc(n) = n;

end

end

n = n+1;

else

n = n+1;

end

end

plot(nc,x,'--o')

grid on

legend('Location','best')

xlabel('n')

ylabel('x_n')

title({'Lab1_A'})

Matlab plot is drawing an individual line to each data point (7)

请先登录,再进行评论。

请先登录,再回答此问题。

另请参阅

类别

MATLABGraphicsFormatting and AnnotationLabels and AnnotationsAxis Labels

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

标签

  • plotting
  • data points

产品

  • MATLAB

版本

R2023b

Community Treasure Hunt

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

Start Hunting!

发生错误

由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。


Translated by Matlab plot is drawing an individual line to each data point (8)

Matlab plot is drawing an individual line to each data point (9)

选择网站

选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:

您也可以从以下列表中选择网站:

美洲

欧洲

亚太

联系您当地的办事处

Matlab plot is drawing an individual line to each data point (2024)
Top Articles
Latest Posts
Article information

Author: Catherine Tremblay

Last Updated:

Views: 6460

Rating: 4.7 / 5 (47 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Catherine Tremblay

Birthday: 1999-09-23

Address: Suite 461 73643 Sherril Loaf, Dickinsonland, AZ 47941-2379

Phone: +2678139151039

Job: International Administration Supervisor

Hobby: Dowsing, Snowboarding, Rowing, Beekeeping, Calligraphy, Shooting, Air sports

Introduction: My name is Catherine Tremblay, I am a precious, perfect, tasty, enthusiastic, inexpensive, vast, kind person who loves writing and wants to share my knowledge and understanding with you.