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

조회 수: 39 (최근 30일)
Vishnuvardhan Naidu Tanga
Vishnuvardhan Naidu Tanga 2021년 9월 24일
댓글: Star Strider 2021년 9월 24일
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,

채택된 답변

Star Strider
Star Strider 2021년 9월 24일
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
Vishnuvardhan Naidu Tanga
Vishnuvardhan Naidu Tanga 2021년 9월 24일
Thank you so much for the help @Star Strider. Have a nice weekend.
Star Strider
Star Strider 2021년 9월 24일
As always, my pleasure!
You too!
.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2021년 9월 24일
You can use either plot(), which is very general, or line() which is more specialized for lines.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by