How to display a linear regression line along with the scatter plot (2024)

169 views (last 30 days)

Show older comments

HAT on 15 Feb 2023

  • Link

    Direct link to this question

    https://jmaab.mathworks.com/matlabcentral/answers/1913105-how-to-display-a-linear-regression-line-along-with-the-scatter-plot

  • Link

    Direct link to this question

    https://jmaab.mathworks.com/matlabcentral/answers/1913105-how-to-display-a-linear-regression-line-along-with-the-scatter-plot

Edited: HAT on 15 Feb 2023

Accepted Answer: Dyuman Joshi

Open in MATLAB Online

I want to diplay a linear regression line (y=b+mx) along with the scattered plot, but I got stuck.

I need only to display the scatter points and the regression line (y=b+mx) without confidence bounds.

Moreover, the scatter points (dots) should remain the same, instead of x.

I wonder if I could get help.

Thank you in advance.

%Two data x and y

x=[0.06309839457075230.05693107619633370.09028491744333860.05865575645363810.03341446972672780.07126318804719290.07599369681925050.1237383391828880.02970326371090490.07958922825838190.02822615962940580.05371832499871050.1505722574324090.09779636990330210.08852692083261750.08663963852494920.06692985048651100.03443124139251160.03486679608968030.04936561884930810.02706505382994660.03809683374340810.08884456429543780.08959452988850490.05413598539551820.07443219954648170.09135045866358170.06565543006217120.1172591952930690.07132773533507690.03373605523729400.08067380458939770.01879387692142770.1122039289348480.03448906166116090.1120701004623410.03543402102419640.04857534580997900.1007188832239410.08754117406964600.03561330318588130.05828179863562050.04827906342289890.1589973229920620.07299922900970770.04712447430219200.04460848096593500.05113160238979590.05064962538439760.1493807528938380.06890454808273980.05185330619200890.02097885521116530.08533212175630260.09334527596721510.06323211220103040.09890456245366580.1186469453957640.07463944914258170.05633649734147520.06994053863106580.1763135304489040.08050859674232520.07119517069343920.09858737778335320.03611237537507500.1076203229906260.1559530113092760.07038443592225950.06983003496596530.01336932916192660.05632515380760250.04205943357734830.02099298072493190.08364806117569990.09187601533425810.1090710239880300.1120111484569820.09329468418169260.05272733055724430.06039650226899180.09978319703701410.08655659485505630.04518649190173010.05349285558988820.07536118495801760.09750807053429450.06413639677131380.04925283781379290.01158959638316130.03956753546458570.05435628125057300.03029120283161280.09529966575865370.07355693582133970.02044152913469910.02914874079428660.06779173912117210.04392156589517290.03338165490416100.04564035519523540.04865533993022020.04705384329017010.08119578579448990.05150618534835910.09146183193038290.05455849546641580.04965850333063480.03946723483194680.05512729490073500.05206441965539220.08344327647565730.07349598361265990.04644065495715000.05977282805885480.04102139626488290.06904048539612150.04968099765649870.03616692050940100.07303227046671410.1077305973500340.02484053550058770.05119423825764930.01895039377524950.04086802887939210.1207850372900730.05945152312210300.03282459665423760.05484581486869530.05299490388655310.07910291044705280.05542372528664840.09949083401251330.05423992437662950.05344481075533410.06735932733335770.09397911929003670.08233009457623240.04425666002408200.06106171212748780.08988843386442920.05897966828047150.09762099489736520.08684448015714540.06520493594170580.05370214996787300.03016545195017580.05125361543515390.03459874376280450.07792823362551900.08251540443375720.08597997771422920.04422071683689850.009872432635278130.09500217131842070.06134378225517520.1424415268164360.1008024302739120.04142167014987310.1090196756087900.02969130823362430.03477428601591560.1358097634201030.09845060292349620.06740374185930820.02294076662960690.01899968522704670.07435713351149890.08471928141228800.1150836906128880.1236961789667090.09564178039590830.05197928740587790.03930760755779760.06773653774106940.05914268578390450.08479462646718230.06067566482028720.07240677342994400.04308352013769810.08081108532927790.04984233753227010.01947088969741060.07511311867277410.03487263898201650.07406122886704350.05943355162868840.05943481980176550.06824063084136350.05318946558616870.07437903025942930.02195581761074920.02721236622580390.1085384395378590.07470598962823690.09468588223297400.05692767023968260.01493835251691370.05980501840408440.05454003093042100.03430027318035690.08116874052151880.03882679548564320.1086318001002040.06745774263631810.05685443418378040.01081676197308900.04230296423731160.03777518600772560.03935527223300520.07101539871478490.1218323511381840.07693823765095650.09435856405268140.02571388418044950.1061639048143680.03643312446913570.06044182707484090.07387447949806140.1376934410016740.0459022534232133];

