Re: scatter plots

From: Adam Phillips (asphilli AT cgd.ucar.edu)
Date: Thu Jun 16 2005 - 11:16:43 MDT


Hi Sara,

Yes, although you can't do it by using gsn_csm_xy directly. (Correct me
if I'm wrong, anyone..) I would draw your array on a blank xy plot using
gsn_add_polymarker. First though, create an array that contains the
marker colors based on the enso values:

arr = new(dimsizes(enso),string)
  do gg = 0,dimsizes(enso)-1
     if (enso(gg).eq.-1) then
        arr(gg) = "blue"
     end if
     if (enso(gg).eq.0) then
        arr(gg) = "black"
     end if
     if (enso(gg).eq.1) then
        arr(gg) = "red"
     end if
  end do

Then draw a blank xy plot (don't draw or advance the frame):
darr = new(ny,float)
darr = 0
plot = gsn_csm_xy(wks,onset&year,darr,res)
; blank because Y axis range set from 124->365
(There are other ways of drawing a blank xy plot but timewise this way
was fastest for me.)

Then draw polymarkers using do loops:
  polyres = True
  polyres@gsMarkerIndex = 16 ; polymarker style
  polyres@gsMarkerSizeF = 10. ; polymarker size
  dum = new((/dimsizes(model),ny/),graphic)
  do gg = 0,ny-1
     polyres@gsMarkerColor = arr(gg)
     do hh = 0,dimsizes(model)-1
        dum(hh,gg) =
gsn_add_polymarker(wks,plot,onset&year(gg),onset(hh,gg),polyres)
     end do
  end do

I attached the script in its entirety here. I put fake values in the
onset array to test the script...

Hope that helps,
Adam

Sara Rauscher wrote:
> Hi,
>
> I have a question about scatter plots. Is it possible to control the
> color of the markers by the x-axis value? I have a time series of rainy
> season onset and withdrawal dates for a 22-year period (one per year),
> and I want to color them by year (binned into El Nino years, La Nina
> years, and neutral years).
>
> I attached the script I have been using (ne.scatter.enso.ncl), but right
> now all of the markers are black. I'd like to use the "enso" array (one
> value for each year) to color the markers.
>
> Thanks,
> sara
>
>
> ------------------------------------------------------------------------
>
> ; ***********************************************
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
> ;************************************************
>
> begin
>
> ;************************************************
> ; read in data
> ;************************************************
>
> nnrp = addfile ("../data/8203/onset.grell.nnrp.nc","r")
> echam = addfile ("../data/8203/onset.grell.echam.nc","r")
> sam = addfile ("../data/8203/onset.sam.8203.nc","r")
> gcm = addfile ("../data/8203/onset.echamgcm.8203.nc","r")
> f1 = addfile("/mstore/iri/modelling/rauscher/SAM/sam.cdf", "r")
>
> ; set values
> year1 = 1982 ;first year of sims
> year2 = 2003 ;end year of sims
>
> ; calcs based on set values
> ny = year2 - year1
> y1 = year1 + 1
>
> model = new((/2/),float)
> do i=0,dimsizes(model)-1
> model(i) = i
> end do
>
> onset = new((/dimsizes(model),ny/),float)
> year = new((/ny/), float)
> do i=0,ny-1
> year(i) = i+year1+1
> end do
>
> onset!0="model"
> onset&model = model
> onset!1="year"
> onset&year = year
>
> ;enso array (el nin0 = 1, neutral = 0, la nina = 1)
> ;this is a first guess array and not necessarily the correct event years (jfm +1 of event)
> ;array of values for years 1983-2002
> enso = (/1,0,-1,0,1,0,-1,0,0,1,0,0,1,-1,0,1,-1,-1,-1,0,0/)
>
> onset(0,:) = (/sam->aons(2,:)/)
> onset(1,:) = (/sam->awit(2,:)/)
>
> ;************************************************
> ; plotting parameters
> ;************************************************
> wks = gsn_open_wks ("ps","northeast") ; open workstation
>
> res = True ; plot mods desired
> res@tiMainString = "Northeast Onset and Withdrawal" ; add title
> res@trYMinF = 124
> res@trYMaxF = 365
> res@trXMinF =year1
> res@trXMaxF =year2
> res@tiXAxisString = "Year" ; xaxis string
> res@tiYAxisString = "Onset and Withdrawal Date" ; yaxis string
> res@xyMarkLineModes = (/"Markers","Markers"/)
> res@xyMarkerSizeF = 0.011
> res@xyMarkers = (/16,16/)
> res@xyMarkerColors = (/"black","black"/)
> res@tmXBOn = True ; have tick marks
> res@tmXBMode = "Explicit" ; label independently
> res@tmXBValues = (/1983,1984,1985,1986,1987,1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003/)
> res@tmXBLabels = (/"82-83","83-84","84-85","85-86","86-87","87-88","88-89","89-90","90-91","91-92","92-93","93-94","94-95","95-96","96-97","97-98","98-99","99-00","00-01","01-02","02-03"/)
>
> res@tmYLOn = True
> res@tmYLLabelsOn = True
> res@tmYLMinorOn = False
> res@tmYLMode = "Explicit"
> res@tmYLValues = (/124,154,185,216,244,275,305,336/)
> res@tmYLLabels = (/"Nov", "Dec", "Jan", "Feb", "Mar", "Apr", "May", "Jun"/)
>
> ;font stuff
> res@tiMainFont = 21
> res@tiXAxisFont = 21
> res@tiYAxisFont = 21
>
> res@tmXBLabelFont = 21
> res@tmXBLabelFontHeightF = 0.017
> res@tmXBLabelAngleF = 90
> res@tmXBLabelJust = "CenterLeft"
> res@tmYLLabelFont = 21
> res@tmYLLabelFontHeightF = 0.017
>
> plot = gsn_csm_xy (wks,onset&year,onset(:,:),res) ; create plot
>
> end
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk@ucar.edu
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk

