3737from qwt .transform import QwtLogTransform , QwtTransform
3838
3939DBL_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
4242
4343
4444def qwtLogInterval (base , interval ):
@@ -198,14 +198,15 @@ def __init__(self, base=10):
198198 self .__data = QwtScaleEngine_PrivateData ()
199199 self .setBase (base )
200200
201- def autoScale (self , maxNumSteps , x1 , x2 , stepSize ):
201+ def autoScale (self , maxNumSteps , x1 , x2 , stepSize , relative_margin = 0.0 ):
202202 """
203203 Align and divide an interval
204204
205205 :param int maxNumSteps: Max. number of steps
206206 :param float x1: First limit of the interval (In/Out)
207207 :param float x2: Second limit of the interval (In/Out)
208208 :param float stepSize: Step size
209+ :param float relative_margin: Margin as a fraction of the interval width
209210 :return: tuple (x1, x2, stepSize)
210211 """
211212 pass
@@ -473,20 +474,27 @@ class QwtLinearScaleEngine(QwtScaleEngine):
473474 def __init__ (self , base = 10 ):
474475 super (QwtLinearScaleEngine , self ).__init__ (base )
475476
476- def autoScale (self , maxNumSteps , x1 , x2 , stepSize ):
477+ def autoScale (self , maxNumSteps , x1 , x2 , stepSize , relative_margin = 0.0 ):
477478 """
478479 Align and divide an interval
479480
480481 :param int maxNumSteps: Max. number of steps
481482 :param float x1: First limit of the interval (In/Out)
482483 :param float x2: Second limit of the interval (In/Out)
483484 :param float stepSize: Step size
485+ :param float relative_margin: Margin as a fraction of the interval width
484486 :return: tuple (x1, x2, stepSize)
485487
486488 .. seealso::
487489
488490 :py:meth:`setAttribute()`
489491 """
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+
490498 interval = QwtInterval (x1 , x2 )
491499 interval = interval .normalized ()
492500 interval .setMinValue (interval .minValue () - self .lowerMargin ())
@@ -640,14 +648,15 @@ def __init__(self, base=10):
640648 super (QwtLogScaleEngine , self ).__init__ (base )
641649 self .setTransformation (QwtLogTransform ())
642650
643- def autoScale (self , maxNumSteps , x1 , x2 , stepSize ):
651+ def autoScale (self , maxNumSteps , x1 , x2 , stepSize , relative_margin = 0.0 ):
644652 """
645653 Align and divide an interval
646654
647655 :param int maxNumSteps: Max. number of steps
648656 :param float x1: First limit of the interval (In/Out)
649657 :param float x2: Second limit of the interval (In/Out)
650658 :param float stepSize: Step size
659+ :param float relative_margin: Margin as a fraction of the interval width
651660 :return: tuple (x1, x2, stepSize)
652661
653662 .. seealso::
@@ -657,11 +666,18 @@ def autoScale(self, maxNumSteps, x1, x2, stepSize):
657666 if x1 > x2 :
658667 x1 , x2 = x2 , x1
659668 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+
660677 interval = QwtInterval (
661678 x1 / math .pow (logBase , self .lowerMargin ()),
662679 x2 * math .pow (logBase , self .upperMargin ()),
663680 )
664- interval = interval .limited (LOG_MIN , LOG_MAX )
665681 if interval .maxValue () / interval .minValue () < logBase :
666682 linearScaler = QwtLinearScaleEngine ()
667683 linearScaler .setAttributes (self .attributes ())
@@ -674,7 +690,7 @@ def autoScale(self, maxNumSteps, x1, x2, stepSize):
674690 linearInterval = linearInterval .limited (LOG_MIN , LOG_MAX )
675691
676692 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.
678694 # Set the step to 0, so that a new step is calculated and a linear scale is used.
679695 stepSize = 0.0
680696 return x1 , x2 , stepSize
0 commit comments