y=[0.02508616939125110.005851904584649800.06803699267915280.01235495625868420.02825784262639280.02898788785128770.01832478152402380.02640467693512470.02983945888291290.01843754015416120.01282343939982600.02520503879260580.02955893747770740.01725815127142850.01983229143171100.05106121221090210.02653921019727720.01665424591143240.06140140187435780.01215600045568790.007187486930920650.01530447362533970.02931769695732870.01758691749216160.01146932375750250.05065900015594090.006104658398886970.01264871097736350.07025559228962820.04185702990609150.002360605321422300.04555168314593130.01108001401434060.02200562988598200.01056527068648780.03731364966526350.03879075833997520.04054890561585150.05483303838306990.03131050462741290.01633614920311270.02242567993869370.03409183578322050.08565875920792770.02372819659425350.01850484026394380.01605536279792750.02487645045941000.006710411559444680.008602252703395450.02842315208788380.01488588214414660.02408405346431920.03248764875004750.01863923520079120.02903601080408250.03920293193574570.005147746807632230.04037344913441470.005535149481286240.01594729127363010.03910147474113820.01245042942226370.03562269453655350.01816283488626090.01786817791879540.008527726534151020.01381551792770360.01287975068836400.01757290802578200.03326496229591980.01321449196161320.01421899605877210.008674722208722890.01385856439633980.08080963008683750.02758595595651180.04306588379252900.04375295799421200.04321430877500070.02166440208035450.02824670313100190.02641980427494840.01865362058347010.02049446175892890.01614207505089470.04196421047287290.02662221277170960.02382606841623980.01673233865258800.02111662374901620.01384894514545490.01717114934857410.01981547348052070.02882560164927440.02177443980623730.02246401374338890.01192035431071330.01852851059329220.02768439043805910.02604602934448230.01167391734912310.03644104845792420.02617992587653700.01432957287985270.06180402521198640.03607622978139380.02992191014572120.01221044192388760.02245331538654880.04663429690068530.04392416479062210.02391866597289510.04295360630565060.02332041803987100.02579523408652140.04422638103152390.004652671023481240.008314119756583380.02368930524997210.03573911327841130.02144722261569450.03281760166651200.01679887662486930.02028378330330650.01191480521059290.02288268674627090.03437817081711760.05408342362560890.02532217118081910.04683238963675220.07426868195099080.01499216032334250.02993636828616560.01404228004761850.009234665744434230.03393424656031260.02915567745814040.02648005663618670.02540964164753420.02723399019516170.03061757840231540.02658228133770050.02856888114346440.02642818442911060.008469949739236200.01263903471571100.01178236936381920.01064385688237980.02620670375311810.03218054387415190.01487501703890710.01520491010840410.01079179178222010.03602501098042130.02270160643298700.02135934751347250.02442212649283070.04116646680124720.04019678224628380.003102150603142170.03327187839083830.05295847107765880.01354454957371360.008609164878156000.02793237398141910.01605243043049930.01714574035157510.01357765479894870.01063349070076620.02834725147901110.007763856519468300.02398863302822520.01992173257932690.01683000204905450.01981692178225890.01016890244086790.04217152018978650.01508275027525570.02264026156704000.03840037540975820.02439507995838780.02217027907751740.005676357881481870.01849356805114840.009574579838463420.03944340471769480.01378015384557110.006691083665336190.01756979856105050.02571160119947900.02060429770543870.01354852374646910.007413083865351770.01790471405585570.02589819160376720.01570924821449790.03013732000504650.01378315390614410.01714130501252070.01505720446094570.009383182067219050.02889209568875290.05689369870924980.05632160234276680.02386738374113450.007973603096864640.02043170222339950.03269979164399860.07261277693026760.01514350396627880.01232171648086810.01832598746924330.02148468765592650.01402008538721360.03766925500800250.01576112723160320.02585600420594970.01469396499930020.02531022027067200.0188959246844452];

disp('Scatter graph ');

Scatter graph

scatter(x',y','.')

xlabel('x')

ylabel('y')

How to display a linear regression line along with the scatter plot (2)

mdl = fitlm(x,y)

mdl =

Linear regression model: y ~ 1 + x1Estimated Coefficients: Estimate SE tStat pValue ________ _________ ______ __________ (Intercept) 0.017481 0.0022911 7.6298 7.1359e-13 x1 0.1106 0.031273 3.5367 0.00049439Number of observations: 221, Error degrees of freedom: 219Root Mean Squared Error: 0.0144R-squared: 0.054, Adjusted R-Squared: 0.0497F-statistic vs. constant model: 12.5, p-value = 0.000494

plot(mdl)

How to display a linear regression line along with the scatter plot (3)

%Here I need to display only the scatter points and the regression line (y=1+x) without confidence bounds.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Dyuman Joshi on 15 Feb 2023

  • Link

    Direct link to this answer

    https://jmaab.mathworks.com/matlabcentral/answers/1913105-how-to-display-a-linear-regression-line-along-with-the-scatter-plot#answer_1172240

  • Link

    Direct link to this answer

    https://jmaab.mathworks.com/matlabcentral/answers/1913105-how-to-display-a-linear-regression-line-along-with-the-scatter-plot#answer_1172240

Open in MATLAB Online

%Two data x and y

