[ncl-talk] Looking to create a scatterplot of x vs y colored based on value of z
Tyler Herrington
therring at uwaterloo.ca
Thu Jun 7 10:58:20 MDT 2018
Hi Dennis, Mary,
Thanks for the advice :)
I think I finally got it to work now!
Cheers,
Tyler
On Thu, Jun 7, 2018, 12:53 PM Dennis Shea <shea at ucar.edu> wrote:
> The attached is similar to Mary's example. It also shows how to add a
> label bar with text.
> ====
> Key features
> ====
> [snip]
> catTypes = (/ "invalid"
> \ ; 0
> , "marine" , "p. marine" , "dust" , "dust
> mixture" \ ; 1,2,3,4
> , "clean/bg", "p. continental", "smoke",
> "volcanic" /) ; 5,6,7,8
> nCatTypes =
> dimsizes(catTypes) ;
> nCatTypes=9
>
> catColors = (/ "gray90"
> \ ; 0
> , "blue", "aquamarine", "yellow", "orange"
> \ ; 1,2,3,4
> , "green", "red", "black" , "brown"
> /) ; 5,6,7,8
> nCatColors =
> dimsizes(catColors) ;
> nCatColors=9
> [snip]
>
>
> *map = gsn_csm_map(wks, res) ; BLANK map*
> ;---http://www.ncl.ucar.edu/Document/Graphics/Images/markers.png
>
> polyres = True
> polyres at gsMarkerIndex = 1 ; period
> polyres at gsMarkerSizeF = 0.05
>
> ;---For each of the aerosol categories add the appropriate colored marker
> ;---Must check for a category where no values were encountered (=>
> _FillValue)
>
> object = new(nCatTypes,"graphic") ; graphic object place
> holder
> do k=0,nCatTypes-1
> *ji := ind(data.eq.k) ; indices will
> change each iteration*
> nji = dimsizes(ji)
> if (nji.gt.1 .or. .not.ismissing(ji)) then
> polyres at gsMarkerColor = catColors(k)
> object(k) =
> gsn_add_polymarker(wks,map,longitude(ji),latitude(ji),polyres)
> end if
> end do
> draw(map)
>
> ;---Explicitly create and locate label bar based on 'map' ViewPort
> information
>
> [ see script]
>
> On Thu, Jun 7, 2018 at 8:51 AM, Mary Haley <haley at ucar.edu> wrote:
>
>> Hi Tyler,
>>
>> You unfortunately can't use xyMarkerColor in this way, where you give it
>> a bunch of markers and then an array of colors to apply to each marker.
>> The XY plotting in NCL wants to apply the same color to a set of markers.
>>
>> One way you can do this is to create a blank plot with the correct axes
>> limits, and then use gsn_polymarker. I did this in a hurry because I have a
>> meeting to go to, but see the attached.
>>
>> You may also want to visit this page for some more examples (search for
>> "marker"):
>>
>> http://www.ncl.ucar.edu/Applications/polyg.shtml
>>
>> --Mary
>>
>>
>> On Wed, Jun 6, 2018 at 4:11 PM, Tyler Herrington <therring at uwaterloo.ca>
>> wrote:
>>
>>> Good Afternoon,
>>>
>>> I am looking to create a scatterplot of Change in Tree Fraction vs
>>> Change in Grass Fraction where the scatterplot values are colored based on
>>> their change in albedo value.
>>>
>>> Each of Tree, Grass and Albedo have been converted into 1D arrays with
>>> 6480 values
>>>
>>> Albedo values vary between -0.415 and +0.153, and I am looking to create
>>> a colormap with 19 colors from the GMT_polar colormap.
>>>
>>> My script works ok for cases where z = ind() returns a single value
>>> (which is the case when i = 0), however it fails if z returns 2 or more
>>> values (for example when i=1), and thus I was hoping to put this out to see
>>> if anyone had any suggestions on how to fix this problem?
>>>
>>> begin
>>>
>>> ;********************************************
>>> ; Read in Tundra and Boreal Albedo Feedbacks
>>> ;********************************************
>>>
>>> dirA = "/praid/users/herringtont/Caldwell_2016/GFDL_Albedo/albedo/"
>>> dirV = "/praid/users/herringtont/Caldwell_2016/Vegetation_Data/"
>>>
>>> A1 =
>>> dirA+"albedo_Amon_GFDL-ESM2G_rcp45_r1i1p1_208001-209912_avg_diff_LandOnly_NH.nc"
>>> T1 =
>>> dirV+"treeFrac_Lmon_GFDL-ESM2G_rcp45_r1i1p1_diff_Frac_LandOnly_NH.nc"
>>> G1 =
>>> dirV+"grassFrac_Lmon_GFDL-ESM2G_rcp45_r1i1p1_diff_Frac_LandOnly_NH.nc"
>>>
>>> A2 = addfile(A1,"r")
>>> T2 = addfile(T1,"r")
>>> G2 = addfile(G1,"r")
>>>
>>> ALB = A2 ->rsuscs(4,:,:)
>>> Grass = G2 ->grassFrac(4,:,:)
>>> Tree = T2 ->treeFrac(4,:,:)
>>>
>>> ;********************************************
>>> ; Reshape ALB, Tree, and Grass from 2D to 1D
>>> ;********************************************
>>>
>>> ALB1d = ndtooned(ALB)
>>> Tree1d = ndtooned(Tree)
>>> Grass1d = ndtooned(Grass)
>>>
>>> ;************************************
>>> ; Create Color Scale for Scatterplot
>>> ;************************************
>>>
>>> dALB = dimsizes(ALB1d)
>>> colorALB = new(dALB,"integer")
>>> clrs = (/2,3,4,5,6,7,8,9,10,12,13,14/)
>>>
>>> i = 0
>>> do while (i.le.10)
>>>
>>> j = i-1
>>>
>>> if (i.eq.0) then
>>>
>>> VAL1 = -0.45
>>> VAL2 = -0.40
>>>
>>> else
>>> VAL1 = -0.35+(0.05*j)
>>> VAL2 = -0.30+(0.05*j)
>>> end if
>>>
>>> print(VAL1)
>>> print(VAL2)
>>>
>>> z = ind(ALB1d.gt.VAL1.and.ALB1d.lt.VAL2)
>>>
>>> print(z)
>>> print(i)
>>>
>>> colorALB(z) = clrs(i)
>>>
>>> i = i+1
>>>
>>> end do
>>>
>>> print(colorALB)
>>> ;******************************************************
>>> ; Create April Mean Veg vs Alb Change Scatterplot
>>> ;******************************************************
>>>
>>> wks = gsn_open_wks("png","GFDL_Veg_Alb_Apr_Scatter_Color")
>>>
>>> gsn_define_colormap(wks,"GMT_polar")
>>>
>>> res = True
>>> res at gsnDraw = False ; Don't draw plot or advance
>>> res at gsnFrame = False ; frame. Will do this later.
>>>
>>> res at tiXAxisString = "Change in Tree Fraction"
>>> res at tiYAxisString = "Change in Grass Fraction"
>>>
>>>
>>> square = NhlNewMarker(wks, "y", 35, 0.0, 0.0, 1., 0.5, 0.)
>>> res at xyMarkLineMode = "Markers"
>>> res at xyMarkerThicknessF = 2.5
>>> res at xyMarkerColor = colorALB
>>> res at xyMarker = square ; this is a filled square
>>>
>>>
>>> plot = gsn_csm_xy(wks,Tree1d,Grass1d,res)
>>>
>>> maximize_output(wks,False)
>>>
>>> system("mv *.png /home/herringtont/Caldwell_2016/figures/scatterplot")
>>>
>>>
>>> end
>>>
>>>
>>>
>>> here is the printout from the script when I execute it currently:
>>>
>>> Variable: VAL1
>>> Type: float
>>> Total Size: 4 bytes
>>> 1 values
>>> Number of Dimensions: 1
>>> Dimensions and sizes: [1]
>>> Coordinates:
>>> (0) -0.45
>>>
>>>
>>> Variable: VAL2
>>> Type: float
>>> Total Size: 4 bytes
>>> 1 values
>>> Number of Dimensions: 1
>>> Dimensions and sizes: [1]
>>> Coordinates:
>>> (0) -0.4
>>>
>>>
>>> Variable: z
>>> Type: integer
>>> Total Size: 4 bytes
>>> 1 values
>>> Number of Dimensions: 1
>>> Dimensions and sizes: [1]
>>> Coordinates:
>>> (0) 3265
>>>
>>>
>>> Variable: i
>>> Type: integer
>>> Total Size: 4 bytes
>>> 1 values
>>> Number of Dimensions: 1
>>> Dimensions and sizes: [1]
>>> Coordinates:
>>> (0) 0
>>>
>>>
>>> Variable: VAL1
>>> Type: float
>>> Total Size: 4 bytes
>>> 1 values
>>> Number of Dimensions: 1
>>> Dimensions and sizes: [1]
>>> Coordinates:
>>> (0) -0.35
>>>
>>>
>>> Variable: VAL2
>>> Type: float
>>> Total Size: 4 bytes
>>> 1 values
>>> Number of Dimensions: 1
>>> Dimensions and sizes: [1]
>>> Coordinates:
>>> (0) -0.3
>>> fatal:Number of dimensions on right hand side do not match number of
>>> dimension in left hand side
>>> fatal:["Execute.c":8640]:Execute: Error occurred at or near line 61 in
>>> file Albedo_Veg_GFDL_Scatter.ncl
>>>
>>>
>>>
>>> Thank you for your help,
>>>
>>> Sincerely,
>>>
>>> Tyler Herrington, MSc
>>> PhD Student (Climate Science)
>>> Geography and Environmental Management
>>> University of Waterloo
>>>
>>>
>>> _______________________________________________
>>> ncl-talk mailing list
>>> ncl-talk at ucar.edu
>>> List instructions, subscriber options, unsubscribe:
>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>
>>>
>>
>> _______________________________________________
>> 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/20180607/7c729939/attachment.html>
More information about the ncl-talk
mailing list