[ncl-talk] Scatter Plot and Regression Line
Melissa Lazenby
M.Lazenby at sussex.ac.uk
Fri Jun 15 09:07:19 MDT 2018
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20180615/3b3061a8/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Scatter_Plot_CanESM2.PNG
Type: image/png
Size: 41685 bytes
Desc: Scatter_Plot_CanESM2.PNG
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20180615/3b3061a8/attachment.png>
More information about the ncl-talk
mailing list