x = [0.06309839457075230.05693107619633370.09028491744333860.05865575645363810.03341446972672780.07126318804719290.07599369681925050.1237383391828880.02970326371090490.07958922825838190.02822615962940580.05371832499871050.1505722574324090.09779636990330210.08852692083261750.08663963852494920.06692985048651100.03443124139251160.03486679608968030.04936561884930810.02706505382994660.03809683374340810.08884456429543780.08959452988850490.05413598539551820.07443219954648170.09135045866358170.06565543006217120.1172591952930690.07132773533507690.03373605523729400.08067380458939770.01879387692142770.1122039289348480.03448906166116090.1120701004623410.03543402102419640.04857534580997900.1007188832239410.08754117406964600.03561330318588130.05828179863562050.04827906342289890.1589973229920620.07299922900970770.04712447430219200.04460848096593500.05113160238979590.05064962538439760.1493807528938380.06890454808273980.05185330619200890.02097885521116530.08533212175630260.09334527596721510.06323211220103040.09890456245366580.1186469453957640.07463944914258170.05633649734147520.06994053863106580.1763135304489040.08050859674232520.07119517069343920.09858737778335320.03611237537507500.1076203229906260.1559530113092760.07038443592225950.06983003496596530.01336932916192660.05632515380760250.04205943357734830.02099298072493190.08364806117569990.09187601533425810.1090710239880300.1120111484569820.09329468418169260.05272733055724430.06039650226899180.09978319703701410.08655659485505630.04518649190173010.05349285558988820.07536118495801760.09750807053429450.06413639677131380.04925283781379290.01158959638316130.03956753546458570.05435628125057300.03029120283161280.09529966575865370.07355693582133970.02044152913469910.02914874079428660.06779173912117210.04392156589517290.03338165490416100.04564035519523540.04865533993022020.04705384329017010.08119578579448990.05150618534835910.09146183193038290.05455849546641580.04965850333063480.03946723483194680.05512729490073500.05206441965539220.08344327647565730.07349598361265990.04644065495715000.05977282805885480.04102139626488290.06904048539612150.04968099765649870.03616692050940100.07303227046671410.1077305973500340.02484053550058770.05119423825764930.01895039377524950.04086802887939210.1207850372900730.05945152312210300.03282459665423760.05484581486869530.05299490388655310.07910291044705280.05542372528664840.09949083401251330.05423992437662950.05344481075533410.06735932733335770.09397911929003670.08233009457623240.04425666002408200.06106171212748780.08988843386442920.05897966828047150.09762099489736520.08684448015714540.06520493594170580.05370214996787300.03016545195017580.05125361543515390.03459874376280450.07792823362551900.08251540443375720.08597997771422920.04422071683689850.009872432635278130.09500217131842070.06134378225517520.1424415268164360.1008024302739120.04142167014987310.1090196756087900.02969130823362430.03477428601591560.1358097634201030.09845060292349620.06740374185930820.02294076662960690.01899968522704670.07435713351149890.08471928141228800.1150836906128880.1236961789667090.09564178039590830.05197928740587790.03930760755779760.06773653774106940.05914268578390450.08479462646718230.06067566482028720.07240677342994400.04308352013769810.08081108532927790.04984233753227010.01947088969741060.07511311867277410.03487263898201650.07406122886704350.05943355162868840.05943481980176550.06824063084136350.05318946558616870.07437903025942930.02195581761074920.02721236622580390.1085384395378590.07470598962823690.09468588223297400.05692767023968260.01493835251691370.05980501840408440.05454003093042100.03430027318035690.08116874052151880.03882679548564320.1086318001002040.06745774263631810.05685443418378040.01081676197308900.04230296423731160.03777518600772560.03935527223300520.07101539871478490.1218323511381840.07693823765095650.09435856405268140.02571388418044950.1061639048143680.03643312446913570.06044182707484090.07387447949806140.1376934410016740.0459022534232133];

y = [0.02508616939125110.005851904584649800.06803699267915280.01235495625868420.02825784262639280.02898788785128770.01832478152402380.02640467693512470.02983945888291290.01843754015416120.01282343939982600.02520503879260580.02955893747770740.01725815127142850.01983229143171100.05106121221090210.02653921019727720.01665424591143240.06140140187435780.01215600045568790.007187486930920650.01530447362533970.02931769695732870.01758691749216160.01146932375750250.05065900015594090.006104658398886970.01264871097736350.07025559228962820.04185702990609150.002360605321422300.04555168314593130.01108001401434060.02200562988598200.01056527068648780.03731364966526350.03879075833997520.04054890561585150.05483303838306990.03131050462741290.01633614920311270.02242567993869370.03409183578322050.08565875920792770.02372819659425350.01850484026394380.01605536279792750.02487645045941000.006710411559444680.008602252703395450.02842315208788380.01488588214414660.02408405346431920.03248764875004750.01863923520079120.02903601080408250.03920293193574570.005147746807632230.04037344913441470.005535149481286240.01594729127363010.03910147474113820.01245042942226370.03562269453655350.01816283488626090.01786817791879540.008527726534151020.01381551792770360.01287975068836400.01757290802578200.03326496229591980.01321449196161320.01421899605877210.008674722208722890.01385856439633980.08080963008683750.02758595595651180.04306588379252900.04375295799421200.04321430877500070.02166440208035450.02824670313100190.02641980427494840.01865362058347010.02049446175892890.01614207505089470.04196421047287290.02662221277170960.02382606841623980.01673233865258800.02111662374901620.01384894514545490.01717114934857410.01981547348052070.02882560164927440.02177443980623730.02246401374338890.01192035431071330.01852851059329220.02768439043805910.02604602934448230.01167391734912310.03644104845792420.02617992587653700.01432957287985270.06180402521198640.03607622978139380.02992191014572120.01221044192388760.02245331538654880.04663429690068530.04392416479062210.02391866597289510.04295360630565060.02332041803987100.02579523408652140.04422638103152390.004652671023481240.008314119756583380.02368930524997210.03573911327841130.02144722261569450.03281760166651200.01679887662486930.02028378330330650.01191480521059290.02288268674627090.03437817081711760.05408342362560890.02532217118081910.04683238963675220.07426868195099080.01499216032334250.02993636828616560.01404228004761850.009234665744434230.03393424656031260.02915567745814040.02648005663618670.02540964164753420.02723399019516170.03061757840231540.02658228133770050.02856888114346440.02642818442911060.008469949739236200.01263903471571100.01178236936381920.01064385688237980.02620670375311810.03218054387415190.01487501703890710.01520491010840410.01079179178222010.03602501098042130.02270160643298700.02135934751347250.02442212649283070.04116646680124720.04019678224628380.003102150603142170.03327187839083830.05295847107765880.01354454957371360.008609164878156000.02793237398141910.01605243043049930.01714574035157510.01357765479894870.01063349070076620.02834725147901110.007763856519468300.02398863302822520.01992173257932690.01683000204905450.01981692178225890.01016890244086790.04217152018978650.01508275027525570.02264026156704000.03840037540975820.02439507995838780.02217027907751740.005676357881481870.01849356805114840.009574579838463420.03944340471769480.01378015384557110.006691083665336190.01756979856105050.02571160119947900.02060429770543870.01354852374646910.007413083865351770.01790471405585570.02589819160376720.01570924821449790.03013732000504650.01378315390614410.01714130501252070.01505720446094570.009383182067219050.02889209568875290.05689369870924980.05632160234276680.02386738374113450.007973603096864640.02043170222339950.03269979164399860.07261277693026760.01514350396627880.01232171648086810.01832598746924330.02148468765592650.01402008538721360.03766925500800250.01576112723160320.02585600420594970.01469396499930020.02531022027067200.0188959246844452];

