[Thread Prev][Thread Next][Index]

Re: [ferret_users] How to make a plot with 2 Y axis



On 8 Nov 2021 Olivier Marti contributed a useful ferret script, COPY_VIEWPORT.JNL, to answer partially Pierre Friedlingstein’s ferret user question about how to make a plot with a second y axis.  It happens that I was writing my own script to modernize the old LEFT_AXIS_PLOT.JNL and RIGHT_AXIS_PLOT.JNL scripts.  Olivier’s script came at just the right time.  

I attach my script PLOT_2_Y_AXES_COMMON_X_AXIS.JNL script.  It plots 2 different y variables, one on the left y axis and one on the right y axis, on a common x axis.  The benefit over the old scripts is that you can put the ferret plot commands, with many options, in the input left and right axis symbol command lines.  The script is below, and also an example script, separated by a line of  *******.



\cancel mode verify

! PLOT_2_Y_AXES_COMMON_X_AXIS.JNL plots 2 y values, on left and right axes, on a common x axis. 

! Programmed by E.D. Cokelet, NOAA/PMEL, Edward.D.Cokelet@xxxxxxxx, 5 Nov 2021
! Last modified 15 Nov 2021.   

! Usage:
! Define the single x (x_bottom_var) and the two y values (y_left_var and y_right_var) you wish to plot.  Set a viewport, and set up the plot instructions.

! set viewport full
! define symbol left_instructions   = plot/vs/title="Test Plot"/line/color=red/thick/hlim=-1:12/vlim=-2:150
! define symbol right_instructions = plot/vs/sym=27/color=green/thick/nolabel/vlim=-20:600
! go plot_2_y_axes_common_x_axis ($left_axis_instructions) x_bottom_var y_left_var ($right_axis_instructions) y_right_var
!                                                                            $1                           $2             $3                        $4                           $5
! Where
! $1 = ferret plot instructions for the variable on the left y axis
! $2 = the variable plotted on the lower x axis
! $3 = the variable plotted on the left  y axis
! $4 = ferret plot instructions for the variable on the right y axis
! $5 = the variable plotted on the right y axis

! This script uses the COPY_VIEWPORT.JNL script as contributed to the Ferret user's group by Olivier Marti on 8 Nov 2021 via email.  



can mode logo                                          ! Eliminates the ferret blurb at the upper right of a plot


! Set up to plot the botttom x and left y axes. 

$1/axes=(1,1,1,0)/set $2, $3
go unlabel 1                                                ! This code gets rid of the x,y,x,t,e,f and data set information at the upper left of a ferret plot.  
go unlabel 2
go unlabel 3
go unlabel 4
go unlabel 5
go unlabel 6
go unlabel 7

let p2yacx_shortening = 0.5                       ! Make plot width 0.5 inches shorter to fit the right axis and its label.  Change this if you wish.  
ppl set axov_xlen 'ppl$xlen' - `p2yacx_shortening`
ppl axlen 'axov_xlen'

ppl plot

!pause


! Capture the low and high values of the x axis in user units.
! These are needed later to prevent these limits being reset in the ($right_axis_instructions)

let p2yacx_x_low  = `($xaxis_min)`
let p2yacx_x_high = `($xaxis_max)`

go copy_viewport                                         ! Set an identical viewport on top of the original and plot the right-hand y variable on it.  

! Adding HLIM at the end of the command line below forces HLIM for this viewport to be the same as the first viewport.  
! Without this you would NOT be plotting on the common x plot axis.

$4/axes=(0,0,0,1)/HLIM=`p2yacx_x_low`:`p2yacx_x_high`/set $2, $5
go unlabel 1
go unlabel 2
go unlabel 3
go unlabel 4
go unlabel 5
go unlabel 6
go unlabel 7
ppl plot


! Put the x axis back to its default ferret length and clean up.  

ppl set axov_xlen 'ppl$xlen' + `p2yacx_shortening`            ! Add back the 0.5 inches
ppl axlen 'axov_xlen'

can variable p2yacx_*
can symbol  p2yacx_*
set mode logo
set mode/last verify



***********************************************************************


! EXAMPLE_PLOT_2_Y_AXES_COMMON_X_AXIS.JNL raws 2 plots to demonstrate PLOT_2_Y_AXES_COMMON_X_AXIS.JNL script.

! Programmed by E.D. Cokelet, NOAA/PMEL, 5 Nov 2021.
! Last modified 15 Nov 2021.   


set window 1
set view full

define axis/x=0:10:1  x_ax
let xx = x[gx=x_ax]
set variable/title="Distance"/units="km" xx ! x axis label becomes "Distance (km)"

let left_var  = 10*xx
set variable/title="Left Var"/units="km" left_var ! left y axis label becomes "Left Var (km)"

let right_var = (5*xx^2 + 10)
set variable/title="Right Var"/units="m" right_var ! right y axis label becomes "Right Var (m)"

define symbol left_instructions = plot/vs/title="Test Plot"/line/color=red/thick/hlim=-1:12/vlim=-2:150
define symbol right_instructions = plot/vs/sym=27/color=green/thick/hlim=-50:100/vlim=-20:600


go plot_2_y_axes_common_x_axis ($left_instructions) xx left_var ($right_instructions) right_var

!pause


! Make another test plot with less options set up

set window 2
set view full

let xx2 = 10*xx
let left_var2 = 10*left_var
let right_var2 = (5*xx^2 + 10) * 10^(-10)

define symbol left_instructions2 = plot/vs/line/color=black
define symbol right_instructions2 = plot/vs/sym=27/color=lightblue

go plot_2_y_axes_common_x_axis ($left_instructions2) xx2 left_var2 ($right_instructions2) right_var2




______________________________________________

Edward (Ned) D. Cokelet, Ph.D. Oceanographer
NOAA/PMEL off:  206-526-6820
7600 Sand Point Way NE fax: 206-526-6485
Seattle, WA 98115-6349

Edward.D.Cokelet@xxxxxxxx

The contents of this message are mine personally and do not necessarily reflect any position of the Government or the National Oceanic and Atmospheric Administration.





[Thread Prev][Thread Next][Index]
Contact Us
Dept of Commerce / NOAA / OAR / PMEL / Ferret

Privacy Policy | Disclaimer | Accessibility Statement