# Thread: manipulating data for log scale

1. Beginner
Join Date
Nov 2012
Posts
11
Qt products
Platforms

## manipulating data for log scale

Hello,

I have data to plot either in linear scale or in semilog scale. However, part of the dataset will be negative. It is customary in the field (semiconductor physics) to plot by taking the absolute value of the data. I have my data in array of doubles, and I pass pointers using the setRawSamples function.

I dont want to apply fabs on my dataset, in order not to lose the negative info. Also it is necessary to easily flip from linear to log scale. So I would like to do it using the QwtLogTransform and the transform function. So I overloaded it in a derived class (see below). First this is not very flexible since the method is "const". Second I have a problem with the graph scale which goes to logMin (1E-150) whatever I do.

I don't understand clearly what happen here (how is the scale calculated, not using transform results ?)

Any hint welcome !

Qt Code:
`class LogTransform: public QwtLogTransform{public:     virtual double transform( double value ) const override    {        if (value == 0) return 0;        return log(fabs(value));    }     virtual QwtTransform *copy() const override    {        return new LogTransform();    } }; and also, where the log scale is applied : QwtLogScaleEngine* scaleEngine = new QwtLogScaleEngine();scaleEngine->setTransformation( new LogTransform() );`
To copy to clipboard, switch view to plain text mode

2. ## Re: manipulating data for log scale

Second I have a problem with the graph scale which goes to logMin (1E-150) whatever I do.
Uwe can answer about Qwt, but this problem could well be due to your comparison in line 7. A floating point value, especially one measured experimentally, is never exactly equal to zero unless it is set to exactly zero. So your comparison is probably only rarely true for your data. A better solution is to define some "epsilon" value, where if the absolute value of the number is less than epsilon, you treat it as zero. For experimental data, this is typically the minimum resolution of the intensity scale - where two measured values are treated as equal if their difference is less than the measurement error (the ability of the measurement to distinguish them).

Qt Code:
`double epsilon = 1.0e-6;if ( std::abs( value ) < epsilon ) return 0;`
To copy to clipboard, switch view to plain text mode

First this is not very flexible since the method is "const".
It is perfectly appropriate that these methods are defined as "const". A const method does not change the state of the class instance when it is called. In this case, transform() simply returns a new value based on the input (changing nothing), and copy() returns a new instance, again changing nothing in the class instance.

If you need to store some value in the class instance as a member variable that changes its value when transform() or copy() are called, then declare that variable as "mutable". It is designed for that purpose - to preserve the const-ness of the interface while allowing minor side-effect changes to occur in member variables.

3. Beginner
Join Date
Nov 2012
Posts
11
Qt products
Platforms

## Re: manipulating data for log scale

Hi d_stranz !

My point was that an exact value of 0 is problematic, since log(0) -> minus infinity while log (1E-6) = -6. So a value close to zero, but not exactly 0 would not be a problem. So I wanted to eliminate the exact 0 value.
I'll do a test with your suggested solution to see how the scaling behaves.
Now for the "mutable" attribute, I learned something helpful that I did not know. I'll do some test using it as well.
Thanks and Happy easter !

4. ## Re: manipulating data for log scale

My point was that an exact value of 0 is problematic, since log(0) -> minus infinity while log (1E-6) = -6.
I think you missed my point (and example). I did not say to convert values near zero to epsilon; I said to use the epsilon value to to determine if something was sufficiently close to zero that it could be treated as zero. You can set the actual value used for epsilon to whatever is appropriate for your instrumentation. Clipping the near zero values in this way avoids the logMin problem because you will never pass anything that small to the log() function - it will be trapped and set to zero by the epsilon comparison.

5. Uwe
Expert
Join Date
Feb 2006
Location
Munich, Germany
Posts
3,316
Thanked 879 Times in 827 Posts
Qt products
Platforms

## Re: manipulating data for log scale

A logarithmic scale never goes to 0 due to its nature. And because of limitations of a double it always has a minimum - like there is a limitation for what can be displayed as a maximum.

If you need to have a scale that includes the 0 you need to define ranges with different transformations.
F.e logarithmic for >= 0.01 and something based on QwtPowerTransform for below.

The "At 400" transformation in playground/scaleengine shows how to use different transformations.

HTH,
Uwe

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.