scatter(x,y,'.')

xlabel('x')

ylabel('y')

How to display a linear regression line along with the scatter plot (5)

mdl = fitlm(x,y);

% use handle for plotting

h = plot(mdl)

h =

4×1 Line array: Line (Data) Line (Fit) Line (Confidence bounds) Line

delete(h([3 4])) %delete bounds

How to display a linear regression line along with the scatter plot (6)

9 Comments

Show 7 older commentsHide 7 older comments

HAT on 15 Feb 2023

Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/1913105-how-to-display-a-linear-regression-line-along-with-the-scatter-plot#comment_2617470

  • Link

    Direct link to this comment

    https://jmaab.mathworks.com/matlabcentral/answers/1913105-how-to-display-a-linear-regression-line-along-with-the-scatter-plot#comment_2617470

Edited: HAT on 15 Feb 2023

@Dyuman Joshi Thanks, but how can I include the expression y=1+x instead of "Fit", and with the same scatter points (dots instead of x)?

I wanted the scatter points (dots) remain the same, instead of x.

Dyuman Joshi on 15 Feb 2023

Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/1913105-how-to-display-a-linear-regression-line-along-with-the-scatter-plot#comment_2617525

  • Link

    Direct link to this comment

    https://jmaab.mathworks.com/matlabcentral/answers/1913105-how-to-display-a-linear-regression-line-along-with-the-scatter-plot#comment_2617525

Open in MATLAB Online

%Two data x and y

x = [0.06309839457075230.05693107619633370.09028491744333860.05865575645363810.03341446972672780.07126318804719290.07599369681925050.1237383391828880.02970326371090490.07958922825838190.02822615962940580.05371832499871050.1505722574324090.09779636990330210.08852692083261750.08663963852494920.06692985048651100.03443124139251160.03486679608968030.04936561884930810.02706505382994660.03809683374340810.08884456429543780.08959452988850490.05413598539551820.07443219954648170.09135045866358170.06565543006217120.1172591952930690.07132773533507690.03373605523729400.08067380458939770.01879387692142770.1122039289348480.03448906166116090.1120701004623410.03543402102419640.04857534580997900.1007188832239410.08754117406964600.03561330318588130.05828179863562050.04827906342289890.1589973229920620.07299922900970770.04712447430219200.04460848096593500.05113160238979590.05064962538439760.1493807528938380.06890454808273980.05185330619200890.02097885521116530.08533212175630260.09334527596721510.06323211220103040.09890456245366580.1186469453957640.07463944914258170.05633649734147520.06994053863106580.1763135304489040.08050859674232520.07119517069343920.09858737778335320.03611237537507500.1076203229906260.1559530113092760.07038443592225950.06983003496596530.01336932916192660.05632515380760250.04205943357734830.02099298072493190.08364806117569990.09187601533425810.1090710239880300.1120111484569820.09329468418169260.05272733055724430.06039650226899180.09978319703701410.08655659485505630.04518649190173010.05349285558988820.07536118495801760.09750807053429450.06413639677131380.04925283781379290.01158959638316130.03956753546458570.05435628125057300.03029120283161280.09529966575865370.07355693582133970.02044152913469910.02914874079428660.06779173912117210.04392156589517290.03338165490416100.04564035519523540.04865533993022020.04705384329017010.08119578579448990.05150618534835910.09146183193038290.05455849546641580.04965850333063480.03946723483194680.05512729490073500.05206441965539220.08344327647565730.07349598361265990.04644065495715000.05977282805885480.04102139626488290.06904048539612150.04968099765649870.03616692050940100.07303227046671410.1077305973500340.02484053550058770.05119423825764930.01895039377524950.04086802887939210.1207850372900730.05945152312210300.03282459665423760.05484581486869530.05299490388655310.07910291044705280.05542372528664840.09949083401251330.05423992437662950.05344481075533410.06735932733335770.09397911929003670.08233009457623240.04425666002408200.06106171212748780.08988843386442920.05897966828047150.09762099489736520.08684448015714540.06520493594170580.05370214996787300.03016545195017580.05125361543515390.03459874376280450.07792823362551900.08251540443375720.08597997771422920.04422071683689850.009872432635278130.09500217131842070.06134378225517520.1424415268164360.1008024302739120.04142167014987310.1090196756087900.02969130823362430.03477428601591560.1358097634201030.09845060292349620.06740374185930820.02294076662960690.01899968522704670.07435713351149890.08471928141228800.1150836906128880.1236961789667090.09564178039590830.05197928740587790.03930760755779760.06773653774106940.05914268578390450.08479462646718230.06067566482028720.07240677342994400.04308352013769810.08081108532927790.04984233753227010.01947088969741060.07511311867277410.03487263898201650.07406122886704350.05943355162868840.05943481980176550.06824063084136350.05318946558616870.07437903025942930.02195581761074920.02721236622580390.1085384395378590.07470598962823690.09468588223297400.05692767023968260.01493835251691370.05980501840408440.05454003093042100.03430027318035690.08116874052151880.03882679548564320.1086318001002040.06745774263631810.05685443418378040.01081676197308900.04230296423731160.03777518600772560.03935527223300520.07101539871478490.1218323511381840.07693823765095650.09435856405268140.02571388418044950.1061639048143680.03643312446913570.06044182707484090.07387447949806140.1376934410016740.0459022534232133];

