[ncl-talk] xy-plot with markers (depending on a 3. variable)

Karin Meier-Fleischer meier-fleischer at dkrz.de
Wed Jul 15 08:04:28 MDT 2015


Hallo Anna Lena,

I think the script below does what you want. It is your script just modified to add 
polymarkers and a labelbar.

;--------------------------------------------------------------------------------------
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
;----------------------------------------------------------------------
; Function to attach a labelbar outside of an XY plot
;----------------------------------------------------------------------
function attach_labelbar(wks,plot,labels,colors)
local lbres, vph, vpw, nboxes
begin
  nboxes = dimsizes(labels)
  getvalues plot                      ; Get plot size for use in
    "vpHeightF" : vph                 ; creating labelbar.
    "vpWidthF"  : vpw
  end getvalues

  lbres                    = True          ; labelbar only resources
  lbres at lbAutoManage       = False          ; Necessary to control sizes
  lbres at lbPerimOn          = False
  lbres at vpWidthF           = 0.2 * vpw     ; labelbar width
  lbres at vpHeightF          = vph           ; labelbar height
  lbres at lbFillColors       = colors
  lbres at lbMonoFillPattern  = True          ; Solid fill pattern
  lbres at lbLabelFontHeightF = 0.02          ; font height. default is small
  lbres at lbLabelJust        = "CenterLeft"  ; left justify labels
  lbres at lbBoxLinesOn       = False

  lbid = gsn_create_labelbar(wks,nboxes,labels,lbres)
                             
  amres                = True
  amres at amParallelPosF = 0.61     ; Move away from plot
  annoid = gsn_add_annotation(plot,lbid,amres)

  return(annoid)
end

;------------
;-- MAIN
;------------

begin
;************************************************
; read in data
;************************************************

  filename1 = "Agent3.txt"

  data1 = asciiread(filename1,(/100,3/),"float")

;************************************************
; to plot multiple lines, you must put them into
; a mulidimensional array
;************************************************

   x_1     = new((/100/),float,0)
   y_1     = new((/100/),float,0)
   t       = new((/100/),float)

;;************************************************
;;Deklaration des Datenfelds
;;***********************************************

  x_1 = data1(:,1)
  y_1 = data1(:,2)
  t   = data1(:,0)

  ndimt = dimsizes(t)
  
  num_distinct_markers = dimsizes(t)       ; number of distinct markers

;;************************************************
;;Ressourcen
;;************************************************

  wks   = gsn_open_wks ("pdf","Agent_colors")                  ; open ps file

;Colormanagement
  gsn_define_colormap(wks,"BkBlAqGrYeOrReViWh200")
  getvalues wks
         "wkColorMapLen" : cmaplen
  end getvalues
  col_step  = toint((cmaplen)/(num_distinct_markers))

  markerColors = new((/num_distinct_markers/),integer)

  do i = 0, num_distinct_markers-1
        print(t(i))
        markerColors(i)= toint( t(i)*col_step)
  end do
  print(markerColors)

  res                        = True                       ; plot mods desired
  res at gsnDraw           = False                 ; don't draw
  res at gsnFrame          = False

;Ränder des Gitters festlegen    
  res at trXMinF             = 0
  res at trXMaxF             = 50
  res at trYMinF             = 0
  res at trYMaxF             = 50

;Gitternetz
  res at tmXMajorGrid      = True
  res at tmXMinorGrid      = True
  res at tmXMajorGridLineColor = 6
  res at tmXMinorGridLineColor = 9
  res at tmXMajorGridLineDashPattern = 1
  res at tmXMinorGridLineDashPattern = 1
  res at tmXMajorGridThicknessF = 1.5
  res at tmYMajorGrid      = True
  res at tmYMajorGridLineColor = 6
  res at tmYMajorGridLineDashPattern = 1
  res at tmYMajorGridThicknessF = 1.5

;---Set some marker resources
  res at xyMarkLineMode     = "Markers"
  res at xyMarkerThicknessF = 2.5
  res at xyMarkerColors     = markerColors
  res at xyMarker           = 17

  plot1 = gsn_csm_xy (wks,x_1,y_1,res)

;-- add the polymarkers
  pmres = True
  do i=0,dimsizes(markerColors)-1
    pmres at gsMarkerColor = markerColors(i)
    pmres at gsMarkerIndex = 17  
    pmres at gsMarkerSizeF    = 0.05
    dum = unique_string("marker")
    plot1@$dum$ = gsn_add_polymarker(wks,plot1, x_1(i), y_1(i), pmres)
  end do

;-- add labelbar
  lid = attach_labelbar(wks,plot1,markerColors+"",markerColors)

;-- draw the plot and advance the frame
  draw(plot1)
  frame(wks)
  
end
;--------------------------------------------------------------------------------------


Bye,
Karin


