X
11406 Rate this article:
No rating

Hash syntax for accessing children in (New) Graphics

Anonym
A useful feature of (New) Graphics (NG) is the ability to access a component of a visualization by name using hash syntax. This behavior is derived from the GetByName method implemented by most container classes in Object Graphics. For example, here I have a window that holds a plot of a Bessel J function:
x = findgen(100)/5
y = beselj(x)
w = window()
p = plot(x, y, color='red', name='bessel_j0', /current)
Note that I gave the plot a name. (The default name of a plot is ‘Plot’, or if more than one plot is present in a window, ‘Plot n’, where n = 1,2,3,…; likewise, ‘Surface’ for a surface, ‘Image’ for an image, etc.) Now discard the reference to the plot (maybe, e.g., it fell out of scope):
p = !null
So I now can’t access the properties of the plot. Or can I?! Retrieve the reference to the plot, by its name, from the window using hash syntax:
q = w['bessel_j0']
and use the recovered reference to change the color of the plot:
q.color = 'green'
Neat! I use this feature of NG frequently, especially to get access to the axes of a plot, post-creation. For example, to hide the top and right axes of the plot above, try:
q['axis2'].hide = 1
q['axis3'].hide = 1
In NG, axes are numbered starting at zero with the bottom axis and increasing clockwise (the left axis is index 1, etc.). In a subsequent post, I’ll talk about the very handy GETWINDOWS function for when you don’t have a reference to the window object holding a visualization.