Skip to content

Commit 3cf995f

Browse files
committed
QwtPlot: added support for margins
Fix #82
1 parent 40082f7 commit 3cf995f

File tree

5 files changed

+65
-5
lines changed

5 files changed

+65
-5
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
],
2525
"[python]": {
2626
"editor.codeActionsOnSave": {
27-
"source.organizeImports": true
27+
"source.organizeImports": "explicit"
2828
},
2929
"editor.defaultFormatter": "ms-python.black-formatter"
3030
},

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# PythonQwt Releases
22

3+
## Version 0.12.0
4+
5+
- Added support for margins in `QwtPlot` (see Issue #82):
6+
- Default margins are set to 0.05 (5% of the plot area) at each side of the plot
7+
- Margins are adjustable for each plot axis using `QwtPlot.setAxisMargin` (and
8+
`QwtPlot.axisMargin` to get the current value)
9+
310
## Version 0.11.2
411

512
- Fixed `TypeError` on `QwtPlotLayout.minimumSizeHint`

qwt/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
.. _GitHub: https://github.com/PlotPyStack/PythonQwt
2828
"""
2929

30-
__version__ = "0.11.2"
30+
__version__ = "0.12.0"
3131
QWT_VERSION_STR = "6.1.5"
3232

3333
import warnings

qwt/interval.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,18 @@ def extend(self, value):
396396
if not self.isValid():
397397
return self
398398
return QwtInterval(min([value, self.__minValue]), max([value, self.__maxValue]))
399+
400+
def extend_fraction(self, value):
401+
"""
402+
Extend the interval by a fraction of its width
403+
404+
:param float value: Fraction
405+
:return: extended interval
406+
"""
407+
if not self.isValid():
408+
return self
409+
return QwtInterval(
410+
self.__minValue - value * self.width(),
411+
self.__maxValue + value * self.width(),
412+
self.__borderFlags,
413+
)

qwt/plot.py

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def __init__(self):
101101
self.scaleDiv = None # QwtScaleDiv
102102
self.scaleEngine = None # QwtScaleEngine
103103
self.scaleWidget = None # QwtScaleWidget
104+
self.margin = None # Margin (float) in %
104105

105106

106107
class QwtPlot(QFrame):
@@ -431,6 +432,7 @@ def initAxesData(self):
431432
d.scaleWidget.setTitle(text)
432433

433434
d.doAutoScale = True
435+
d.margin = 0.05
434436
d.minValue = 0.0
435437
d.maxValue = 1000.0
436438
d.stepSize = 0.0
@@ -586,6 +588,19 @@ def axisStepSize(self, axisId):
586588
else:
587589
return 0
588590

591+
def axisMargin(self, axisId):
592+
"""
593+
:param int axisId: Axis index
594+
:return: Margin in % of the canvas size
595+
596+
.. seealso::
597+
598+
:py:meth:`setAxisMargin()`
599+
"""
600+
if self.axisValid(axisId):
601+
return self.__axisData[axisId].margin
602+
return 0.0
603+
589604
def axisInterval(self, axisId):
590605
"""
591606
:param int axisId: Axis index
@@ -857,6 +872,24 @@ def setAxisMaxMajor(self, axisId, maxMajor):
857872
d.isValid = False
858873
self.autoRefresh()
859874

875+
def setAxisMargin(self, axisId, margin):
876+
"""
877+
Set the margin of the scale widget
878+
879+
:param int axisId: Axis index
880+
:param float margin: Margin in % of the canvas size
881+
882+
.. seealso::
883+
884+
:py:meth:`axisMargin()`
885+
"""
886+
if self.axisValid(axisId):
887+
d = self.__axisData[axisId]
888+
if margin != d.margin:
889+
d.margin = margin
890+
d.isValid = False
891+
self.autoRefresh()
892+
860893
def setAxisTitle(self, axisId, title):
861894
"""
862895
Change the title of a specified axis
@@ -910,15 +943,20 @@ def updateAxes(self):
910943
intv[item.xAxis()] |= QwtInterval(rect.left(), rect.right())
911944
if rect.height() >= 0.0:
912945
intv[item.yAxis()] |= QwtInterval(rect.top(), rect.bottom())
946+
913947
for axisId in self.AXES:
914948
d = self.__axisData[axisId]
915949
minValue = d.minValue
916950
maxValue = d.maxValue
917951
stepSize = d.stepSize
918-
if d.doAutoScale and intv[axisId].isValid():
952+
if d.margin is not None:
953+
intv_i = intv[axisId].extend_fraction(d.margin)
954+
else:
955+
intv_i = intv[axisId]
956+
if d.doAutoScale and intv_i.isValid():
919957
d.isValid = False
920-
minValue = intv[axisId].minValue()
921-
maxValue = intv[axisId].maxValue()
958+
minValue = intv_i.minValue()
959+
maxValue = intv_i.maxValue()
922960
d.scaleEngine.autoScale(d.maxMajor, minValue, maxValue, stepSize)
923961
if not d.isValid:
924962
d.scaleDiv = d.scaleEngine.divideScale(

0 commit comments

Comments
 (0)