       Re: Double axes and gridlines in 2D-plots

• To: mathgroup at smc.vnet.net
• Subject: [mg7656] Re: [mg7577] Double axes and gridlines in 2D-plots
• From: "caffa" <caffa at iabg.de>
• Date: Thu, 26 Jun 1997 01:36:43 -0400 (EDT)
• Organization: IABG
• Sender: owner-wri-mathgroup at wolfram.com

```Andreas Siemer wrote:

> 1.) I want to plot two quantities in one x-y-plot with two y-axes
>     (left and right). How can I do that?
> 2.) Is there an easy way to invoke a nice arrangement of major and
>     minor grid lines in a 2-D-plot?

Part 1

Let us call z-axis the right y-axis. First of all we define the
transformation y ---> z and viceversa. E.g.:

In:=
ztoy[z_]:=2/5 z+20
In:=
ytoz[y_]:=5/2y-50

This gives:

In:=
Table[{ztoy[z],z},{z,-100,0,50}]//TableForm
Out//TableForm=
-20   -100
0     -50
20     0
In:=
Table[{y,ytoz[y]},{y,-20,20,20}]//TableForm
Out//TableForm=
-20   -100
0     -50
20     0

We are going to use Plot with the option FrameTicks. We need either a
list with elements in the form {position, label} or a function which
generates such a list. We choose the second possibility. A nice tick
position list can be generated with a library function:

In:=
Needs["Graphics`Graphics`"]
In:=
?LinearScale
"LinearScale[xmin, xmax] gives a list of \"nice\" values
between xmin and xmax suitable for use as tick mark positions.
LinearScale[xmin, xmax, n] attempts to find n such values."
In:=
LinearScale[100,200]
Out=
{{100.,100},{120.,120},{140.,140},{160.,160},{180.,180},{200.,2
00}}

The position should be transformed in y-coordinates, and we like to
have the label colored:

In:=
zcolor=RGBColor[1,0,0];
In:=
tickspec[{position_,label_}]:=
{ztoy[position],StyleForm[label,FontColor->zcolor]}
In:=
zmarks[ymin_,ymax_]:=
Map[tickspec,LinearScale[ytoz[ymin],ytoz[ymax]]]

Example

In:=
zmarks[-20,20]
Out=
{{-20.,-100},{-12.,-80},{-4.,-60},{4.,-40},{12.,-20},{20.,0}}

Let us now define to functions to be plotted  respectively according
to the y- and the z-scale:

In:=
yfunc[x_]:=2x-20
In:=
zfunc[x_]:=-20-3 x

Here is the result

In:=
Plot[{yfunc[x],ztoy[zfunc[x]]},{x,0,20},
Axes->None,Frame->True,
FrameTicks->{Automatic,Automatic,None,zmarks},
PlotStyle->{{},zcolor}]
Out:=
- Graphics -

Part 2

We need a function which generates a list of element in the form
{coordinate, style}:

In:=
gridfunc[step_,nsub_,style1_,style2_][min_,max_]:=
Flatten[
Table[  {t+i step/nsub,If[i==0,{style1},{style2}]},
{t,min-Mod[min,step],max,step},{i,0,nsub-1}],1]

Example

In:=
gridfunc[1,4,dum1,dum2][0.,2.]
Out=
{{0.,{dum1}},{0.25,{dum2}},{0.5,{dum2}},{0.75,{dum2}},
{1.,{dum1}},{1.25,{dum2}},{1.5,{dum2}},{1.75,{dum2}},
{2.,{dum1}},{2.25,{dum2}},{2.5,{dum2}},{2.75,{dum2}}}

Here is the result we seek

In:=
Plot[Sin[x],{x,0,2},
Frame->True,
GridLines->
{gridfunc[0.4,4,Thickness[0.01],Thickness[0.005]],
gridfunc[0.3,3,Thickness[0.01],Thickness[0.005]]}]
Out=
- Graphics -

I hope it helps.

-----------------------------------------------------------
Dr. Vittorio G. Caffa            Tel.:   (089) 6088-2054
IABG, Abt. CC22                  Fax:    (089) 6088-2121
Einsteinstr. 20                  E-Mail:   caffa at iabg.de
D-85521 Ottobrunn

```

• Prev by Date: Re Need a new type of Hold
• Next by Date: ThinkQuest Calculus Application on the web
• Previous by thread: Double axes and gridlines in 2D-plots