<div dir="ltr"><div><div><div>Hi Melissa,<br></div></div><br></div><div>The regression calculation is not sensitive to the data ordering,<br><br></div><div>However, drawing the line requires that the 'x' [abscissa] must be monotonic [ascending or descending].<br><br></div><div>The following example draws a polynomial. You can do just the regression line<br></div><a href="https://www.ncl.ucar.edu/Applications/Scripts/regress_6.ncl">https://www.ncl.ucar.edu/Applications/Scripts/regress_6.ncl</a><br><br><pre>;**************************
; Plot lines require that the abscissa be in monotonic order (ascending or descending)
; Note: markers do *not* require any ordering
;**************************

   mono = 1                            ; ascending=1 , descending=-1
   ii   =  dim_pqsort_n(x,mono,0)
   xx   = x(ii)                        ; ascending order 
   yy   = y(ii)<br><br></pre><pre>Use the 'xx' and ''y' for your line.<br><br></pre><pre>FYI:   dim_pqsort_n can be quite useful</pre></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jun 15, 2018 at 9:07 AM, Melissa Lazenby <span dir="ltr"><<a href="mailto:M.Lazenby@sussex.ac.uk" target="_blank">M.Lazenby@sussex.ac.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">




<div>
<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>; ******************************<wbr>*****************</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>; ******************************<wbr>*****************</div>
<div>load "$NCARG_ROOT/lib/ncarg/<wbr>nclscripts/csm/gsn_code.ncl"</div>
<div>load "$NCARG_ROOT/lib/ncarg/<wbr>nclscripts/csm/gsn_csm.ncl"</div>
<div>load "$NCARG_ROOT/lib/ncarg/<wbr>nclscripts/csm/contributed.<wbr>ncl"</div>
<div>;*****************************<wbr>*******************</div>
<div>begin</div>
<div><br>
</div>
<div><br>
</div>
<div>model = (/"bcc-csm1-1-m","CanESM2","<wbr>CanESM2","CCSM4","CESM1-BGC","<wbr>CESM1-CAM5","CSIRO-Mk3-6-0","<wbr>FIO-ESM","GFDL-CM3","GFDL-<wbr>ESM2G","GFDL-ESM2M","GISS-E2-<wbr>H","HadGEM2-CC","HadGEM2-ES","<wbr>IPSL-CM5A-LR","IPSL-CM5A-MR","<wbr>MIROC5","MRI-CGCM3","NorESM1-<wbr>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/<wbr>DATA/mlazenby/Chadwick_<wbr>Analysis/ChadDeltaMstarANNUAL/<wbr>M*_Amon_CanESM2_M*<a href="http://1.nc" target="_blank">1.nc</a>","r")</div>
<div> </div>
<div> in2 = addfile("/research/geog/data2/<wbr>DATA/mlazenby/Chadwick_<wbr>Analysis/ChadDeltaMstarANNUAL/<wbr>Mstar_Amon_CanESM2_Delta_<wbr>Mstar.nc","r")</div>
<div><br>
</div>
<div> Mstar = in->pr(0,0,{-30:30},{-180:180}<wbr>)</div>
<div> DeltaMstar = in2->pr(0,0,{-30:30},{-180:<wbr>180})</div>
<div><br>
</div>
<div>printVarSummary(Mstar)</div>
<div>printVarSummary(DeltaMstar)</div>
<div><br>
</div>
<div>;print(Mstar)</div>
<div><br>
</div>
<div>;*****************************<wbr>*******************</div>
<div>; Create x and calculate the regression coefficient.</div>
<div>; Note regline works on one dimensional arrays.   </div>
<div>;*****************************<wbr>*******************</div>
<div>   ;x     = ispan(0,dimsizes(Mstar)-1,1)*<wbr>1.</div>
<div>   rc    = regCoef(Mstar,DeltaMstar)</div>
<div>   print(rc)</div>
<div>;*****************************<wbr>*******************</div>
<div>; Create an array to hold both the original data</div>
<div>; and the calculated regression line.</div>
<div>;*****************************<wbr>*******************</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>;*****************************<wbr>*******************</div>
<div>; plotting parameters</div>
<div>;*****************************<wbr>*******************</div>
<div>  wks   = gsn_open_wks ("X11","Alpha_Mstar_vs_Delta_<wbr>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,<wbr>DeltaMstar,res)                    ; create plot</div>
<div>  ;plot1  = gsn_csm_xy(wks,rc,res)        ; create plot</div>
<div>end</div>
</div>
<div><br>
</div>
</div>
</div>

<br>______________________________<wbr>_________________<br>
ncl-talk mailing list<br>
<a href="mailto:ncl-talk@ucar.edu">ncl-talk@ucar.edu</a><br>
List instructions, subscriber options, unsubscribe:<br>
<a href="http://mailman.ucar.edu/mailman/listinfo/ncl-talk" rel="noreferrer" target="_blank">http://mailman.ucar.edu/<wbr>mailman/listinfo/ncl-talk</a><br>
<br></blockquote></div><br></div>