MATLAB Answers

How to add Multiple reference lines in single MatLab plot

43 views (last 30 days)
Robert  Flores
Robert Flores on 18 Apr 2018
Answered: KSSV on 18 Apr 2018
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

  0 Comments

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 18 Apr 2018

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')

  0 Comments

Sign in to comment.

More Answers (1)

KSSV
KSSV on 18 Apr 2018
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.

  0 Comments

Sign in to comment.


Translated by