-
Notifications
You must be signed in to change notification settings - Fork 15
/
func_plot_coloredScatter.ncl
241 lines (218 loc) · 7.24 KB
/
func_plot_coloredScatter.ncl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
;; for drawNDCGrid()
;load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
;load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
;load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"
undef("plot_coloredScatter")
function plot_coloredScatter(x[*],y[*],c[*],title[1]:string,filename[1]:string)
begin
res = True
res@tiMainString = title
res@tiMainJust = "TopLeft"
res@tiMainFont = 21
res@tiMainFontHeightF = 0.018
res@tiMainPosition = "Left"
res@xyMarker = 1
res@xyMarkLineMode = "Markers"
res@gsnFrame = False
res@gsnDraw = False
wks = gsn_open_wks("ps",filename)
;gsn_define_colormap(wks,"temp_19lev")
gsn_define_colormap(wks,"so4_21")
nc = 21 ;
print("plot "+filename)
xmaxmin = nice_mnmxintvl(min(x),max(x),nc,True) ;; nc whatever
ymaxmin = nice_mnmxintvl(min(y),max(y),nc,True)
cmaxmin = nice_mnmxintvl(min(c),max(c),nc,True)
;; set x,y and color range if not setted
xmin = xmaxmin(0)
if(isatt(x,"min"))then
xmin = x@min
end if
xmax = xmaxmin(1)
if(isatt(x,"max"))then
xmax = x@max
end if
ymin = ymaxmin(0)
if(isatt(y,"min"))then
ymin = y@min
end if
ymax = ymaxmin(1)
if(isatt(y,"max"))then
ymax = y@max
end if
cmin = cmaxmin(0)
if(isatt(c,"min"))then
cmin = c@min
end if
cmax = cmaxmin(1)
if(isatt(c,"max"))then
cmax = c@max
end if
cint = (cmax - cmin)/nc
np = dimsizes(c)
tres = res
tres@xyMarkerColor= 0
plot = gsn_csm_xy(wks,(/xmin,xmax/),(/ymin,ymax/),tres)
pres =True
pres@gsMarkerIndex = 16
do i =0, np-1
if(.not.(ismissing(x(i)).or.ismissing(y(i)).or.ismissing(c(i))))then
pres@gsMarkerColor = floattoint((c(i)-cmin)/cint)+1
if(pres@gsMarkerColor .lt. 2)then
pres@gsMarkerColor = 2
end if
if(pres@gsMarkerColor .gt. nc)then
pres@gsMarkerColor = nc
end if
gsn_polymarker(wks,plot,x(i),y(i),pres)
end if
end do
lbres = True
lbres@lbAutoManage = False ; we control label bar
lbres@lbFillColors = ispan(2,nc,1); use nice strided colors
lbres@lbPerimOn = False ; Turn off labelbar perimeter.
lbres@lbMonoFillPattern = True ; one pattern, all solid
lbres@vpWidthF = 0.1 ; size
lbres@vpHeightF = 0.6
lbres@lbLabelFontHeightF = 0.012 ; label font height
lblabels = flt2string(fspan(cmin,cmax,nc))
lbx = 0.8
lby = 0.8
gsn_labelbar_ndc(wks,nc,lblabels,lbx,lby,lbres)
;;drawNDCGrid(wks)
draw(plot)
frame(wks)
return True
end
function plot_colorScatter(title[1]:string,filename[1]:string,x[*],y[*],c[*],ires:logical)
begin
if(all(ismissing(x)).or.all(ismissing(y)))then
print(" colorScatter(): "+filename+" all missing value")
return True
end if
res = True
res@tiMainString = title
res@tiMainJust = "TopLeft"
res@tiMainFont = 21
res@tiMainFontHeightF = 0.018
res@tiMainPosition = "Left"
res@xyMarker = 1
res@xyMarkLineMode = "Markers"
res@gsnFrame = False
res@gsnDraw = False
copy_VarAtts(ires,res)
stdshading = True
stdshading = False
if(num(.not.ismissing(x)).lt.3)then
stdshading = False
end if
if(stdshading)then
reg = regline(x,y)
reg@rstd = 1 ;; for test
regx = (/min(x),max(x),max(x),min(x)/)
regy = reg@yintercept+reg*regx
regy = (/regy(0)-reg@rstd,regy(1)-reg@rstd,regy(2)+reg@rstd,regy(3)+reg@rstd/)
ssres = True
ssres@tfPolyDrawOrder = "PreDraw"
ssres@gsFillColor = "SlateBlue"
regres = True
regres@txFontHeightF = 0.015
regres@txJust = "CenterLeft"
end if
wks = gsn_open_wks("ps",filename)
;gsn_define_colormap(wks,"temp_19lev")
gsn_define_colormap(wks,"so4_21")
nc = 21 ;
print("plot "+filename)
xmaxmin = nice_mnmxintvl(min(x),max(x),nc,True) ;; nc whatever
ymaxmin = nice_mnmxintvl(min(y),max(y),nc,True)
if(dimsizes(c).gt.2)then
cmaxmin = nice_mnmxintvl(min(c),max(c),nc,True)
monocolor = False
else
cmaxmin = (/c,c,c/)
monocolor = True
end if
;; set x,y and color range if not setted
xmin = xmaxmin(0)
if(isatt(x,"min"))then
xmin = x@min
end if
xmax = xmaxmin(1)
if(isatt(x,"max"))then
xmax = x@max
end if
ymin = ymaxmin(0)
if(isatt(y,"min"))then
ymin = y@min
end if
ymax = ymaxmin(1)
if(isatt(y,"max"))then
ymax = y@max
end if
cmin = cmaxmin(0)
if(isatt(c,"min"))then
cmin = c@min
end if
cmax = cmaxmin(1)
if(isatt(c,"max"))then
cmax = c@max
end if
cint = (cmax - cmin)/nc
np = dimsizes(x)
tres = res
tres@xyMarkerColor= 0
plot = gsn_csm_xy(wks,(/xmin,xmax/),(/ymin,ymax/),tres)
if(stdshading)then
printVarSummary(regx)
printVarSummary(regy)
gsn_polygon(wks,plot,regx,regy,ssres)
gsn_text_ndc(wks,"y = "+reg+"x + "+reg@yintercept,0.5,0.75,regres)
gsn_text_ndc(wks,"rstd = "+reg@rstd,0.5,0.73,regres)
end if
pres =True
pres@gsMarkerIndex = 16
pres@tfPolyDrawOrder = "Postdraw"
if(monocolor)then
pres@gsMarkerColor = toint(c)
end if
do i =0, np-1
if(.not.(ismissing(x(i)).or.ismissing(y(i))))then
if((.not.monocolor).and.(ismissing(c(i))))then
continue
end if
if(.not.monocolor)then
pres@gsMarkerColor = toint((c(i)-cmin)/cint+1)
if(pres@gsMarkerColor .lt. 2)then
pres@gsMarkerColor = 2
end if
if(pres@gsMarkerColor .gt. nc)then
pres@gsMarkerColor = nc
end if
end if
gsn_polymarker(wks,plot,x(i),y(i),pres)
end if
end do
if(.not.monocolor)then
lbres = True
lbres@lbAutoManage = False ; we control label bar
lbres@lbFillColors = ispan(2,nc,1); use nice strided colors
lbres@lbPerimOn = False ; Turn off labelbar perimeter.
lbres@lbMonoFillPattern = True ; one pattern, all solid
lbres@vpWidthF = 0.1 ; size
lbres@vpHeightF = 0.6
lbres@lbLabelFontHeightF = 0.012 ; label font height
lblabels = flt2string(fspan(cmin,cmax,nc))
lbx = 0.8
lby = 0.8
gsn_labelbar_ndc(wks,nc,lblabels,lbx,lby,lbres)
end if
;;drawNDCGrid(wks)
draw(plot)
frame(wks)
if(isvar("reg"))then
return reg
end if
return True
end