Adding an Additional Axis to a Plot

QUESTION: What is the best way to add a second, logarithmic axis to my line plot?

ANSWER: An additional axis can be added to a plot with the AXIS command. Be sure you set the SAVE keyword when you use the command, so that the subsequent overplotting of data will use the proper axis scaling.

Here is IDL code that plots a damped sine curve on a normal, linear Y axis and logarithmic data on a second, logarithmic axis. Click here to download a more complete code example.

      ; Create the data.

   x = FINDGEN(100)
   theta = x/5
   curve = SIN(x/5) / EXP(x/50)

      ; Plot the damped sine curve. Suppress drawing the right-hand Y axis.

   PLOT, curve, YSTYLE=8

      ; Draw a second, logarithmic axis on the right-hand side of the plot.

   AXIS, YAXIS=1, YLOG=1, YRANGE=[0.1, 100], /SAVE

      ; Overplot the logarithmic data.

   OPLOT, theta

Here is an illustration of the plot produced by the IDL code above.

A plot with a second, logarithmic Y axis. (4K)

[Return to IDL Programming Tips]