(*Author: Shawn Sheridan
	Title: Gridlines
	Purpose: This notebook shows how to create custom gridlines.  *)

©Wolfram Research, Inc. 1989
This code was written by Shawn C. Sheridan under the employment of Wolfram Research, Inc.  WRI holds all copyrights.  The code may be distributed freely and may be used for any purpose other than resale.

SimpleGrid

SimpleGrid takes two iterator specifications and an optional third list of line attributes.  The iterator specifications should be in the same format used by the Table function.

SimpleGrid[xl:{x_,xmin_,xmax_,delx_:1},
	yl:{y_,ymin_,ymax_,dely_:1},
	att_List:{}]:=Graphics[Flatten[{att,
	Table[Line[{{xmin,y},{xmax,y}}],yl],
	Table[Line[{{x,ymin},{x,ymax}}],xl]}]]

thegrid=SimpleGrid[{x, -1, 3, 0.5},{y, -4, 4},
	{Thickness[.001]}]

thegraph=Plot[2x(x-1)(x-2),{x,-1,3}]

Show[thegrid,thegraph,PlotRange->{-4,4}]

The Bugs

thegrid=SimpleGrid[
	{x,-Pi,Pi,Pi/4},
	{y,-1,1,.25},
	{Dashing[{.01,.01}]}]

Show[%]

thegrid=SimpleGrid[
	{x,-Pi,Pi,Pi/4},
	{y,-1,1,.25},
	{Thickness[.01],Dashing[{.01,.01}]}]

Show[%]

FancyGrid

FancyGrid takes two lists (or an optional third) as arguments.  The first list is the set of x-gridline positions, the second list is the set of y-gridline positions, and the optional third list is a list of line attributes to be applied to the gridlines.  FancyGrid is useful if you need to specify gridlines that are not equally spaced.

FancyGrid[xlist_,ylist_,optlist_:{}]:=
	Graphics[Flatten[
		{optlist,
		Apply[Line,
			Transpose[{Outer[List,{Max[xlist]},ylist],
			Outer[List,{Min[xlist]},ylist]},{3,2,1}],{1}],
		Apply[Line,
			Transpose[{Outer[List,xlist,{Max[ylist]}],
			Outer[List,xlist,{Min[ylist]}]},{3,1,2}],{1}]
		}]]

logtable=N[Union[Flatten[
			Table[Log[10,i*10^j],{j,0,2}, {i,10}]
				]]]

thegrid=Show[ FancyGrid[
				{0, 1, 2, 3, 4, 5},
				logtable,
				{ Thickness[.0001] }]
			]

Here is a plot of 4^x on Log paper...

thegraph=Plot[4^(Log[10,x]),{x,.01,5}]

Show[thegrid,thegraph,PlotRange->All,
	Ticks->{{1,2,3,4,5},{1,2,3}}]

Show[thegrid,thegraph,PlotRange->All,
	Ticks->{{1,2,3,4,5},{{1,10^1},{2,10^2},{3, 10^3}}}]

Show[thegrid,thegraph,PlotRange->All,
	Ticks->{{1,2,3,4,5},
		{{1,HoldForm[10^1]},
		{2,HoldForm[10^2]},
		{3, HoldForm[10^3]}}}]