How to add Multiple reference lines in single MatLab plot

조회 수: 3 (최근 30일)
Robert  Flores
Robert Flores 2018년 4월 18일
답변: KSSV 2018년 4월 18일
Hello,
I have the plot I want, however I would like to add both vertical and horizontal lines to reference certain points within my graph. In the attachments is an image of what I am talking about. I would like to reference my average Impact energy, Avg, please see code below. As well as referencing the point when the temperature is equal to 70 C. If possible, I would also like to have the corresponding Impact Energy value (when Temp = 70 C) displayed. If you can help, great, thanks.
Beginning of code is as follows:
Temp = [100,75,50,25,0,-25,-50,-65,-75,-85,-100,-125,-150,-175];
Impact_Energy = [89.3,88.6,87.6,85.4,82.9,78.9,73.1,66,59.3,47.9,34.3,29.3,27.1,25];
% Part B
Avg = (89.3+25)/2; % This average is measured in Joules
plot(Temp,Impact_Energy,'r*')
title('Impact Energy V. Temperature')
xlabel('Temperature, measured in C')
ylabel('Impact Energy, measured in J')
set(gca, 'XLim', [-180, 200], 'XTick', -180:10:200,'XTickLabel', -180:10:200,'Fontsize',10);
hold on
scatter(Temp,Impact_Energy,'X')
line(Temp,Impact_Energy)
hold off

채택된 답변

Star Strider
Star Strider 2018년 4월 18일

Add this line just after your ‘Impact_Energy’ vector assignment:

T70 = interp1(Temp, Impact_Energy, [-70; 70]);

and add these two lines just before your hold off call:

plot(-70*[1 1], [0 T70(1)], '--b', [min(xlim) -70], [1 1]*T70(1),  '--b')
plot( 70*[1 1], [0 T70(2)], '--b', [min(xlim)  70], [1 1]*T70(2),  '--b')

추가 답변 (1개)

KSSV
KSSV 2018년 4월 18일
Temp = [100,75,50,25,0,-25,-50,-65,-75,-85,-100,-125,-150,-175];
Impact_Energy = [89.3,88.6,87.6,85.4,82.9,78.9,73.1,66,59.3,47.9,34.3,29.3,27.1,25];
% Part B
Avg = (89.3+25)/2; % This average is measured in Joules
plot(Temp,Impact_Energy,'r*')
title('Impact Energy V. Temperature')
xlabel('Temperature, measured in C')
ylabel('Impact Energy, measured in J')
set(gca, 'XLim', [-180, 200], 'XTick', -180:10:200,'XTickLabel', -180:10:200,'Fontsize',10);
hold on
scatter(Temp,Impact_Energy,'X')
line(Temp,Impact_Energy)
% hold off
% DRaw line at x = 70 
x = 70 ;
y = interp1(Temp,Impact_Energy,x) ;
plot([x,x],[y,0],'--r')
plot([x,-180],[y,y],'--r')

YOu may follow the same for other line.

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by