Graph axis calibration in C ++

I draw some floating point data in a 2D plot, and I need to calibrate the plot axis to small units that look neat. Obviously, this unit varies with the amount of data. I am trying to find a good way to split the axis into a beautiful number. for example, if my data is -1.3345 to +5.882, it can divide units into 1.0 or 0.5. if my data from -100 to 800 divide the axes into units of 100 or 50. (I hope this makes sense) right now I am dividing the range (the largest value is the smallest value) by some fixed integer and getting the units, but this gives me an ugly number with long long digits. Is there any reasonable way to do this?

+2
source share
2 answers

ACM Algorithm 463 provides three simple functions for creating good axes with outputs xminp, xmaxp and dist for the minimum and maximum values ​​on the scale and the distance between the marks on the scale, given the request for intervals n , which include the xmin and xmax data points:

  • Scale1() gives a linear scale with approximately n intervals and dist is an integer cardinality 10 times 1, 2, or 5.
  • Scale2() gives a linear scale at exact intervals n (the gap between xminp and xmaxp tends to be larger than the gap created by Scale1() ).
  • Scale3() gives a logarithmic scale.

The code is in Fortran, but it is very easy to interpret and translate to other languages. There are more complex functions that give a more beautiful scale (for example, those specified in gnuplot ), but Scale1 will most likely do this work with minimal code.

(EDIT)

I found the text of the original 1973 document on the Internet here , which gives more explanation than the code linked to above.

+1
source

One way to calculate a good step would be to find the value of the most significant digit of the length of the range (i.e. diff = maxVlaue - minValue ) and use it as a step. To calculate the value of the most significant digit, use this simple formula:

 pow(10, floor(log10(diff))) 

This takes the decimal logarithm of the difference, discards the fractional part, if any, and increments the ten to the extent of this logarithm. For a difference of 7.2165, the calculation will return 1; for 721.65 it will return 100, etc.

One of the drawbacks of this calculation is that the grid pitch for diff from 9.99 and diff from 1.001 will be the same. One way to solve this issue is to calculate the number of grid lines that you get for the step, and reduce the step ten times if the number of lines is not enough (for example, less than three).

+2
source

All Articles