- trendline: https://in.mathworks.com/matlabcentral/fileexchange/76467
- polyfit: https://in.mathworks.com/help/matlab/ref/polyfit.html
- polyval: https://in.mathworks.com/help/matlab/ref/polyval.html
Y double axis required trendlines
3 views (last 30 days)
Show older comments
x = [1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020];
y1 = [18.1969 15.834 16.5986 3.1579 10.9866 8.2062 8.9229 9.6429 6.4604 14.805 9.1178 0.49 2.9925 8.7875 5.7404 5.365 8.32 13.025 10.9654 3.26 8.975 4.795 0.97 32.3175 11.1025 0.65 9.5425 12.115 9.8975 21.875 16.6925];
y2 = [17.360463 15.30933 12.563672 2.174533 8.407282 5.828774 4.764783 5.524783 3.644908 15.515 7.022406 0.1715 1.168625 4.729125 3.232158 2.149 6.37575 12.0585 7.024658 1.355 5.591 3.088 0.396 25.004125 8.130375 0.2355 5.040375 7.95375 5.477375 18.4155 16.567625];
figure('Color','w')
[hAx,hLine1,hLine2] = plotyy(x,y1,x,y2);
ylabel(hAx(1),'ACE (knot^2)') % left y-axis
ylabel(hAx(2),'PDI (knot^3)') % right y-axis
%grid on
0 Comments
Answers (1)
Shashi Kiran
on 13 Sep 2024
Hi Somnath,
To add trendlines to a double-axis plot in MATLAB, the trendline function from the MathWorks File Exchange as given by this link,
It utilizes MATLAB's polyfit and polyval functions to calculate and draw linear trendlines for both datasets.
Here is how it can be implemented:
x = [1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020];
y1 = [18.1969 15.834 16.5986 3.1579 10.9866 8.2062 8.9229 9.6429 6.4604 14.805 9.1178 0.49 2.9925 8.7875 5.7404 5.365 8.32 13.025 10.9654 3.26 8.975 4.795 0.97 32.3175 11.1025 0.65 9.5425 12.115 9.8975 21.875 16.6925];
y2 = [17.360463 15.30933 12.563672 2.174533 8.407282 5.828774 4.764783 5.524783 3.644908 15.515 7.022406 0.1715 1.168625 4.729125 3.232158 2.149 6.37575 12.0585 7.024658 1.355 5.591 3.088 0.396 25.004125 8.130375 0.2355 5.040375 7.95375 5.477375 18.4155 16.567625];
figure('Color','w')
[hAx,hLine1,hLine2] = plotyy(x,y1,x,y2);
ylabel(hAx(1),'ACE (knot^2)') % left y-axis
ylabel(hAx(2),'PDI (knot^3)') % right y-axis
xlabel('Year')
% Plot trendline for y1
axes(hAx(1)); % Activate the left axis
coefficients_y1 = trendline(x, y1, 'r');
% Plot trendline for y2
axes(hAx(2)); % Activate the right axis
coefficients_y2 = trendline(x, y2, 'b');
legend([hLine1, hLine2], {'ACE', 'PDI'}, 'Location', 'best')
% Function Definition from MathWorks File Exchange
function coefficients=trendline(x,y,color)
if nargin<3
color='k';
end
coefficients = polyfit(x, y, 1);
xFit = linspace(min(x), max(x), 1000);
yFit = polyval(coefficients , xFit);
hold on;
plot(xFit, yFit, [color,'-'], 'LineWidth', 2);
grid on;
end
Refer to the following documentations for more details about the functions:
Hope this solves your query.
2 Comments
Shashi Kiran
on 13 Sep 2024
Hi Somnath,
I believe you have not executed the function definition.
Please run the complete code provided above to see the results.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!