[ncl-talk] Scatter Plot and Regression Line

Dennis Shea shea at ucar.edu
Fri Jun 15 09:31:44 MDT 2018


Hi Melissa,

The regression calculation is not sensitive to the data ordering,

However, drawing the line requires that the 'x' [abscissa] must be
monotonic [ascending or descending].

The following example draws a polynomial. You can do just the regression
line
https://www.ncl.ucar.edu/Applications/Scripts/regress_6.ncl

;**************************
; 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)

Use the 'xx' and ''y' for your line.

FYI:   dim_pqsort_n can be quite useful


On Fri, Jun 15, 2018 at 9:07 AM, Melissa Lazenby <M.Lazenby at sussex.ac.uk>
wrote:

> Dear NCL Users
>
> 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.
>
> 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.
>
> Any advice regarding this issue would be very much appreciated!
>
> Below is my code for the plot.
>
> Many thanks for your help in advance.
>
> Kindest Regards
> Melissa
>
> ; ***********************************************
> ; scatter_1.ncl
> ;
> ; Concepts illustrated:
> ;   - Drawing a scatter plot
> ;   - Changing the markers in an XY plot
> ;   - Changing the marker color in an XY plot
> ;   - Changing the marker size in an XY plot
> ;   - Generating dummy data using "random_chi"
> ;
> ; ***********************************************
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
> ;************************************************
> begin
>
>
> 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"/)
>
>
> ;read in M* and DeltaM*
>
>  in = addfile("/research/geog/data2/DATA/mlazenby/Chadwick_
> Analysis/ChadDeltaMstarANNUAL/M*_Amon_CanESM2_M*1.nc","r")
>
>  in2 = addfile("/research/geog/data2/DATA/mlazenby/Chadwick_
> Analysis/ChadDeltaMstarANNUAL/Mstar_Amon_CanESM2_Delta_Mstar.nc","r")
>
>  Mstar = in->pr(0,0,{-30:30},{-180:180})
>  DeltaMstar = in2->pr(0,0,{-30:30},{-180:180})
>
> printVarSummary(Mstar)
> printVarSummary(DeltaMstar)
>
> ;print(Mstar)
>
> ;************************************************
> ; Create x and calculate the regression coefficient.
> ; Note regline works on one dimensional arrays.
> ;************************************************
>    ;x     = ispan(0,dimsizes(Mstar)-1,1)*1.
>    rc    = regCoef(Mstar,DeltaMstar)
>    print(rc)
> ;************************************************
> ; Create an array to hold both the original data
> ; and the calculated regression line.
> ;************************************************
>  ;data      = new ( (/2,dimsizes(Mstar)/), typeof(Mstar))
>  ;data(0,:) = Mstar
> ; y = mx+b
> ; m is the slope:       rc      returned from regline
> ; b is the y intercept: rc at yave attribute of rc returned from regline
>  ;data(1,:) = rc*(Mstar-rc at xave) + rc at yave
>
> ;************************************************
> ; plotting parameters
> ;************************************************
>   wks   = gsn_open_wks ("X11","Alpha_Mstar_vs_Delta_Mstar")            ;
> open workstation
>
>   res                   = True                     ; plot mods desired
>   res at gsnMaximize       = True                     ; maximize plot
>   res at xyMarkLineMode    = "Markers"                ; choose to use markers
>   res at xyMarkers         =  1                      ; choose type of
> marker
>   res at xyMarkerColor     = "Black"               ; Marker color
>   res at xyMarkerSizeF     = 0.01                     ; Marker size (default
> 0.01)
>   res at tiXAxisString   = "Mstar"
>   res at tiYAxisString   = "~F8~D~F21~Mstar~"
>   ;res at gsnDraw             = False              ; do not draw the plot
>   ;res at gsnFrame            = False
>   res at tiMainString     = "CanESM2 Plot of Mstar vs Delta Mstar"
>
>   sres                   = True                     ; plot mods desired
>   sres at xyFillColors      = True
>   sres at gsnMaximize       = True                     ; maximize plot
>   sres at xyMarkLineMode    = "Markers"                ; choose to use
> markers
>   sres at xyMarkers         =  1                      ; choose type of
> marker
>   sres at xyMarkerColor     = "mediumpurple1"               ; Marker color
>   sres at xyMarkerSizeF     = 0.06                     ; Marker size
> (default 0.01)
>
>   ;res at trYMinF            =  -0.3               ; min value on y-axis
>   ;res at trYMaxF            =  0.8               ; max value on y-axis
>   ;res at trXMinF            =  -0.10                  ; min value on x-axis
>   ;res at trXMaxF            =  0.40                  ; max value on x-axis
>
>   ;res at tmXBMode         = "Manual"
>   ;res at tmYLMode         = "Manual"
>   ;res at tmXBTickSpacingF = 0.1
>   ;res at tmYLTickSpacingF = 2.
>
>
>   plot  = gsn_csm_xy(wks,Mstar,DeltaMstar,res)                    ;
> create plot
>   ;plot1  = gsn_csm_xy(wks,rc,res)        ; create plot
> end
>
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20180615/2c6aa448/attachment.html>


More information about the ncl-talk mailing list