y = [0.02508616939125110.005851904584649800.06803699267915280.01235495625868420.02825784262639280.02898788785128770.01832478152402380.02640467693512470.02983945888291290.01843754015416120.01282343939982600.02520503879260580.02955893747770740.01725815127142850.01983229143171100.05106121221090210.02653921019727720.01665424591143240.06140140187435780.01215600045568790.007187486930920650.01530447362533970.02931769695732870.01758691749216160.01146932375750250.05065900015594090.006104658398886970.01264871097736350.07025559228962820.04185702990609150.002360605321422300.04555168314593130.01108001401434060.02200562988598200.01056527068648780.03731364966526350.03879075833997520.04054890561585150.05483303838306990.03131050462741290.01633614920311270.02242567993869370.03409183578322050.08565875920792770.02372819659425350.01850484026394380.01605536279792750.02487645045941000.006710411559444680.008602252703395450.02842315208788380.01488588214414660.02408405346431920.03248764875004750.01863923520079120.02903601080408250.03920293193574570.005147746807632230.04037344913441470.005535149481286240.01594729127363010.03910147474113820.01245042942226370.03562269453655350.01816283488626090.01786817791879540.008527726534151020.01381551792770360.01287975068836400.01757290802578200.03326496229591980.01321449196161320.01421899605877210.008674722208722890.01385856439633980.08080963008683750.02758595595651180.04306588379252900.04375295799421200.04321430877500070.02166440208035450.02824670313100190.02641980427494840.01865362058347010.02049446175892890.01614207505089470.04196421047287290.02662221277170960.02382606841623980.01673233865258800.02111662374901620.01384894514545490.01717114934857410.01981547348052070.02882560164927440.02177443980623730.02246401374338890.01192035431071330.01852851059329220.02768439043805910.02604602934448230.01167391734912310.03644104845792420.02617992587653700.01432957287985270.06180402521198640.03607622978139380.02992191014572120.01221044192388760.02245331538654880.04663429690068530.04392416479062210.02391866597289510.04295360630565060.02332041803987100.02579523408652140.04422638103152390.004652671023481240.008314119756583380.02368930524997210.03573911327841130.02144722261569450.03281760166651200.01679887662486930.02028378330330650.01191480521059290.02288268674627090.03437817081711760.05408342362560890.02532217118081910.04683238963675220.07426868195099080.01499216032334250.02993636828616560.01404228004761850.009234665744434230.03393424656031260.02915567745814040.02648005663618670.02540964164753420.02723399019516170.03061757840231540.02658228133770050.02856888114346440.02642818442911060.008469949739236200.01263903471571100.01178236936381920.01064385688237980.02620670375311810.03218054387415190.01487501703890710.01520491010840410.01079179178222010.03602501098042130.02270160643298700.02135934751347250.02442212649283070.04116646680124720.04019678224628380.003102150603142170.03327187839083830.05295847107765880.01354454957371360.008609164878156000.02793237398141910.01605243043049930.01714574035157510.01357765479894870.01063349070076620.02834725147901110.007763856519468300.02398863302822520.01992173257932690.01683000204905450.01981692178225890.01016890244086790.04217152018978650.01508275027525570.02264026156704000.03840037540975820.02439507995838780.02217027907751740.005676357881481870.01849356805114840.009574579838463420.03944340471769480.01378015384557110.006691083665336190.01756979856105050.02571160119947900.02060429770543870.01354852374646910.007413083865351770.01790471405585570.02589819160376720.01570924821449790.03013732000504650.01378315390614410.01714130501252070.01505720446094570.009383182067219050.02889209568875290.05689369870924980.05632160234276680.02386738374113450.007973603096864640.02043170222339950.03269979164399860.07261277693026760.01514350396627880.01232171648086810.01832598746924330.02148468765592650.01402008538721360.03766925500800250.01576112723160320.02585600420594970.01469396499930020.02531022027067200.0188959246844452];

s = scatter(x,y,'.');

xlabel('x')

ylabel('y')

How to display a linear regression line along with the scatter plot (9)

mdl = fitlm(x,y);

% use handle for plotting

h = plot(mdl)

h =

4×1 Line array: Line (Data) Line (Fit) Line (Confidence bounds) Line

h(1).Marker = '.';

h(1).Color = [0 0.4470 0.7410];

legend('Data','y = 1 + x')

delete(h([3 4])) %delete bounds

How to display a linear regression line along with the scatter plot (10)

