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[1]:=
ztoy[z_]:=2/5 z+20
In[2]:=
ytoz[y_]:=5/2y-50
This gives:
In[3]:=
Table[{ztoy[z],z},{z,-100,0,50}]//TableForm
Out[3]//TableForm=
-20 -100
0 -50
20 0
In[4]:=
Table[{y,ytoz[y]},{y,-20,20,20}]//TableForm
Out[4]//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[5]:=
Needs["Graphics`Graphics`"]
In[6]:=
?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[7]:=
LinearScale[100,200]
Out[7]=
{{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[8]:=
zcolor=RGBColor[1,0,0];
In[9]:=
tickspec[{position_,label_}]:=
{ztoy[position],StyleForm[label,FontColor->zcolor]}
In[10]:=
zmarks[ymin_,ymax_]:=
Map[tickspec,LinearScale[ytoz[ymin],ytoz[ymax]]]
Example
In[11]:=
zmarks[-20,20]
Out[11]=
{{-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[12]:=
yfunc[x_]:=2x-20
In[13]:=
zfunc[x_]:=-20-3 x
Here is the result
In[14]:=
Plot[{yfunc[x],ztoy[zfunc[x]]},{x,0,20},
Axes->None,Frame->True,
FrameTicks->{Automatic,Automatic,None,zmarks},
PlotStyle->{{},zcolor}]
Out[14]:=
- Graphics -
Part 2
We need a function which generates a list of element in the form
{coordinate, style}:
In[15]:=
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[16]:=
gridfunc[1,4,dum1,dum2][0.,2.]
Out[16]=
{{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[17]:=
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[17]=
- 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