Re: OT: Determining the size of plotting area
  Home FAQ Contact Sign in
comp.lang.fortran only
 
Advanced search
POPULAR GROUPS

more...

 Up
Re: OT: Determining the size of plotting area         

Group: comp.lang.fortran · Group Profile
Author: glen herrmannsfeldt
Date: Sep 16, 2008 23:17

Robert Singer wrote:
> First, Let me describe what I'm interested in; most of you have used
> excel or some similar program; when you put an array of data in it and
> plot a graph/chart (x-y type), he will automatically determine the
> upper and lower limit of the graph.
(snip)
> Okey, the first step is to determine xmin, xmax and the same for y,
> for the given array. That is not a problem.
> As far as I know fortran has no intristics for rounding numbers, so
> that would be one part of the problem.

X and Y are separate, so just consider one. This is based on
one I have used and modified over the years:

You need Xmin, Xmax, and the desired axis length: Axlen
(number of intermediate tick marks.) Compute the value
for the low end (TL) and tick increment (TX).

real table(14)
! available scale factors, with power of 10 removed
! up to 20 allows for some roundoff that might occur
! on 10th inch graph paper, you might want /1, 2, 5, 10, 20/
data table/1.,1.2,1.5,2.,2.4,3.,4.,5.,6.,8.,10.,12.,15.,20./
F=0.01
if(Xmin<0) F=0.99 ! round in the right direction
TX=(Xmax-Xmin)/Axlen ! first approximation to scale factor
if(TX<=0) TX=abs(2*TX/Axlen)+1 ! reasonable results if no data range
P=10.**int(log10(TX)) ! largest power of 10 less than TX
TX=TX/P-0.01 ! divide by P and reduce slightly
! TX should be between about 1 and 10
DO I=1,size(table)
if(table(I).ge.TX) exit ! potential nice scale factor
ENDDO
DO I=I,size(table)
TX=table(I)*P ! restore power of 10 removed earlier
TL=TX*floor(Xmin/TX+F) ! compute new lower limit based on scale
T=TL+(Axlen+0.01)*TX ! compute upper limit from lower limit and scale
if(T.gt.Xmax) exit ! it works!
TL=P*floor(Xmin/P+F) ! it doesn't, recompute lower limit
T=TL+(Axlen+0.01)*TX ! and upper limit
if(T.gt.Xmax) exit ! and test again
ENDDO ! try the next largest scale constant
! because of the rounding, the result may be less centered than needed
TL=TL-aint((Axlen+(TL-Xmax)/TX)/2.)*TX
if(Xmin*TL.lt.0) TL=0 ! don't cross zero if the data doesn't

The loop allows successive attempts such that the lower limit
is not greater than Xmin, the upper limit is not lower than Xmax,
zero is on a tick if included or near the data limits.
The last correction is needed as the scale factor increases
may result in the graph being way off center.

-- glen
no comments
diggit! del.icio.us! reddit!

RELATED THREADS
SubjectArticles qty Group
do structure definitions go in data area or in code area...15 comp.lang.c ·