Les Beckham on 15 Feb 2023

Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/1913105-how-to-display-a-linear-regression-line-along-with-the-scatter-plot#comment_2617550

  • Link

    Direct link to this comment

    https://jmaab.mathworks.com/matlabcentral/answers/1913105-how-to-display-a-linear-regression-line-along-with-the-scatter-plot#comment_2617550

Contrary to what is implied by the confusing output of fitlm (see below underlined and bold), y = 1 + x is not the equation of the fit line.

mdl =

Linear regression model:

y ~ 1 + x1

Estimated Coefficients:

Estimate SE tStat pValue

________ _________ ______ __________

(Intercept) 0.017481 0.0022911 7.6298 7.1359e-13

x1 0.1106 0.031273 3.5367 0.00049439

It is actually y = 0.017481 + 0.1106*x.

Dyuman Joshi on 15 Feb 2023

Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/1913105-how-to-display-a-linear-regression-line-along-with-the-scatter-plot#comment_2617565

  • Link

    Direct link to this comment

    https://jmaab.mathworks.com/matlabcentral/answers/1913105-how-to-display-a-linear-regression-line-along-with-the-scatter-plot#comment_2617565

Thanks for the comment, Les. I am aware of the "formula" and the actual equation, however, I answered according to the requirements of OP.

Les Beckham on 15 Feb 2023

Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/1913105-how-to-display-a-linear-regression-line-along-with-the-scatter-plot#comment_2617600

  • Link

    Direct link to this comment

    https://jmaab.mathworks.com/matlabcentral/answers/1913105-how-to-display-a-linear-regression-line-along-with-the-scatter-plot#comment_2617600

Open in MATLAB Online

Note that this can also be done with polyfit (in base Matlab):

%Two data x and y

x = [0.06309839457075230.05693107619633370.09028491744333860.05865575645363810.03341446972672780.07126318804719290.07599369681925050.1237383391828880.02970326371090490.07958922825838190.02822615962940580.05371832499871050.1505722574324090.09779636990330210.08852692083261750.08663963852494920.06692985048651100.03443124139251160.03486679608968030.04936561884930810.02706505382994660.03809683374340810.08884456429543780.08959452988850490.05413598539551820.07443219954648170.09135045866358170.06565543006217120.1172591952930690.07132773533507690.03373605523729400.08067380458939770.01879387692142770.1122039289348480.03448906166116090.1120701004623410.03543402102419640.04857534580997900.1007188832239410.08754117406964600.03561330318588130.05828179863562050.04827906342289890.1589973229920620.07299922900970770.04712447430219200.04460848096593500.05113160238979590.05064962538439760.1493807528938380.06890454808273980.05185330619200890.02097885521116530.08533212175630260.09334527596721510.06323211220103040.09890456245366580.1186469453957640.07463944914258170.05633649734147520.06994053863106580.1763135304489040.08050859674232520.07119517069343920.09858737778335320.03611237537507500.1076203229906260.1559530113092760.07038443592225950.06983003496596530.01336932916192660.05632515380760250.04205943357734830.02099298072493190.08364806117569990.09187601533425810.1090710239880300.1120111484569820.09329468418169260.05272733055724430.06039650226899180.09978319703701410.08655659485505630.04518649190173010.05349285558988820.07536118495801760.09750807053429450.06413639677131380.04925283781379290.01158959638316130.03956753546458570.05435628125057300.03029120283161280.09529966575865370.07355693582133970.02044152913469910.02914874079428660.06779173912117210.04392156589517290.03338165490416100.04564035519523540.04865533993022020.04705384329017010.08119578579448990.05150618534835910.09146183193038290.05455849546641580.04965850333063480.03946723483194680.05512729490073500.05206441965539220.08344327647565730.07349598361265990.04644065495715000.05977282805885480.04102139626488290.06904048539612150.04968099765649870.03616692050940100.07303227046671410.1077305973500340.02484053550058770.05119423825764930.01895039377524950.04086802887939210.1207850372900730.05945152312210300.03282459665423760.05484581486869530.05299490388655310.07910291044705280.05542372528664840.09949083401251330.05423992437662950.05344481075533410.06735932733335770.09397911929003670.08233009457623240.04425666002408200.06106171212748780.08988843386442920.05897966828047150.09762099489736520.08684448015714540.06520493594170580.05370214996787300.03016545195017580.05125361543515390.03459874376280450.07792823362551900.08251540443375720.08597997771422920.04422071683689850.009872432635278130.09500217131842070.06134378225517520.1424415268164360.1008024302739120.04142167014987310.1090196756087900.02969130823362430.03477428601591560.1358097634201030.09845060292349620.06740374185930820.02294076662960690.01899968522704670.07435713351149890.08471928141228800.1150836906128880.1236961789667090.09564178039590830.05197928740587790.03930760755779760.06773653774106940.05914268578390450.08479462646718230.06067566482028720.07240677342994400.04308352013769810.08081108532927790.04984233753227010.01947088969741060.07511311867277410.03487263898201650.07406122886704350.05943355162868840.05943481980176550.06824063084136350.05318946558616870.07437903025942930.02195581761074920.02721236622580390.1085384395378590.07470598962823690.09468588223297400.05692767023968260.01493835251691370.05980501840408440.05454003093042100.03430027318035690.08116874052151880.03882679548564320.1086318001002040.06745774263631810.05685443418378040.01081676197308900.04230296423731160.03777518600772560.03935527223300520.07101539871478490.1218323511381840.07693823765095650.09435856405268140.02571388418044950.1061639048143680.03643312446913570.06044182707484090.07387447949806140.1376934410016740.0459022534232133];

