Photo of Dr. S. Salewski

Homepage of Stefan Salewski

Scaling axes in diagrams and data plots

When we try to write a program for plotting data or drawing diagrams, i.e. with GTK+/Cairo, a not always trivial task is to find a fine partition for the axes. In particular when the range of the data is not bound to a fixed range.

So I wrote a small function, which generates tic position and label values for arbitrary data ranges and a width number of desired tics/intervals. It is written in C language, but it should not be difficult to convert it to other programing languages. Of course, we may find a similar, maybe more sophisticated function in the famous gnuplot program.

The C source files are available in this directory for download:

This is the function header and some testing output:
/*
x1, x2: range to partition
majors, minors: DESIRED! number of intervals==(tics-1). 1 <= majors <= 50, 1 <= minors <= 10.
extend: if true, we may extend bounds with major tics, else only with subtics.
array t holds position of tics. Its size should be at least 3*majors*minors.
For major tics we may generate a string and write it at that position in diagram, for subtics we may print a |.
return: total number of generated tics.
*/
int scaleaxis(double x1, double x2, int majors, int minors, int arraysize, bool extend, Tic t[]);


stefan@AMD64X2 ~ $ cd AxisScale
stefan@AMD64X2 ~/AxisScale $ gcc -lm -Wall AxisScale.c ASTest.c 
stefan@AMD64X2 ~/AxisScale $ ./a.out 
Scaling of axis in diagrams and data plots -- some examples:
x1=-10 x2=90 majors=5 minors=5 extend=true
|...|...|...|...|...|...|
-20 0 20 40 60 80 100 

x1=-1100 x2=90 majors=10 minors=10 extend=true
|.........|.........|.........|.........|.........|.........|.........|
-1200 -1000 -800 -600 -400 -200 0 200 

x1=0 x2=100 majors=5 minors=5 extend=true
|...|...|...|...|...|
0 20 40 60 80 100 

x1=9 x2=106 majors=5 minors=5 extend=true
|...|...|...|...|...|...|
0 20 40 60 80 100 120 

x1=9 x2=106 majors=5 minors=5 extend=false
...|...|...|...|...|..
20 40 60 80 100 

x1=-2.1 x2=9.8 majors=6 minors=10 extend=true
|.........|.........|.........|.........|.........|.........|.........|
-4 -2 0 2 4 6 8 10 

x1=-2.1 x2=9.8 majors=6 minors=10 extend=false
.|.........|.........|.........|.........|.........|.........
-2 0 2 4 6 8 

x1=-117.2 x2=112.4 majors=10 minors=1 extend=true
|||||||||||||
-120 -100 -80 -60 -40 -20 0 20 40 60 80 100 120 

x1=-1.172e+07 x2=1.124e+07 majors=10 minors=2 extend=true
|.|.|.|.|.|.|.|.|.|.|.|.|
-1.2e+07 -1e+07 -8e+06 -6e+06 -4e+06 -2e+06 0 2e+06 4e+06 6e+06 8e+06 1e+07 1.2e+07 

x1=-0.001172 x2=0.001124 majors=6 minors=2 extend=false
.|.|.|.|.|.
-0.001 -0.0005 0 0.0005 0.001 

x1=-117.2 x2=112.4 majors=10 minors=2 extend=true
|.|.|.|.|.|.|.|.|.|.|.|.|
-120 -100 -80 -60 -40 -20 0 20 40 60 80 100 120 

x1=-212 x2=-51.3 majors=3 minors=2 extend=false
.|.|.
-200 -100 

x1=2.1 x2=9.8 majors=6 minors=10 extend=true
|.........|.........|.........|.........|
2 4 6 8 10 

x1=2.1 x2=9.8 majors=6 minors=10 extend=false
|.........|.........|.........|.........
2 4 6 8 

x1=-2.1 x2=9.8 majors=6 minors=10 extend=true
|.........|.........|.........|.........|.........|.........|.........|
-4 -2 0 2 4 6 8 10 

x1=-2.1 x2=9.8 majors=6 minors=10 extend=false
.|.........|.........|.........|.........|.........|.........
-2 0 2 4 6 8 

x1=-2.1 x2=-1.2 majors=6 minors=10 extend=false
......|.........|.........|.........|.........|
-2 -1.8 -1.6 -1.4 -1.2 

x1=-2.1 x2=-1.2 majors=6 minors=10 extend=true
|.........|.........|.........|.........|.........|
-2.2 -2 -1.8 -1.6 -1.4 -1.2 

x1=-210000 x2=-120000 majors=6 minors=5 extend=true
|...|...|...|...|...|
-220000 -200000 -180000 -160000 -140000 -120000 

This function is not much tested jet, but at least it may be a starting point for people who intend do develop plotting tools.