# how can I plot multiple y values for single x value?

150 views (last 30 days)

Show older comments

##### 3 Comments

KALYAN ACHARJYA
on 2 Dec 2020

Edited: KALYAN ACHARJYA
on 2 Dec 2020

Chaman Srivastava
on 2 Dec 2020

@kalyan acharjya Yes indeed it will be a straight line. But my goal is to obtain a regression curve with confidence interval, something like what is shown in the curve attached.

Any leads for this? Thanks

### Answers (1)

Setsuna Yuuki.
on 2 Dec 2020

you can try creating a vector of ones and multiplying this by your x values, for example:

y = rand(1,15);

xvalue = 3;

x = xvalue*ones(1,length(y));

scatter(x,y,'LineWidth',3);

##### 3 Comments

Chaman Srivastava
on 2 Dec 2020

Hey Bastian,

Thank you the piece of code. I have tried something like this but plotting a regression using polyfit isn't working. Any leads on that?

Setsuna Yuuki.
on 2 Dec 2020

I tried it this way

clearvars;

x = linspace(0,30);

i = 0; j = 1;

comp = 1:3:length(x);

vectorx = zeros(1,length(x));

%Vector with three same x values

for n = 1:length(x)

vectorx(n) = i;

if(n == comp(j))

i = i+1;

j = j+1;

end

end

x = vectorx;

%Random data

y = 0.2+rand(1,length(vectorx));

p = polyfit(x,y,1);

f = polyval(p,x);

figure

plot(x,y,'o',x,f,'-','LineWidth',3)

legend('data','linear fit')

grid on;

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!