y = [0.02508616939125110.005851904584649800.06803699267915280.01235495625868420.02825784262639280.02898788785128770.01832478152402380.02640467693512470.02983945888291290.01843754015416120.01282343939982600.02520503879260580.02955893747770740.01725815127142850.01983229143171100.05106121221090210.02653921019727720.01665424591143240.06140140187435780.01215600045568790.007187486930920650.01530447362533970.02931769695732870.01758691749216160.01146932375750250.05065900015594090.006104658398886970.01264871097736350.07025559228962820.04185702990609150.002360605321422300.04555168314593130.01108001401434060.02200562988598200.01056527068648780.03731364966526350.03879075833997520.04054890561585150.05483303838306990.03131050462741290.01633614920311270.02242567993869370.03409183578322050.08565875920792770.02372819659425350.01850484026394380.01605536279792750.02487645045941000.006710411559444680.008602252703395450.02842315208788380.01488588214414660.02408405346431920.03248764875004750.01863923520079120.02903601080408250.03920293193574570.005147746807632230.04037344913441470.005535149481286240.01594729127363010.03910147474113820.01245042942226370.03562269453655350.01816283488626090.01786817791879540.008527726534151020.01381551792770360.01287975068836400.01757290802578200.03326496229591980.01321449196161320.01421899605877210.008674722208722890.01385856439633980.08080963008683750.02758595595651180.04306588379252900.04375295799421200.04321430877500070.02166440208035450.02824670313100190.02641980427494840.01865362058347010.02049446175892890.01614207505089470.04196421047287290.02662221277170960.02382606841623980.01673233865258800.02111662374901620.01384894514545490.01717114934857410.01981547348052070.02882560164927440.02177443980623730.02246401374338890.01192035431071330.01852851059329220.02768439043805910.02604602934448230.01167391734912310.03644104845792420.02617992587653700.01432957287985270.06180402521198640.03607622978139380.02992191014572120.01221044192388760.02245331538654880.04663429690068530.04392416479062210.02391866597289510.04295360630565060.02332041803987100.02579523408652140.04422638103152390.004652671023481240.008314119756583380.02368930524997210.03573911327841130.02144722261569450.03281760166651200.01679887662486930.02028378330330650.01191480521059290.02288268674627090.03437817081711760.05408342362560890.02532217118081910.04683238963675220.07426868195099080.01499216032334250.02993636828616560.01404228004761850.009234665744434230.03393424656031260.02915567745814040.02648005663618670.02540964164753420.02723399019516170.03061757840231540.02658228133770050.02856888114346440.02642818442911060.008469949739236200.01263903471571100.01178236936381920.01064385688237980.02620670375311810.03218054387415190.01487501703890710.01520491010840410.01079179178222010.03602501098042130.02270160643298700.02135934751347250.02442212649283070.04116646680124720.04019678224628380.003102150603142170.03327187839083830.05295847107765880.01354454957371360.008609164878156000.02793237398141910.01605243043049930.01714574035157510.01357765479894870.01063349070076620.02834725147901110.007763856519468300.02398863302822520.01992173257932690.01683000204905450.01981692178225890.01016890244086790.04217152018978650.01508275027525570.02264026156704000.03840037540975820.02439507995838780.02217027907751740.005676357881481870.01849356805114840.009574579838463420.03944340471769480.01378015384557110.006691083665336190.01756979856105050.02571160119947900.02060429770543870.01354852374646910.007413083865351770.01790471405585570.02589819160376720.01570924821449790.03013732000504650.01378315390614410.01714130501252070.01505720446094570.009383182067219050.02889209568875290.05689369870924980.05632160234276680.02386738374113450.007973603096864640.02043170222339950.03269979164399860.07261277693026760.01514350396627880.01232171648086810.01832598746924330.02148468765592650.01402008538721360.03766925500800250.01576112723160320.02585600420594970.01469396499930020.02531022027067200.0188959246844452];

coeffs = polyfit(x, y, 1)

coeffs = 1×2

0.1106 0.0175

xfit = [0 max(x)];

yfit = polyval(coeffs, xfit);

plot(x, y, '.', xfit, yfit, 'r-');

xlabel('x')

ylabel('y')

legend('data', sprintf('y = %.6f*x + %.6f', coeffs))

grid on

How to display a linear regression line along with the scatter plot (14)

HAT on 15 Feb 2023

Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/1913105-how-to-display-a-linear-regression-line-along-with-the-scatter-plot#comment_2617610

  • Link

    Direct link to this comment

    https://jmaab.mathworks.com/matlabcentral/answers/1913105-how-to-display-a-linear-regression-line-along-with-the-scatter-plot#comment_2617610

Thanks. But I have still doubt, whether y=1+x is a fit for the given data or not. I wonder if you confirm me whether y=1+x is a linear regression fit for the given data.

Les Beckham on 15 Feb 2023

Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/1913105-how-to-display-a-linear-regression-line-along-with-the-scatter-plot#comment_2617635

  • Link

    Direct link to this comment

    https://jmaab.mathworks.com/matlabcentral/answers/1913105-how-to-display-a-linear-regression-line-along-with-the-scatter-plot#comment_2617635

@Dyuman Joshi I realize that you were just doing that because it is what OP asked for.

@HAT y = 1 + x is definitely NOT the equation of the fit line for this data as I said in my previous comment:

"It is actually y = 0.017481 + 0.1106*x".

