How do I add lines on a scatter graph?

39 views (last 30 days)
I want to make a scatter graph using data from an imported table (Mortalitymatlab), where there's multiple datasets plotted against the x axis. This is the code I have so far and I realise its pretty clunky, but it does at least get all the variables plotted onto the same axis. But where I've been getting stuck is that for each dataset I want to add a line between the points, and I can't seem to figure out how to do that. I've tried adding the '-x' command which has been suggested on other forums, but it doesn't seem to be doing anything. Any help would be appreciated, as it seems that the moment tables are used everything stops working as well! Thanks!
scatter(Mortalitymatlab,"Day","Gt6")
hold on
scatter(Mortalitymatlab,"Day","Gt13")
hold on
scatter(Mortalitymatlab,"Day","Gs30")
hold on
scatter(Mortalitymatlab,"Day","Gs32.5")
hold on
scatter(Mortalitymatlab,"Day","Gs37.5")
hold on
scatter(Mortalitymatlab,"Day","GpH7.7")
hold on
scatter(Mortalitymatlab,"Day","GpH7.85")
hold on
scatter(Mortalitymatlab,"Day","GpH8.1")
hold on
scatter(Mortalitymatlab,"Day","GpH8.35")
hold on
scatter(Mortalitymatlab,"Day","Gc100")
hold on
scatter(Mortalitymatlab,"Day","Gc250")

Accepted Answer

TITTU GEORGE
TITTU GEORGE on 20 Apr 2023
% this is to generate sample data
x1=[1 , 2, 3, 4 ,5];
y1=[10, 20 ,30, 40, 50];
% this is to generate sample data
x2=x1-2;
y2=y1-15;
% this is to generate sample data
x3=x2-3;
y3=y2-20;
% plot the sample data one after the other
plot(x1,y1)
hold on
plot(x2,y2)
hold on
plot(x3,y3)
% make new varibale for connecting the points
% this can be automated but i have written manually here
a1=[x1(1) x2(1) x3(1)];
b1=[y1(1) y2(1) y3(1)];
a2=[x1(2) x2(2) x3(2)];
b2=[y1(2) y2(2) y3(2)];
a3=[x1(3) x2(3) x3(3)];
b3=[y1(3) y2(3) y3(3)];
a4=[x1(4) x2(4) x3(4)];
b4=[y1(4) y2(4) y3(4)];
a5=[x1(5) x2(5) x3(5)];
b5=[y1(5) y2(5) y3(5)];
plot(a1,b1)
hold on
plot(a2,b2)
hold on
plot(a3,b3)
hold on
plot(a4,b4)
hold on
plot(a5,b5)
hold on

More Answers (1)

Cris LaPierre
Cris LaPierre on 20 Apr 2023
A scatter plot is just markers, no line.
You need to use the plot function instead.
Also, you only need to turn 'hold on' once. Be sure to pair it with a 'hold off' once all lines have been added to the axes.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!