Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
how to add linear regression line and equation in the figure?

Subject: how to add linear regression line and equation in the figure?

From: Kuo-Hsien

Date: 19 Jul, 2012 21:09:15

Message: 1 of 4

Hi all,

I search a bit, but can't find an easy Matlab function for this.

I like to add a linear regression line to my 1:1 scatter plot.
I also like to print the regression equation y = ax +b, r2 and RMSE, p-value in the corner of the scatter plot.
I wonder if you can show me an easy way.


Thanks a lot.
Michael

Subject: how to add linear regression line and equation in the figure?

From: TideMan

Date: 19 Jul, 2012 23:53:01

Message: 2 of 4

do

On Friday, July 20, 2012 9:09:15 AM UTC+12, Kuo-Hsien wrote:
> Hi all,
>
> I search a bit, but can't find an easy Matlab function for this.
>
> I like to add a linear regression line to my 1:1 scatter plot.
> I also like to print the regression equation y = ax +b, r2 and RMSE, p-value in the corner of the scatter plot.
> I wonder if you can show me an easy way.
>
>
> Thanks a lot.
> Michael

doc text
shows you exactly what to do to print the equation. Indeed, there's an example.

To plot the line:
xl=[min(x);max(x)];
yl=a*xl + b;
hold on
plot(xl,yl,'g-',...
   'LineWidth','g')
hold off

Subject: how to add linear regression line and equation in the figure?

From: TideMan

Date: 19 Jul, 2012 23:53:59

Message: 3 of 4

On Friday, July 20, 2012 9:09:15 AM UTC+12, Kuo-Hsien wrote:
> Hi all,
>
> I search a bit, but can't find an easy Matlab function for this.
>
> I like to add a linear regression line to my 1:1 scatter plot.
> I also like to print the regression equation y = ax +b, r2 and RMSE, p-value in the corner of the scatter plot.
> I wonder if you can show me an easy way.
>
>
> Thanks a lot.
> Michael

ooops
should have been
'LineWidth',1.5

Subject: how to add linear regression line and equation in the figure?

From: Barry Williams

Date: 20 Jul, 2012 10:45:19

Message: 4 of 4

TideMan <mulgor@gmail.com> wrote in message <791613e1-8bd5-4444-821e-ef2d28b0e7d9@googlegroups.com>...
> On Friday, July 20, 2012 9:09:15 AM UTC+12, Kuo-Hsien wrote:
> > Hi all,
> >
> > I search a bit, but can't find an easy Matlab function for this.
> >
> > I like to add a linear regression line to my 1:1 scatter plot.
> > I also like to print the regression equation y = ax +b, r2 and RMSE, p-value in the corner of the scatter plot.
> > I wonder if you can show me an easy way.
> >
> >
> > Thanks a lot.
> > Michael
>
> ooops
> should have been
> 'LineWidth',1.5

Because your question appears to indicate that you are a new Matlab user, I would add only a couple of small suggestions to what Tideman has said. Note carefully the last example in the documentation of the text function:

h = text(x,y,'string') % Note the assignment of the text to the handle h.

Having inserted many text strings into figures with minor typos in the x,y coordinates, only to have their location outside of the x and y limits of the axes, I learned to assign a handle. That way if you decide you don't like the font size or position or whatever, it's easy to change, for example:

set(h, 'Position', [newX newY])

With more complicated text like the one you want to put into the figure, you may want to consider taking the time to assign the text to a character array using some functions like strvcat and sprintf. That way it's really easy to update if you change your data. It may seem a bit roundabout, but the extra time often pays off. You can learn, for example, that if you are concatenating strings, normally the leading and trailing spaces between strings are lost, but you can a char(32) to add them back in.

Barry

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us