Two graphs with different y-scale in the same plot

So how can we plot two 1-D curves in the same graph if they have different y-scales? For instance, that could be two time series: convective cloudiness and convective heating rate.

What we need is to draw a second y-axis on the right side of the graph with the scale of the second plot. That's done with:

'set ylpos offset side'

Bellow you will find a script that does the job, it is fully commented so you won't have any trouble to adapt it for your needs.

* reinit grads and turns off grid lines and grads labels
'reinit'
'set grid off'
'set grads off'

'open mydata.ctl'

* set graph area to full window but 1in margin all around
'set parea 1. 10. 1. 7.5'

* set time range and fix lat/lon
'set t 1 12'
'set x 1'
'set y 1'
'set lev 500'

**** First graph ****
* no markers, solid line style, white color and thick line
'set cmark 0'; 'set cstyle 1'; 'set ccolor 1'; 'set cthick 6'

* fix y-range and y-step so that grads does what we want
'set vrange 0 7'
'set ylint 1'

* y axis is drawn white, thick and labels have size 0.20
'set ylopts 1 6 0.20'

* x axis is drawn blue, thin and labels have size 0.15
'set xlopts 4 1 0.15'
'd tloop(aave(cvlh*86400,lon=-70,lon=-50,lat=-15,lat=0))'

* draw first y label
'draw ylab Convective Heating Rate (K/day)'

**** Second graph ****
* no markers, solid line style, red color and thick line
'set cmark 0'; 'set cstyle 2'; 'set ccolor 2'; 'set cthick 6'

* IMPORTANT: must fix y-range and y-step otherwise grads will
*   try to use the same as the first plot!
'set vrange 0.0 0.05'
'set ylint 0.01'

* y axis is drawn white, thick and labels have size 0.20
'set ylopts 2 4 0.20'

* IMPORTANT: place second y-axis on the right
'set ylpos 0 r'
'd tloop(aave(cvcl,lon=-70,lon=-50,lat=-15,lat=0))'

* IMPORTANT: can't use draw ylab for 2nd label
'set strsiz 0.2'
'set string 2 c 6 -90'
'draw string 10.5 4.25 Convective Cloud [0-1]'

* Graph title
'draw title Convection at 500mb'

*end

Some remarks about the second plot:

  1. You must fix y-range both for the first and second plots. If you don't do that GrADS will try to draw the second with the same y-scale as of the first.
  2. Remember to place the second axis on the right. That's done with 'set ylpos 0 r'.
  3. Unfortunatelly you won't be able to use 'draw ylab ...' to draw the second y-axis label. You will need to do that by hand, i.e., with string primitives.

Some remarks about time series:

  1. It is necessary to fix lat/lon dimentions to plot a time series of an area average. You must also wrap the area average function with a tloop() function, otherwise GrADS will complain.

Here's the result of the above script:

GrADS pages you should read:

set ylopts
set ylpos
set ylint
set parea
tloop()
ave()

Page last modified on May 26, 2015, at 05:50 PM
Powered by PmWiki