<html dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" id="owaParaStyle"></style>
</head>
<body fpstyle="1" ocsi="0">
<div style="direction: ltr;font-family: Tahoma;color: #000000;font-size: 10pt;">Dear NCL Users
<div><br>
</div>
<div>I am currently trying to plot 2 variables against one another (gridpoint by gridpoint) and draw in a regression/best fit line and determine the slope of the best fit line.</div>
<div><br>
</div>
<div>I have been able to plot the scatter plot which I am happy with please see attached. However I am struggling with drawing the best-fit line through these points and then outputting the gradient of the regression line.</div>
<div><br>
</div>
<div>Any advice regarding this issue would be very much appreciated!</div>
<div><br>
</div>
<div>Below is my code for the plot.</div>
<div><br>
</div>
<div>Many thanks for your help in advance.</div>
<div><br>
</div>
<div>Kindest Regards</div>
<div>Melissa</div>
<div><br>
</div>
<div>
<div>; ***********************************************</div>
<div>; scatter_1.ncl</div>
<div>;</div>
<div>; Concepts illustrated:</div>
<div>; - Drawing a scatter plot</div>
<div>; - Changing the markers in an XY plot</div>
<div>; - Changing the marker color in an XY plot</div>
<div>; - Changing the marker size in an XY plot</div>
<div>; - Generating dummy data using "random_chi"</div>
<div>;</div>
<div>; ***********************************************</div>
<div>load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"</div>
<div>load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"</div>
<div>load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"</div>
<div>;************************************************</div>
<div>begin</div>
<div><br>
</div>
<div><br>
</div>
<div>model = (/"bcc-csm1-1-m","CanESM2","CanESM2","CCSM4","CESM1-BGC","CESM1-CAM5","CSIRO-Mk3-6-0","FIO-ESM","GFDL-CM3","GFDL-ESM2G","GFDL-ESM2M","GISS-E2-H","HadGEM2-CC","HadGEM2-ES","IPSL-CM5A-LR","IPSL-CM5A-MR","MIROC5","MRI-CGCM3","NorESM1-M","NorESM1-ME"/)</div>
<div><br>
</div>
<div><br>
</div>
<div>;read in M* and DeltaM*</div>
<div><br>
</div>
<div> in = addfile("/research/geog/data2/DATA/mlazenby/Chadwick_Analysis/ChadDeltaMstarANNUAL/M*_Amon_CanESM2_M*1.nc","r")</div>
<div> </div>
<div> in2 = addfile("/research/geog/data2/DATA/mlazenby/Chadwick_Analysis/ChadDeltaMstarANNUAL/Mstar_Amon_CanESM2_Delta_Mstar.nc","r")</div>
<div><br>
</div>
<div> Mstar = in->pr(0,0,{-30:30},{-180:180})</div>
<div> DeltaMstar = in2->pr(0,0,{-30:30},{-180:180})</div>
<div><br>
</div>
<div>printVarSummary(Mstar)</div>
<div>printVarSummary(DeltaMstar)</div>
<div><br>
</div>
<div>;print(Mstar)</div>
<div><br>
</div>
<div>;************************************************</div>
<div>; Create x and calculate the regression coefficient.</div>
<div>; Note regline works on one dimensional arrays. </div>
<div>;************************************************</div>
<div> ;x = ispan(0,dimsizes(Mstar)-1,1)*1.</div>
<div> rc = regCoef(Mstar,DeltaMstar)</div>
<div> print(rc)</div>
<div>;************************************************</div>
<div>; Create an array to hold both the original data</div>
<div>; and the calculated regression line.</div>
<div>;************************************************</div>
<div> ;data = new ( (/2,dimsizes(Mstar)/), typeof(Mstar))</div>
<div> ;data(0,:) = Mstar</div>
<div>; y = mx+b </div>
<div>; m is the slope: rc returned from regline</div>
<div>; b is the y intercept: rc@yave attribute of rc returned from regline</div>
<div> ;data(1,:) = rc*(Mstar-rc@xave) + rc@yave</div>
<div><br>
</div>
<div>;************************************************</div>
<div>; plotting parameters</div>
<div>;************************************************</div>
<div> wks = gsn_open_wks ("X11","Alpha_Mstar_vs_Delta_Mstar") ; open workstation</div>
<div><br>
</div>
<div> res = True ; plot mods desired</div>
<div> res@gsnMaximize = True ; maximize plot</div>
<div> res@xyMarkLineMode = "Markers" ; choose to use markers</div>
<div> res@xyMarkers = 1 ; choose type of marker </div>
<div> res@xyMarkerColor = "Black" ; Marker color</div>
<div> res@xyMarkerSizeF = 0.01 ; Marker size (default 0.01)</div>
<div> res@tiXAxisString = "Mstar" </div>
<div> res@tiYAxisString = "~F8~D~F21~Mstar~"</div>
<div> ;res@gsnDraw = False ; do not draw the plot</div>
<div> ;res@gsnFrame = False </div>
<div> res@tiMainString = "CanESM2 Plot of Mstar vs Delta Mstar" </div>
<div> </div>
<div> sres = True ; plot mods desired</div>
<div> sres@xyFillColors = True</div>
<div> sres@gsnMaximize = True ; maximize plot</div>
<div> sres@xyMarkLineMode = "Markers" ; choose to use markers</div>
<div> sres@xyMarkers = 1 ; choose type of marker </div>
<div> sres@xyMarkerColor = "mediumpurple1" ; Marker color</div>
<div> sres@xyMarkerSizeF = 0.06 ; Marker size (default 0.01)</div>
<div><br>
</div>
<div> ;res@trYMinF = -0.3 ; min value on y-axis</div>
<div> ;res@trYMaxF = 0.8 ; max value on y-axis</div>
<div> ;res@trXMinF = -0.10 ; min value on x-axis</div>
<div> ;res@trXMaxF = 0.40 ; max value on x-axis</div>
<div><br>
</div>
<div> ;res@tmXBMode = "Manual"</div>
<div> ;res@tmYLMode = "Manual"</div>
<div> ;res@tmXBTickSpacingF = 0.1</div>
<div> ;res@tmYLTickSpacingF = 2.</div>
<div><br>
</div>
<div><br>
</div>
<div> plot = gsn_csm_xy(wks,Mstar,DeltaMstar,res) ; create plot</div>
<div> ;plot1 = gsn_csm_xy(wks,rc,res) ; create plot</div>
<div>end</div>
</div>
<div><br>
</div>
</div>
</body>
</html>