Dyuman Joshi on 15 Feb 2023

Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/1913105-how-to-display-a-linear-regression-line-along-with-the-scatter-plot#comment_2617645

  • Link

    Direct link to this comment

    https://jmaab.mathworks.com/matlabcentral/answers/1913105-how-to-display-a-linear-regression-line-along-with-the-scatter-plot#comment_2617645

No, it is not the regression fit.

It is an approximation, giving an idea of the order of the equation (via a particular notation).

The regression fit is as mentioned by Les in the comment above.

HAT on 15 Feb 2023

Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/1913105-how-to-display-a-linear-regression-line-along-with-the-scatter-plot#comment_2617650

  • Link

    Direct link to this comment

    https://jmaab.mathworks.com/matlabcentral/answers/1913105-how-to-display-a-linear-regression-line-along-with-the-scatter-plot#comment_2617650

Edited: HAT on 15 Feb 2023

Thank you very much. Now I edited my question.

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

AI, Data Science, and StatisticsCurve Fitting ToolboxLinear and Nonlinear Regression

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

Tags

  • matlab
  • regression

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How to display a linear regression line along with the scatter plot (19)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

How to display a linear regression line along with the scatter plot (2024)

FAQs

How to plot a regression line in a scatter plot? ›

A scatter plot can be created using the function plot(x, y). The function lm() will be used to fit linear models between y and x. A regression line will be added on the plot using the function abline(), which takes the output of lm() as an argument. You can also add a smoothing line using the function loess().

How do you interpret a scatter plot with a regression line answer? ›

If the regression line has a positive slope, the data has a positive linear relationship; if the regression line of the data has a negative slope, the data has a negative linear relationship. If the data is clustered tightly around its regression line, we might say it shows a strong linear relationship.

How to display the equation of the regression line on a scatter chart? ›

Once we have recreated the scatter plot, we find the equation of the regression line by clicking the three dots at the top right of the plot, selecting “Edit chart,” then clicking on “Customize” and “Series.” We add the regression line by checking the box next to “Trendline,” and then we show the equation by selecting ...

How do I add a linear regression line to a scatter plot in Excel? ›

Create your regression curve by making a scatter plot. Add the regression line by choosing the “Layout” tab in the “Chart Tools” menu. Then select “Trendline” and choose the “Linear Trendline” option, and the line will appear as shown above.

How do you write a linear regression equation from a scatter plot? ›

The Linear Regression Equation

The equation has the form Y= a + bX, where Y is the dependent variable (that's the variable that goes on the Y axis), X is the independent variable (i.e. it is plotted on the X axis), b is the slope of the line and a is the y-intercept.

How do you show regression results on a graph? ›

15 Ways to Visualize Regression Results!
  1. 2.1 Ribbon Plot of Expected Values.
  2. 2.2 Scatterplot of Expected Values.
  3. 2.3 Line Plot of Expected Values, by Percentile.
  4. 2.4 Ribbon Plot by Level of Confidence.
  5. 2.5 2-D Density Plane of Expected Values.
  6. 2.6 Two Ribbons or More!
  7. 2.7 Dot and Whisker Plots.

How do you find the regression line on a graph? ›

We determine the correlation coefficient for bivariate data, which helps understand the relationship between variables. We then build the equation for the least squares line, using standard deviations and the correlation coefficient. The regression line equation y hat = mx + b is calculated.

How do you add a regression line to a scatter plot on a TI 84? ›

Press [STAT] to enter the statistics menu. Press the right arrow key to reach the CALC menu and then press 4: LinReg(ax+b). Ensure Xlist is set at L1, Ylist is set at L2 and Store RegEQ is set at Y1 by pressing [VARS] [→] 1:Function and 1:Y1. Scroll down to Calculate and press [ENTER].

How do you add a linear regression line? ›

Charting a Regression in Excel

To add a regression line, choose "Add Chart Element" from the "Chart Design" menu. In the dialog box, select "Trendline" and then "Linear Trendline." To add the R2 value, select "More Trendline Options" from the "Trendline" menu.

How to draw regression line on scatter plot TI-84? ›

TI-84: Least Squares Regression Line (LSRL)
  1. Enter your data in L1 and L2. Note: Be sure that your Stat Plot is on and indicates the Lists you are using.
  2. Go to [STAT] "CALC" "8: LinReg(a+bx). This is the LSRL.
  3. Enter L1, L2, Y1 at the end of the LSRL. ...
  4. To view, go to [Zoom] "9: ZoomStat".
Jan 10, 2023

How do you plot a regression model? ›

Create Scatter Plot for Simple Linear Regression

pValue of the Weight variable is very small, which means that the variable is statistically significant in the model. Visualize this result by creating a scatter plot of the data, along with a fitted curve and its 95% confidence bounds, using the plot function.

How do I add a regression line to a scatter plot in Matplotlib? ›

By plotting the line m*x + b , we are adding the regression line to our scatterplot. You can further customize your plot with additional Matplotlib features, such as setting labels for the x and y axes, creating a title, or adjusting the figure size.

Top Articles
Latest Posts
Article information

Author: Golda Nolan II

Last Updated:

Views: 5500

Rating: 4.8 / 5 (58 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Golda Nolan II

Birthday: 1998-05-14

Address: Suite 369 9754 Roberts Pines, West Benitaburgh, NM 69180-7958

Phone: +522993866487

Job: Sales Executive

Hobby: Worldbuilding, Shopping, Quilting, Cooking, Homebrewing, Leather crafting, Pet

Introduction: My name is Golda Nolan II, I am a thoughtful, clever, cute, jolly, brave, powerful, splendid person who loves writing and wants to share my knowledge and understanding with you.