37
37
from qwt .transform import QwtLogTransform , QwtTransform
38
38
39
39
DBL_MAX = sys .float_info .max
40
- LOG_MIN = 1.0e-100
41
- LOG_MAX = 1.0e100
40
+ LOG_MIN = 1.0e-150
41
+ LOG_MAX = 1.0e150
42
42
43
43
44
44
def qwtLogInterval (base , interval ):
@@ -198,14 +198,15 @@ def __init__(self, base=10):
198
198
self .__data = QwtScaleEngine_PrivateData ()
199
199
self .setBase (base )
200
200
201
- def autoScale (self , maxNumSteps , x1 , x2 , stepSize ):
201
+ def autoScale (self , maxNumSteps , x1 , x2 , stepSize , relative_margin = 0.0 ):
202
202
"""
203
203
Align and divide an interval
204
204
205
205
:param int maxNumSteps: Max. number of steps
206
206
:param float x1: First limit of the interval (In/Out)
207
207
:param float x2: Second limit of the interval (In/Out)
208
208
:param float stepSize: Step size
209
+ :param float relative_margin: Margin as a fraction of the interval width
209
210
:return: tuple (x1, x2, stepSize)
210
211
"""
211
212
pass
@@ -473,20 +474,27 @@ class QwtLinearScaleEngine(QwtScaleEngine):
473
474
def __init__ (self , base = 10 ):
474
475
super (QwtLinearScaleEngine , self ).__init__ (base )
475
476
476
- def autoScale (self , maxNumSteps , x1 , x2 , stepSize ):
477
+ def autoScale (self , maxNumSteps , x1 , x2 , stepSize , relative_margin = 0.0 ):
477
478
"""
478
479
Align and divide an interval
479
480
480
481
:param int maxNumSteps: Max. number of steps
481
482
:param float x1: First limit of the interval (In/Out)
482
483
:param float x2: Second limit of the interval (In/Out)
483
484
:param float stepSize: Step size
485
+ :param float relative_margin: Margin as a fraction of the interval width
484
486
:return: tuple (x1, x2, stepSize)
485
487
486
488
.. seealso::
487
489
488
490
:py:meth:`setAttribute()`
489
491
"""
492
+ # Apply the relative margin (fraction of the interval width) in linear space:
493
+ if relative_margin > 0.0 :
494
+ margin = (x2 - x1 ) * relative_margin
495
+ x1 -= margin
496
+ x2 += margin
497
+
490
498
interval = QwtInterval (x1 , x2 )
491
499
interval = interval .normalized ()
492
500
interval .setMinValue (interval .minValue () - self .lowerMargin ())
@@ -640,14 +648,15 @@ def __init__(self, base=10):
640
648
super (QwtLogScaleEngine , self ).__init__ (base )
641
649
self .setTransformation (QwtLogTransform ())
642
650
643
- def autoScale (self , maxNumSteps , x1 , x2 , stepSize ):
651
+ def autoScale (self , maxNumSteps , x1 , x2 , stepSize , relative_margin = 0.0 ):
644
652
"""
645
653
Align and divide an interval
646
654
647
655
:param int maxNumSteps: Max. number of steps
648
656
:param float x1: First limit of the interval (In/Out)
649
657
:param float x2: Second limit of the interval (In/Out)
650
658
:param float stepSize: Step size
659
+ :param float relative_margin: Margin as a fraction of the interval width
651
660
:return: tuple (x1, x2, stepSize)
652
661
653
662
.. seealso::
@@ -657,11 +666,18 @@ def autoScale(self, maxNumSteps, x1, x2, stepSize):
657
666
if x1 > x2 :
658
667
x1 , x2 = x2 , x1
659
668
logBase = self .base ()
669
+
670
+ # Apply the relative margin (fraction of the interval width) in logarithmic
671
+ # space, and convert back to linear space.
672
+ if relative_margin is not None :
673
+ log_margin = math .log (x2 / x1 , logBase ) * relative_margin
674
+ x1 /= math .pow (logBase , log_margin )
675
+ x2 *= math .pow (logBase , log_margin )
676
+
660
677
interval = QwtInterval (
661
678
x1 / math .pow (logBase , self .lowerMargin ()),
662
679
x2 * math .pow (logBase , self .upperMargin ()),
663
680
)
664
- interval = interval .limited (LOG_MIN , LOG_MAX )
665
681
if interval .maxValue () / interval .minValue () < logBase :
666
682
linearScaler = QwtLinearScaleEngine ()
667
683
linearScaler .setAttributes (self .attributes ())
@@ -674,7 +690,7 @@ def autoScale(self, maxNumSteps, x1, x2, stepSize):
674
690
linearInterval = linearInterval .limited (LOG_MIN , LOG_MAX )
675
691
676
692
if linearInterval .maxValue () / linearInterval .minValue () < logBase :
677
- # The min / max interval is too short to be represented as a log scale.
693
+ # The min / max interval is too short to be represented as a log scale.
678
694
# Set the step to 0, so that a new step is calculated and a linear scale is used.
679
695
stepSize = 0.0
680
696
return x1 , x2 , stepSize
0 commit comments