Plotting with iPlot

The IDL iPlot tool displays your data in plot form. The iPlot tool then allows you great flexibility in manipulating and visualizing plot data. iPlot can be used for any type of two- or three-dimensional plot, including scatter plots, line plots, polar plots, and histogram plots.

Creating a Simple 2-D Plot

2D_plot_1.gif

To create a simple line plot in the iPlot tool, enter the following code at the IDL command line:

iPlot, RANDOMU(seed, 20)

In this case, we are using the RANDOMU function to return twenty uniformly-distributed, floating-point, pseudo-random numbers that are greater than 0, and less than 1.0.

Creating a 2-D Overplot

2D_plot_2.gif

In the iPlot tool, you may plot a new data set over a previously-drawn data set. This process (called overplotting) is useful for directly comparing multiple data sets.

In this example, we will plot a cosine wave on top of a sine wave.

  1. The variable theory stores the points of a sine wave of decreasing amplitude.
  2. theory = SIN(2.0*FINDGEN(200)*!PI/25.0)*EXP(-0.02*FINDGEN(200))

  3. Plot the sine wave in the iPlot tool.
  4. IPLOT, theory

  5. Create the variable newtheory to contain cosine wave points.
  6. newtheory=COS(2.0*FINDGEN(200)*!PI/25.0)*EXP(-0.02*FINDGEN(200))

  7. Overplot the cosine data in the iPlot tool.
    2D_plot_3.gif
  8. IPLOT, newtheory, /OVERPLOT, $
       LINESTYLE=2

    This plots the second line in the same iPlot window as the first. The LINESTYLE keyword changes the line style property of the plot to display a dashed line rather than a solid line. You can also overplot in the iPlot tool simply by loading new data over an older data set.

Plotting an ASCII Data Set

In this example, we will import an ASCII data set into IDL and plot it with the iPlot tool. Enter the following code at the IDL command line:

  1. Create an ASCII template, which defines the format of a particular ASCII file. IDL will use this template to import the data. The plotTemplate variable contains the template.
  2. plotTemplate = ASCII_TEMPLATE( )

  3. A dialog appears, prompting you to select a file. Select the plot.txt file in the examples/data subdirectory of the IDL distribution.
  4. After selecting the file, the ASCII Template dialog appears.

  5. Select the Delimited field type, since the ASCII data is delimited by tabs (or spaces).
  6. In the Data Starts at Line box, enter a value of 3. (The data does not start at line 1 because there are two comment lines at the beginning of the file.)
  7. Click Next.
  8. In the Delimiter Between Data Elements section, select Tab.
  9. Click Next.
  10. Name the ASCII file fields by selecting a row in the table at the top of the dialog and entering a value in the Name box.
    • Click on the table's first row (FIELD1). In the Name box, enter time.
    • Select the second row and enter temperature1.
    • Select the third row and enter temperature2.
  11. Click Finish.
  12. Enter the following code at the IDL command line to import the ASCII data file plot.txt using the custom template plotTemplate.
  13. plotAscii = READ_ASCII(FILEPATH('plot.txt', SUBDIRECTORY= $
       ['examples', 'data']), TEMPLATE=plotTemplate)

  14. Plot the temperature1 vs. time data.
    2D_plot_4.gif
  15. IPLOT, plotAscii.time, $
      plotAscii.temperature1

For more information on importing ASCII data, see Reading ASCII Data.

Adding Plot Titles

The iPlot tool allows you to modify your plots by adding elements such as error bars, legends, and axis titles. You can also manipulate the plot with tools such as curve fitting or filtering.

2D_plot_5.gif

In this example, we will add a main title and axis titles to the ASCII data plot we created previously. The VIEW_TITLE keyword adds a main title, and the XTITLE and YTITLE keywords add axis labels. If you have not already done so this session, do the example Plotting an ASCII Data Set.

Enter the following code at the IDL command line, which will create a new iPlot dialog and add titles to the plot.

IPLOT, plotAscii.time, plotAscii.temperature1, $
   VIEW_TITLE='Temperature Over Time', $
   XTITLE='Time in Seconds', $
   YTITLE='Temperature Celsius'

Alternately, you could add title annotations to an existing plot by selecting the Text tool tb_text.gif, positioning the cursor at the location where you want the title to appear, and typing the text. Double-clicking on the text displays the text annotation property sheet, which allows you to modify the size, font, color, and other properties of the annotation.

Changing the Data Range of a Plot

Your data set may contain more data than you want to display in a particular plot. While you could use IDL's array subscripting syntax to create a subset of the original array, it is often easier to simply limit the range used when creating the plot display.

2D_plot_8.gif

For example, suppose you wanted to restrict the range displayed in your plot to show only time values (the X-axis) between 15 and 18 seconds, and temperature values (the Y-axis) between 8 and 13 degrees. Using the [XYZ]RANGE keywords to the IPLOT routine allows you to do this when creating the plot:

IPLOT, plotAscii.time, plotAscii.temperature1, $
   XRANGE=[15,18], YRANGE=[8,13]

Alternately, you could start by displaying the full data range in iPlot, and then alter the Dataspace properties to reflect the new X and Y ranges:

2D_plot_9.gif

Using Plotting Symbols and Line Styles

2D_plot_10.gif

When plotting several data sets in a single plot, it is often useful to use symbols, line styles, and legends to differentiate between the data sets. The following procedure created the plot shown at right.

  1. First, plot the temperature1 values using the standard (solid) line style and a diamond symbol to mark the data points:
  2. IPLOT, plotAscii.time, plotAscii.temperature1, SYM_INDEX=4

    Note that we set the SYM_INDEX keyword equal to four to create the diamond symbols.

  3. Next, overplot the temperature2 values using a dashed line (LINESTYLE=2) and a triangle symbol to mark the data points (SYM_INDEX=5):
  4. IPLOT, plotAscii.time, plotAscii.temperature2, SYM_INDEX=5, $
       LINESTYLE=2, /OVERPLOT

  5. To insert a legend, click in the plot area to select it (the axis lines around the plots will be highlighted), then select New Legend from the Insert menu. The legend is created using default names for the data sets (Plot and Plot1).
  6. Double click on Plot in the legend to bring up the property sheet, and change the value in the Text field to Temperature 1. Similarly, change Plot1 to Temperature 2.

Adding Error Bars

You can add error bars to your plot using the [XYZ]ERROR keyword to IPLOT.

Suppose you know that the temperature values you have collected are only accurate within 0.3 degrees Celsius. To include error bars on your plot of temperature versus time, you would do the following:

2D_plot_11.gif

  1. Create an array with the same number of elements as you have temperature readings:
  2. error_bars = FLTARR(N_ELEMENTS(plotAscii.temperature1))+0.3

    This creates a floating-point array with the same number of elements as the plotASCII.temperature1 array, setting each element's value equal to 0.3.

     

    Note
    The size of the error bar does not need to be the same for every data point. Each element in the error_bars array could contain a different value.

  3. Use the YERROR keyword to add the error bars:
  4. IPLOT, plotAscii.time, plotAscii.temperature1, YERROR=error_bars