MATLAB Answers

0

Can't plot a trendline

Asked by Carter Squires on 3 Dec 2018
Latest activity Answered by Chad Greene
on 4 Mar 2019
I'm trying to plot a least squares trendline to a scatterplot of data on CO2 concentration versus temperature anomaly. The code is below-- the scatterplot is successfully created, but the trendline is not appearing. I'm not getting any error messages from this code. Any help would be appreciated.
%% plot Tanomaly vs. CO2
figure(8)
clf
plot(CO2.value(2:162),IPCC_combined_Tanom(2:162),'r.','markersize',15)
hold on
grid on
xlabel('Global CO2 Concentration (PPMV)')
ylabel('Global Combined T Anomaly °C')
title('Temperaure anomaly vs CO2 Concentration')
set(gca,'fontsize',18)
xlim([280 400])
ylim([-1 1])
for n=1:10:length(CO2.value)
text(CO2.value(n),IPCC_combined_Tanom(n),[' \leftarrow ' num2str(year(n))],'fontsize',14)
end
window=axis;
loc=find(~isnan(CO2.value(2:162)) & ~isnan(IPCC_combined_Tanom(2:162)));
A=[CO2.value(loc) ones(length(loc),1)];
m=A\IPCC_combined_Tanom(loc);
plot(window(1:2),m(1)*window(1:2)+m(2),'k-','linewidth',2)
disp(['Climate sensitivity: ' num2str(round(m(1),5)) ' °C/PPMV CO2'])

  1 Comment

Well, what can we possibly do? You didn't attach any data - not even a screenshot.

Sign in to comment.

2 Answers

Answer by Star Strider
on 3 Dec 2018

I can’t see that you invited the trendline to appear.
See the documentation on lsline (link) to invite it to participate.

  0 Comments

Sign in to comment.


Answer by Chad Greene
on 4 Mar 2019

Check out the polyplot function in the Climate Data Toolbox. If you define the x and y data, plotting a least-squares trend line is just
polyplot(x,y)

  0 Comments

Sign in to comment.