Hash syntax for accessing children in (New) Graphics

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.

About these ads

About Mark

I solve scientific programming and visualization problems with IDL.
This entry was posted in language, visualization and tagged , , , . Bookmark the permalink.

2 Responses to Hash syntax for accessing children in (New) Graphics

  1. Pingback: Displaying exploded axes with (New) Graphics | The IDL Data Point

  2. Pingback: Using (New) Graphics in an IDL widget program, part I | The IDL Data Point

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s