Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

IDL Code for Stick Plots

  1. Jun 11, 2009 #1


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Today I was trying to figure out how to create a stick plot using IDL. I'm talking about a plot in which each data point is connected to the x-axis by a vertical line. It is often used to visualize time sequences and other discrete data in engineering. I've also heard it called a stem plot, but a Google search for that revealed mostly stem and leaf plots (which are something different entirely). I know that it is possible to create a stick plot in MATLAB, but I could not find any IDL procedure to do so. I decided to write my own procedure, thinking that it would be simple. Examples of the output from my code are attached below, along with the code itself.

    For those who know IDL but don't want to read the entire code, what I did was basically use the PLOT command to plot the data points, and then use the PLOTS command to draw a vertical line connected to each one (with a FOR loop, unfortunately).

    I'm wondering:

    1. Is the an existing IDL procedure to do this (which would be embarrassing)?
    2. If not, is there a better way of doing it than what I have come up with?

    http://img30.imageshack.us/img30/6823/gaussian.png [Broken]
    http://img30.imageshack.us/img30/1515/logxzb.png [Broken]
    http://img26.imageshack.us/img26/1610/sine.png [Broken]

    Code (Text):

    ; NAME:           STICKPLOT

    ; PURPOSE:        This procedure creates IDL plots of data in which
    ;                 each data point is connected to the x-axis by a
    ;                 vertical stick. This type of plot is often used in
    ;                 the context of engineering to visualize time
    ;                 sequences or other discrete data. It is entirely
    ;                 possible that a procedure to do this already exists
    ;                 in IDL, making this a wasted endeavour.


    ; LAST REVISED:   June 11, 2009

    ; The Call to the Procedure:

    ; STICKPLOT, x, y [, xrange = [minvalue, maxvalue]] [, yrange =
    ; [minvalue, max value] [, psym=value] [, xtitle = 'string'] [,
    ; ytitle='string'] [, title = 'string']

    PRO STICKPLOT, x, y, xrange = xr, yrange = yr, psym = psym,$
      xtitle = xtitle, ytitle = ytitle, title = title

    ; Create a custom symbol for marking data points (a filled circle)

      A = findgen(17)*(!PI*2/16)

      USERSYM, cos(A),sin(A),/fill

    ; If the user does not specify a symbol for the data points, default
    ; to the filled circles:

      if ~keyword_set(psym) then psym = 8
    ; If the user does not specify a range for the x values, set it to the
    ; full range of x values:

      if ~keyword_set(xr) then xr = [min(x), max(x)]

    ; If the user does not specify a range for the y values, set the
    ; lower limit to the smallest in the array, OR zero (whichever is
    ; less), and set the upper limit to the largest y value in the array

      if ~keyword_set(yr) then yr = [0<min(y), max(y)]

    ; Plot the data points:

      plot, x, y, xr = xr, yr = yr,  /xs, /ys, psym=psym, xtitle = xtitle,$
      ytitle = ytitle, title = title
    ; Create a "clipped" version of the y array so that the vertical lines
    ; that are to be drawn will not exceed the limits of the plot's y range

      lim = y < yr[1] > (yr[0])

    ; Find out the y limit that is smallest (in absolute value) so that
    ; the sticks can start from here (if required)

      absyr = abs(yr)

      sorted = sort(absyr)

    ; If there is a sign change in the y plot limits, then start all of
    ; the sticks at zero. Otherwise, start the sticks at the top or the
    ; bottom of the plot (as the case may be). In either case, use a for
    ; loop to draw n sticks, one for each value:

      if (yr[0]*yr[1] gt 0) then for i = min(where(x ge xr[0])),$
      max(where(x le xr[1])) do plots, [x[i],x[i]], [yr[sorted[0]],lim[i]]$
      else for i = min(where(x ge xr[0])),max(where(x le xr[1])) do plots,$
      [x[i],x[i]], [0,lim[i]]


    Last edited by a moderator: May 4, 2017
  2. jcsd
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?
Draft saved Draft deleted