-- 
--------------------------------------------------------------
Adam Phillips			             asphilli@ucar.edu
National Center for Atmospheric Research   tel: (303) 497-1726
ESSL/CGD                                   fax: (303) 497-1333
P.O. Box 3000				
Boulder, CO 80307-3000	  http://www.cgd.ucar.edu/cas/asphilli

; *********************************************** load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl" load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl" ;************************************************

begin

;************************************************ ; read in data ;************************************************ ; set values year1 = 1982 ;first year of sims year2 = 2003 ;end year of sims ; calcs based on set values ny = year2 - year1 y1 = year1 + 1 model = ispan(0,1,1)

onset = new((/dimsizes(model),ny/),float) year = new((/ny/), float) do i=0,ny-1 year(i) = i+year1+1 end do

onset!0="model" onset&model = model onset!1="year" onset&year = year

;enso array (el nin0 = 1, neutral = 0, la nina = 1) ;this is a first guess array and not necessarily the correct event years (jfm +1 of event) ;array of values for years 1983-2002 enso = (/1,0,-1,0,1,0,-1,0,0,1,0,0,1,-1,0,1,-1,-1,-1,0,0/)

onset(0,:) = random_uniform(150,300,ny) onset(1,:) = random_uniform(160,310,ny) arr = new(dimsizes(enso),string) ;set up array to color polymarkers by enso value do gg = 0,dimsizes(enso)-1 if (enso(gg).eq.-1) then arr(gg) = "blue" end if if (enso(gg).eq.0) then arr(gg) = "black" end if if (enso(gg).eq.1) then arr(gg) = "red" end if end do ;************************************************ ; plotting parameters ;************************************************ wks = gsn_open_wks ("ps","northeast") ; open workstation

res = True ; plot mods desired res@tiMainString = "Northeast Onset and Withdrawal" ; add title res@trYMinF = 124 res@trYMaxF = 365 res@trXMinF =year1 res@trXMaxF =year2+1 res@tiXAxisString = "Year" ; xaxis string res@tiYAxisString = "Onset and Withdrawal Date" ; yaxis string res@tmXBOn = True ; have tick marks res@tmXBMode = "Explicit" ; label independently res@tmXBValues = (/1983,1984,1985,1986,1987,1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003/) res@tmXBLabels = (/"82-83","83-84","84-85","85-86","86-87","87-88","88-89","89-90","90-91","91-92","92-93","93-94","94-95","95-96","96-97","97-98","98-99","99-00","00-01","01-02","02-03"/)

res@tmYLOn = True res@tmYLLabelsOn = True res@tmYLMinorOn = False res@tmYLMode = "Explicit" res@tmYLValues = (/124,154,185,216,244,275,305,336/) res@tmYLLabels = (/"Nov", "Dec", "Jan", "Feb", "Mar", "Apr", "May", "Jun"/)

;font stuff res@tiMainFont = 21 res@tiXAxisFont = 21 res@tiYAxisFont = 21

res@tmXBLabelFont = 21 res@tmXBLabelFontHeightF = 0.017 res@tmXBLabelAngleF = 90 res@tmXBLabelJust = "CenterLeft" res@tmYLLabelFont = 21 res@tmYLLabelFontHeightF = 0.017 res@gsnDraw = False res@gsnFrame = False darr = new(ny,float) darr = 0 plot = gsn_csm_xy(wks,onset&year,darr,res) ;draw blank xy plot delete(darr) polyres = True polyres@gsMarkerIndex = 16 ; polymarker style polyres@gsMarkerSizeF = 10. ; polymarker size

dum = new((/dimsizes(model),ny/),graphic) do gg = 0,ny-1 polyres@gsMarkerColor = arr(gg) ; color the polymarkers by enso value do hh = 0,dimsizes(model)-1 ; for each model run dum(hh,gg) = gsn_add_polymarker(wks,plot,onset&year(gg),onset(hh,gg),polyres) end do end do draw(plot) frame(wks) end

_______________________________________________ ncl-talk mailing list ncl-talk@ucar.edu http://mailman.ucar.edu/mailman/listinfo/ncl-talk



This archive was generated by hypermail 2b29 : Mon Jun 20 2005 - 08:43:23 MDT