How to draw a line between two points on a graph?

Hello everyone,
Is there any way to draw a line on a plot in the center as shown in the figure. And I need the length of the line to be same as for the first plot and to the others. Please kindly help me. Thanks in advance.
My code:
Z = readtable('Atq100_2.xlsx') ;
data = table2array(Z) ;
N = size(data,2);
Nsp = N/2;
ttlc = {'x=10mm', 'x=250mm', 'x=500mm', 'x=1000mm', 'x=1500mm'};
xofst = [10 250 500 1000 1500];
figure
ylim([-1 1]*200)
xlim([0 1600])
hold on
for k = 1:Nsp
col = [2 1]+2*(k-1);
famp = 5; % Amplification Factor
datacol1 = data(:,col(1))*famp+xofst(k);
hp(k) = plot(datacol1, data(:,col(2)), 'LineWidth',2);
% plot(data(:,col(1))+xofst(k), data(:,col(2)), 'LineWidth',2)
% minx = min(data(:,col(1))+xofst(k));
% maxx = max(data(:,col(1))+xofst(k));
minx = min(datacol1);
maxx = max(datacol1);
plot([1;1]*[minx maxx], ([1;1]*ylim).', ':k', 'LineWidth',1) % Plot Vertical 'scale' Lines
% plot([1;1]*[minx], ([1;1]*ylim).', ':k', 'LineWidth',1) % Plot Vertical 'scale' Lines
end
hold off
hl = legend([hp], ttlc, 'Location','northeastoutside');
WIth regards,

 Accepted Answer

I recognise that code!
I added this plot call to the others in the loop:
plot([minx maxx], [0 0], '-k', 'LineWidth',1) % Plot Horizontal 'zero' Lines
It plots a horizontal line at 0 for each section defined by the ‘scale’ lines.
Z = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/748504/Atq100_2.xlsx') ;
data = table2array(Z) ;
N = size(data,2);
Nsp = N/2;
ttlc = {'x=10mm', 'x=250mm', 'x=500mm', 'x=1000mm', 'x=1500mm'};
xofst = [10 250 500 1000 1500];
figure
ylim([-1 1]*200)
xlim([0 1600])
hold on
for k = 1:Nsp
col = [2 1]+2*(k-1);
famp = 5; % Amplification Factor
datacol1 = data(:,col(1))*famp+xofst(k);
hp(k) = plot(datacol1, data(:,col(2)), 'LineWidth',2);
% plot(data(:,col(1))+xofst(k), data(:,col(2)), 'LineWidth',2)
% minx = min(data(:,col(1))+xofst(k));
% maxx = max(data(:,col(1))+xofst(k));
minx = min(datacol1);
maxx = max(datacol1);
plot([1;1]*[minx maxx], ([1;1]*ylim).', ':k', 'LineWidth',1) % Plot Vertical 'scale' Lines
% plot([1;1]*[minx], ([1;1]*ylim).', ':k', 'LineWidth',1) % Plot Vertical 'scale' Lines
plot([minx maxx], [0 0], '-k', 'LineWidth',1) % Plot Horizontal 'zero' Lines
end
hold off
hl = legend([hp], ttlc, 'Location','northeastoutside');
Experimenbt to get different results.
.

10 Comments

Once again hai and thank you for your quick response. But what I need is i need all the horizantal lines to be of equal length not just the max and min lengths. For example the length for all the lines has to be same as length of the line for the first plot.
Sure!
That simply requires a tweak in the code —
Z = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/748504/Atq100_2.xlsx') ;
data = table2array(Z) ;
N = size(data,2);
Nsp = N/2;
ttlc = {'x=10mm', 'x=250mm', 'x=500mm', 'x=1000mm', 'x=1500mm'};
xofst = [10 250 500 1000 1500];
figure
ylim([-1 1]*200)
xlim([0 1720])
hold on
for k = 1:Nsp
col = [2 1]+2*(k-1);
famp = 5; % Amplification Factor
datacol1 = data(:,col(1))*famp+xofst(k);
hp(k) = plot(datacol1, data(:,col(2)), 'LineWidth',2);
% plot(data(:,col(1))+xofst(k), data(:,col(2)), 'LineWidth',2)
% minx = min(data(:,col(1))+xofst(k));
% maxx = max(data(:,col(1))+xofst(k));
minx(k) = min(datacol1);
maxx(k) = max(datacol1);
plot([1;1]*[minx(k) maxx(k)], ([1;1]*ylim).', ':k', 'LineWidth',1) % Plot Vertical 'scale' Lines
% plot([1;1]*[minx], ([1;1]*ylim).', ':k', 'LineWidth',1) % Plot Vertical 'scale' Lines
plot([minx(1) maxx(1)]+minx(k)*(k>1), [0 0], '-k', 'LineWidth',1) % Plot Horizontal 'zero' Lines
Line_Coordinates = [minx(1) maxx(1)]+minx(k)*(k>1) % Check Line Length (Delete)
LineLength = diff(Line_Coordinates) % Check Line Length (Delete)
end
Line_Coordinates = 1×2
10.0446 210.3789
LineLength = 200.3344
Line_Coordinates = 1×2
260.1239 460.4582
LineLength = 200.3344
Line_Coordinates = 1×2
510.0542 710.3886
LineLength = 200.3344
Line_Coordinates = 1×2
1.0e+03 * 1.0105 1.2109
LineLength = 200.3344
Line_Coordinates = 1×2
1.0e+03 * 1.5105 1.7109
LineLength = 200.3344
hold off
hl = legend([hp], ttlc, 'Location','northeastoutside');
The horizontal line lengths are all the same, even though they might not appear that way in the plot with the original xlim values, so I changed xlim to allow the complete ‘zero line’ to show for all the curves.. I added ‘Line_Coordinates’ and ‘LineLength’ to demonstrate their equality. (Delete them later.)
.
Thank you once again @Star Strider. One small change i request you is that the line should not start from zero. It should start from point 10. And similarly all the other should start at the dotted offset line. Can you please change correspondingly. Have a nice weekend and thank you for your help.
As always, my pleasure!
Thank you! You, too!
.
Thank you once again @Star Strider. One small change i request you is that the line should not start from zero. It should start from point 10. And similarly all the other should start at the dotted offset line. Can you please change correspondingly. Have a nice weekend and thank you for your help.
As always, my pleasure!
Please check the ‘LineCoordinates’ results. They show where the ‘zero lines’ begin and end. It can be difficult to see in the plot, however for the first line:
Line_Coordinates = 1×2
10.0446 210.3789
So it works correctly.
.
I am sorry. For the first plot it is fine but for the other plots it starts at 10 points after. For example for the second plot it should start at 250 but starts at 260. Simillarly same for the others. I have attached the data here.
That is easily corrected —
Z = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/748504/Atq100_2.xlsx') ;
data = table2array(Z) ;
N = size(data,2);
Nsp = N/2;
ttlc = {'x=10mm', 'x=250mm', 'x=500mm', 'x=1000mm', 'x=1500mm'};
xofst = [10 250 500 1000 1500];
figure
ylim([-1 1]*200)
xlim([0 1720])
hold on
for k = 1:Nsp
col = [2 1]+2*(k-1);
famp = 5; % Amplification Factor
datacol1 = data(:,col(1))*famp+xofst(k);
hp(k) = plot(datacol1, data(:,col(2)), 'LineWidth',2);
% plot(data(:,col(1))+xofst(k), data(:,col(2)), 'LineWidth',2)
% minx = min(data(:,col(1))+xofst(k));
% maxx = max(data(:,col(1))+xofst(k));
minx(k) = min(datacol1);
maxx(k) = max(datacol1);
plot([1;1]*[minx(k) maxx(k)], ([1;1]*ylim).', ':k', 'LineWidth',1) % Plot Vertical 'scale' Lines
% plot([1;1]*[minx], ([1;1]*ylim).', ':k', 'LineWidth',1) % Plot Vertical 'scale' Lines
plot([minx(1) maxx(1)]-minx(1)+minx(k), [0 0], '-k', 'LineWidth',1) % Plot Horizontal 'zero' Lines
Line_Coordinates = [minx(1) maxx(1)]-minx(1)+minx(k) % Check Line Length (Delete)
LineLength = diff(Line_Coordinates) % Check Line Length (Delete)
end
Line_Coordinates = 1×2
10.0446 210.3789
LineLength = 200.3344
Line_Coordinates = 1×2
250.0793 450.4137
LineLength = 200.3344
Line_Coordinates = 1×2
500.0097 700.3440
LineLength = 200.3344
Line_Coordinates = 1×2
1.0e+03 * 1.0005 1.2008
LineLength = 200.3344
Line_Coordinates = 1×2
1.0e+03 * 1.5005 1.7008
LineLength = 200.3344
hold off
hl = legend([hp], ttlc, 'Location','northeastoutside');
.
Thank you so much for the help @Star Strider. Have a nice weekend.
As always, my pleasure!
You too!
.

Sign in to comment.

More Answers (1)

You can use either plot(), which is very general, or line() which is more specialized for lines.

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!