Using IDL 8 Graphics (a.k.a. New Graphics)
I’ve been using Direct Graphics (DG) in IDL since 1993. I was initially hesitant about IDL 8 Graphics (christened “New Graphics” on the comp.lang.idl-pvwave
newsgroup; I’ll refer to them as NG), but I’ve been pleasantly surprised by how well they work. In particular, I like how their syntax mirrors DG and how I can use them programmatically. I also like how they handle color and how easily they make publication-ready PostScript and PNG files (among other formats).
Here’s a short example of working with NG. If you’d like, please try these commands in IDL. Start by making variables to represent one cycle of a sine and a cosine wave:
t = findgen(361)*!dtor
x = sin(t)
y = cos(t)
Display the sine wave:
p = plot(t, x)
Note how I called PLOT
as a function. (You can still call the DG PLOT
routine as a procedure.) The PLOT function returns a reference—here, the variable “p” —that can be used to control the graphic. For example, I can use “p” to change the line color to red:
p.color = 'red'
and the graphic updates immediately. I specified the color red by name; you could also use an RGB triple. Next, I’ll add titles to the plot:
p.xtitle = 'Time'
p.ytitle = 'Amplitude'
p.title = 'Sine and Cosine Waves'
I could’ve set these properties through identical keywords in the call to PLOT.
I can display the cosine wave on the same axes as the sine wave with another call to PLOT:
q = plot(t, y, color='blue', /overplot)
keyword is the NG analog to the DG OPLOT
I’ll also display a zero line:
!null = plot(p.xrange, [0,0], linestyle='dotted', /overplot)
Note how I used “p” to get the current x-axis range of the plot.
Last, I’ll add a legend positioned in the lower left corner of the graphic. Don't include the zero line in the legend.
p.name = 'Sine'
q.name = 'Cosine'
pq_lgd = legend(target=[p,q], position=[2.0,-0.6], /data)
Here’s a screenshot of the window that results from these steps (this is from the Linux side of my laptop):
The graphic can be saved to a JPEG file with one command:
IDL uses the extension on the file name to determine the file type. The default resolution of the image is 600 dpi, which makes a 2598 x 2079 image on my laptop!
NG won’t replace DG in every instance. For example, NG are typically slower than DG. This is especially true when displaying more than about 104
points. NG may not be for everyone. If you’re already comfortable with DG, please continue to use them. But when I think back to when I was learning how to go beyond the basics in DG, I remember it wasn’t always easy. (In particular, I remember the first time my advisor asked me to print a plot. That was a frustrating day. Or two.) I hope that new users of IDL will try NG, and I hope that NG just makes sense to them.
If you’d like to learn more about using NG, please continue to follow me on this blog; I’ll post numerous examples
of using them in the coming weeks and months.