> Am 15.07.2015 um 12:00 schrieb Hartmut Bittner <anna at hbittnersth.de>:
> 
> Hi Rick,
> thanks a lot for your help. I try your suggestion. Now I get an markerColors array, but in the plot all markers still have the same color (black). I think the plot1 is overwritten each time.  Do you have an idea how can I reach that every marker is drawn individuel and gets a different color?
>  
> I have attached my script, my plot and my data.
>  
> Thanks for your help.
>  
> Anna
> Gesendet: Dienstag, 14. Juli 2015 um 17:23 Uhr
> Von: "Rick Brownrigg" <brownrig at ucar.edu>
> An: "Hartmut Bittner" <anna at hbittnersth.de>
> Cc: "ncl-talk at ucar.edu" <ncl-talk at ucar.edu>
> Betreff: Re: [ncl-talk] xy-plot with markers (depending on a 3. variable)
> Hi Anna,
> 
>  
> You have this as your loop code:
> 
>    do  i= 1, num_distinct_markers
>       res at xyMarkerColors = 2 + i* col_step* t(i)
>       ;print(2+i*col_step)
>       plot1  = gsn_csm_xy (wks,x_1,y_1,res)
>    end do 
>    draw(plot1)
>    frame(wks)
>  
> I'm surprised this doesn't report errors, but it is generating "num_distinct_markers"  unique *plots*, with all the markers of the i-th plot colored with the i-th color, and plot1 is overwritten each time, with the last plot prevailing. 
> 
> What I was suggesting was to create an array of colors "num_distinct_markers" long, and populate that with your marker colors, as in :
>  
>    markerColors = new((/ num_distinct_markers /), integer)
>    do i=0, num_distinct_markers - 1
>       markerColors(i) = 2 + i*col_step * t(i)
>    end do
>  
>    res at xyMarkerColors = markerColors
>    plot1 = gsn_csm_xy(wks, x_1, y_1, res)
>  
>    draw(plot1)
>    frame(wks)
>  
> This is untested, without having your data.  Also note that in your example, you're indexing over 1..num_distinct_markers, whereas I'm looping from 0..(num_distinct_markers - 1).   I'm not totally sure which is correct for your case.
>  
> Hope that helps...
> Rick
> 
>  
>  
>  
> On Tue, Jul 14, 2015 at 6:31 AM, Hartmut Bittner <anna at hbittnersth.de <x-msg://4/anna@hbittnersth.de>> wrote:
> Hi,
> I try to realize Ricks idea. But my problem is still that all my markers have the same color.
>  
> My problem in generel is: I have 1 data set that consists of a x- position ( in my  script: x), a y-position ( in my script: y) and a 3. value (for example the temperature, in my script: t). I want to creat  an x-y plot with the x/y-position and the marker should be controlled by the 3. variable t.  So that I have an 2D-Plot in which 3 variables are presented.
> For exemple the color of the marker gets red for higher t-values and gets green for lower t-values.
>  
> I made an loop over the "xyMarkersColors",but all makers have the same color. I want that the makers have different colors. I think for reaching this goal in every do loop only 1 markers has to be drawn. So that after n- loops all the n xyMarkers are drawn.  How can I reach this? And how can I realize the dependent of the markersColors of variable t? If I put t into the do loop I get an segmentation fault.
>  
>  
> Thanks for your help
>  
> Anna Lena
> Gesendet: Donnerstag, 09. Juli 2015 um 19:26 Uhr
> Von: "Rick Brownrigg" <brownrig at ucar.edu <x-msg://4/brownrig@ucar.edu>>
> An: "Anna Lena Bittner" <anna at hbittnersth.de <x-msg://4/anna@hbittnersth.de>>
> Cc: "ncl-talk at ucar.edu <x-msg://4/ncl-talk@ucar.edu>" <ncl-talk at ucar.edu <x-msg://4/ncl-talk@ucar.edu>>
> Betreff: Re: [ncl-talk] xy-plot with markers (depending on a 3. variable)
> Hi Anna,
>  
> One approach is exemplified by example #8 at:
> 
> http://ncl.ucar.edu/Applications/polyg.shtml <http://ncl.ucar.edu/Applications/polyg.shtml>
>  
> This approach draws the markers 1-by-1, looping over the marker array and setting the colors for each one based upon some criterion.
>  
> Another approach, more or less the same amount of effort, is to set up an array for the resource "xyMarkerColors" that contains a color value for each marker to be drawn. You'd need an array the same length as your xy data, and would have to loop over it to fill in each entry with a color value based on your 3rd variable.
>  
> I note that you are already setting the resource xyMarkerColors with 2 colors. According to the documentation, if you have more xy data than color-values in xyMarkerColors, the remaining markers are drawn with the color set by the resource xyMarkerColor (singular) -- the default for that resource is whatever the foreground color (typically black).  So what you should have gotten in your existing example is one green marker, one blue marker, and the remainder black (?) 
>  
> Make sense?
>  
> Rick
>  
> On Thu, Jul 9, 2015 at 10:25 AM, Anna Lena Bittner <anna at hbittnersth.de <>> wrote:
> Hi,
> 
> I would like to create a xy-plot with markers. The markers should have different colors. The colors should be controlled by a 3. variable.
> In my plot I already created the makers, but I don't know how to define the color with a 3. variable ( in my case the variable t).
> I couldn't find a description for this kind of plot. If there is a description on the homepage let me know.
> 
> I have attached my scripts here.
> 
> 
> Thanks a lot for your help.
> 
> Anna Lena
> 
> ______________________________________________




-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20150715/0cf053a2/attachment-0002.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Agent_colors.pdf
Type: application/pdf
Size: 79065 bytes
Desc: not available
Url : http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20150715/0cf053a2/attachment-0001.pdf 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20150715/0cf053a2/attachment-0003.html 


More information about the ncl-talk mailing list