[ncl-talk] gsnXYFillColors with 2 Y axes

Mary Haley haley at ucar.edu
Tue Feb 3 09:18:19 MST 2015


[ My apologies, I see the question was reposted and handled by Adam. --Mary]

On Tue, Feb 3, 2015 at 9:16 AM, Mary Haley <haley at ucar.edu> wrote:

> Vandhna,
>
> Sorry about the delay in response.  I usually try to get to graphics
> questions much quicker than this.
>
> The error is telling you that one of the input X,Y arrays must be 2D
> (usually this is the Y array) and the other must be 1D (usually the X
> array).
>
> What the code does with the 2D Y array is that each leftmost element of Y
> represents a single curve, and then each color that you specific in
> gsnXYFillColors will represent the color between curves (you will then need
> to specify one fewer color than you have curves).
>
> I've attached a very simple example, which I copied from example
> "newcolor_16.ncl" at:
>
> http://www.ncl.ucar.edu/Applications/rgbacolor.shtml#ex16
>
> For the first frame, this example creates four curves, and fills between
> each curve.
>
> For the second frame, the same plot is drawn, but the fill is done with
> 0.5 opacity.
>
> --Mary
>
> On Tue, Jan 27, 2015 at 11:17 PM, Vandhna Nair <n.vandhna at gmail.com>
> wrote:
>
>> Hi all,
>>
>> I am trying to plot 4 curves (station observations), with their model
>> simulated ranges (min, max) as filled areas using gsnXYFillColors.
>>
>> I get the 4 curves and everything else just fine using the script below
>> (ignore highlighted part for now).
>>
>> ; ***********************************************
>> 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"
>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"
>> ; ***********************************************
>>
>> p1 = asciiread("Station_Suva_pr.csv",(/12,1/),"float")
>> p2 = asciiread("Station_Nadi_pr.csv",(/12,1/),"float")
>> p3 = asciiread("Suva_mod_pr.csv",(/12,2/),"float")
>> p4 = asciiread("Nadi_mod_pr.csv",(/12,2/),"float")
>> t1 = asciiread("Station_Suva_tm.csv",(/12,1/),"float")
>> t2 = asciiread("Station_Nadi_tm.csv",(/12,1/),"float")
>> t3 = asciiread("Suva_mod_tm.csv",(/12,2/),"float")
>> t4 = asciiread("Nadi_mod_tm.csv",(/12,2/),"float")
>>
>> lp1 = p3(:,0)
>> lp2 = p3(:,1)
>> lp3 = p4(:,0)
>> lp4 = p4(:,1)
>>
>> lt1 = t3(:,0)
>> lt2 = t3(:,1)
>> lt3 = t4(:,0)
>> lt4 = t4(:,1)
>>
>>
>> ; ***********************************************
>> ; create new date array for use on the plot
>> ; ***********************************************
>>
>>   date  = ispan(0,11,1) + 1
>>   print(date)
>>   monthnames = (/"Jan", "Feb", "Mar", "Apr" \
>>             ,"May", "Jun", "Jul", "Aug"         \
>>             ,"Sep", "Oct", "Nov"      \
>>             ,"Dec" /)
>>  ;**********************************************************
>> ; to plot multiple lines (shading), you must put them into
>> ; a mulidimensional array
>> ;**********************************************************
>>
>> data = new((/2,12/),float)
>> data(0,:) = p3(:,0)
>> data(1,:) = p3(:,1)
>>
>> data1 = new((/2,12/),float)
>> data1(0,:) = p4(:,0)
>> data1(1,:) = p4(:,1)
>>
>> data2 = new((/2,12/),float)
>> data2(0,:) = t3(:,0)
>> data2(1,:) = t3(:,1)
>>
>> data3 = new((/2,12/),float)
>> data3(0,:) = t4(:,0)
>> data3(1,:) = t4(:,1)
>>
>> ;***************************
>> ; plot parameters
>> ;***************************
>>
>> wks = gsn_open_wks("eps","Suva_Nadi_cmp") ; Open an X11 workstation.
>>
>> resL = True
>> resR = True
>>
>> resL at gsnXYBarChart         = True            ; create bar chart
>> resL at gsnDraw            = False               ; don't draw
>> resL at gsnFrame = False                        ; don't advance frame yet
>>
>> ;Change aspect ratio of plot
>>  resL at vpHeightF= 0.38
>>  resL at vpWidthF = 0.7
>>
>> ; Set min/max values for y axes
>>
>>  resL at trYMinF = 0
>>  resL at trYMaxF = 600
>>  resR at trYMinF = 16
>>  resR at trYMaxF = 30
>>
>>  resL at vpXF     = 0.15
>>
>> ; Define new markers
>>
>>   mstring = "y"
>>   fontnum = 35
>>   xoffset = 0.0
>>   yoffset = 0.0
>>   ratio   = 1.0
>>   size    = 1.0
>>   angle   = 0.0
>>
>>   Pr_index = NhlNewMarker(wks, mstring, fontnum, xoffset, yoffset, ratio,
>> size, angle)
>>
>>   mstring1 = "u"
>>   fontnum1 = 34
>>   xoffset1 = 0.0
>>   yoffset1 = 0.0
>>   ratio1   = 1.0
>>   size1    = 1.0
>>   angle1   = 0.0
>>
>>   Tm_index = NhlNewMarker(wks, mstring1, fontnum1, xoffset1, yoffset1,
>> ratio1, size1, angle1)
>>
>> ; resources for "left" variable
>>   resL at xyLineColor            = "-1"
>>   resL at xyLineThicknesses      = 3.
>>   resL at tiYAxisString = "Precipitation (mm)"
>>   resL at tiYAxisFontHeightF = 0.0198
>>
>> ; resources for "right" variable
>>   resR at xyLineColor            = "limegreen"
>>   resR at xyLineThicknesses      = 3.
>>   resR at tiYAxisString = "Temperature (~S~o~N~C)"
>>   resR at tiYAxisFontHeightF = 0.0198
>>
>>   resL at tiMainString    = "Suva and Nadi Climatologies~C~Precipitation
>> and Temperature"
>>   resL at tiMainOffsetYF  = 0.01
>>   resL at tiMainFontHeightF = 0.0240
>>   resL at tiMainFontThicknessF = 3.0
>>
>> ; Marker resources
>>   mres = True
>>   mres at gsMarkerIndex = Tm_index
>>   mres at gsMarkerSizeF = 18.0
>>   mres at gsMarkerColor = (/"limegreen"/)
>>
>>   mres1 = True
>>   mres1 at gsMarkerIndex = Tm_index
>>   mres1 at gsMarkerSizeF = 18.0
>>   mres1 at gsMarkerColor = (/"tomato2"/)
>>
>>   mres2 = True
>>   mres2 at gsMarkerIndex = Pr_index
>>   mres2 at gsMarkerSizeF = 10.0
>>   mres2 at gsMarkerColor = (/"limegreen"/)
>>
>>   mres3 = True
>>   mres3 at gsMarkerIndex = Pr_index
>>   mres3 at gsMarkerSizeF = 10.0
>>   mres3 at gsMarkerColor = (/"tomato2"/)
>>
>> ; Polyline resources
>>
>>   pres                  = True
>>   pres at gsLineThicknessF = 3.0
>>   pres at gsLineColor = "limegreen"
>>
>>   pres1                  = True
>>   pres1 at gsLineThicknessF = 3.0
>>   pres1 at gsLineColor = "tomato2"
>>
>>  ;Formatting axis labels
>>
>>   resR at tmYRFormat = "f"                           ; remove decimal
>> values from right Y-axis label
>>
>>   resR at tmXBMode        = "Explicit"
>>   resR at tmXBValues      = (/16,18,20,22,24,26,28,30/)
>>
>>   resL at tmXBMode          = "Explicit"              ; explicit labels
>>   resL at tmXBValues        = date                    ; location of labels
>>   resL at tmXBLabels        = monthnames              ; labels themselves
>>
>>   resL at gsnYRefLine           = 0.0               ; create a reference
>> line
>>
>>
>>   plot = gsn_csm_xy2(wks,date,p1(:,0),t1(:,0),resL,resR)
>>
>>   gsn_polymarker(wks,plot at xy2,date,t1(:,0),mres)
>>   gsn_polyline(wks,plot at xy2,date,t2(:,0),pres1)
>>   gsn_polymarker(wks,plot at xy2,date,t2(:,0),mres1)
>>   gsn_polyline(wks,plot,date,p1(:,0),pres)
>>   gsn_polymarker(wks,plot,date,p1(:,0),mres2)
>>   gsn_polyline(wks,plot,date,p2(:,0),pres1)
>>   gsn_polymarker(wks,plot,date,p2(:,0),mres3)
>>
>> ; Create a plot with the area between min and max simulations filled in
>> pink/blue
>>
>>  print(data)
>>  print(data1)
>>  print(data2)
>>  print(data3)
>>
>>  ; 1st shading
>>   delete(resL at xyLineColors)
>>   resL at gsnXYFillColors = "lightpink"
>>   resL at xyLineColor = "-1"
>>   plot1  = gsn_csm_xy(wks,date,data,resL)
>>
>>  ; 2nd shading
>>  resL at gsnXYFillColors = "lightskyblue"
>>  resL at xyLineColor = "-1"
>>  plot2  = gsn_csm_xy(wks,date,data1,resL)
>>
>>  ; 3rd shading
>>  delete(resR at xyLineColors)
>>  resR at gsnXYFillColors = "lightpink"
>>  resR at xyLineColor = "-1"
>>  plot3  = gsn_csm_xy(wks,date,data2,resR)
>>
>>  ; 4th shading
>>  resR at gsnXYFillColors = "lightskyblue"
>>  resR at xyLineColor = "-1"
>>  plot4  = gsn_csm_xy(wks,date,data3,resR)
>>
>>  overlay(plot,plot1)
>>  overlay(plot,plot2)
>>  overlay(plot,plot3)
>>  overlay(plot,plot4)
>>
>>  ;***********************
>>  ; Manually add legend
>>  ;***********************
>>
>>  ; Polylines and polymarkers
>>
>>   gsres             = True
>>   gsres at gsLineColor = "limegreen"
>>   gsres at gsLineThicknessF = 3.0
>>   gsres at gsMarkerIndex = Pr_index
>>   gsres at gsMarkerColor = "limegreen"
>>   gsres at gsMarkerSizeF = 10
>>
>>   gsn_polyline_ndc(wks,(/0.2,0.3/),(/0.31,0.31/),gsres)
>>   gsn_polymarker_ndc(wks,0.25,0.31,gsres)
>>
>>   gsres1             = True
>>   gsres1 at gsLineColor = "limegreen"
>>   gsres1 at gsLineThicknessF = 3.0
>>   gsres1 at gsMarkerIndex = Tm_index
>>   gsres1 at gsMarkerColor = "limegreen"
>>   gsres1 at gsMarkerSizeF = 18
>>
>>   gsn_polyline_ndc(wks,(/0.2,0.3/),(/0.28,0.28/),gsres1)
>>   gsn_polymarker_ndc(wks,0.25,0.28,gsres1)
>>
>>   gsres2             = True
>>   gsres2 at gsLineColor = "tomato2"
>>   gsres2 at gsLineThicknessF = 3.0
>>   gsres2 at gsMarkerIndex = Pr_index
>>   gsres2 at gsMarkerColor = "tomato2"
>>   gsres2 at gsMarkerSizeF = 10
>>
>>   gsn_polyline_ndc(wks,(/0.55,0.65/),(/0.31,0.31/),gsres2)
>>   gsn_polymarker_ndc(wks,0.60,0.31,gsres2)
>>
>>   gsres3             = True
>>   gsres3 at gsLineColor = "tomato2"
>>   gsres3 at gsLineThicknessF = 3.0
>>   gsres3 at gsMarkerIndex = Tm_index
>>   gsres3 at gsMarkerColor = "tomato2"
>>   gsres3 at gsMarkerSizeF = 18
>>
>>   gsn_polyline_ndc(wks,(/0.55,0.65/),(/0.28,0.28/),gsres3)
>>   gsn_polymarker_ndc(wks,0.60,0.28,gsres3)
>>
>>  ; Text
>>
>>   txres = True
>>   txres at txFontHeightF = 0.017
>>
>>   gsn_text_ndc(wks,"Suva precipitation",0.41,0.31,txres)
>>   gsn_text_ndc(wks,"Suva temperature",0.41,0.28,txres)
>>   gsn_text_ndc(wks,"Nadi precipitation",0.76,0.31,txres)
>>   gsn_text_ndc(wks,"Nadi temperature",0.76,0.28,txres)
>>
>>   draw(plot)                                     ; now draw
>>   frame(wks)                                   ; now advance frame
>>
>> ;end
>>
>>
>> However, when I add the highlighted part to my script, I get "fill_bw_xy:
>> Error: If filling between two curves, one set must be 2D, and the other
>> 1D". Also, the plot generated looks very strange - completely destroys my
>> existing plot. Perhaps I am making some trivial mistake as I am using
>> overlay and gsnXYFillColors with 2Y axes for the first time.
>>
>> Any help would be highly appreciated.
>>
>> Attached are:
>> 1. Working plot, without gsnXYFillColors - Suva_Nadi_cmp.eps
>> 2. Plot after adding the highlighted bit - Suva_Nadi_cmp2.eps
>> 3. One of the 4 data files used in gsnXYFillColors, just incase it's
>> needed - Suva_mod_pr.csv
>>
>> Thanks,
>> Vandhna.
>>
>> _______________________________________________
>> ncl-talk mailing list
>> 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/20150203/d4be6938/attachment.html 


More information about the ncl-talk mailing list