Skip to content

Commit

Permalink
QwtPlot: added support for margins
Browse files Browse the repository at this point in the history
Fix #82
  • Loading branch information
PierreRaybaut committed Feb 15, 2024
1 parent 40082f7 commit 3cf995f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
],
"[python]": {
"editor.codeActionsOnSave": {
"source.organizeImports": true
"source.organizeImports": "explicit"
},
"editor.defaultFormatter": "ms-python.black-formatter"
},
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# PythonQwt Releases

## Version 0.12.0

- Added support for margins in `QwtPlot` (see Issue #82):
- Default margins are set to 0.05 (5% of the plot area) at each side of the plot
- Margins are adjustable for each plot axis using `QwtPlot.setAxisMargin` (and
`QwtPlot.axisMargin` to get the current value)

## Version 0.11.2

- Fixed `TypeError` on `QwtPlotLayout.minimumSizeHint`
Expand Down
2 changes: 1 addition & 1 deletion qwt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
.. _GitHub: https://github.com/PlotPyStack/PythonQwt
"""

__version__ = "0.11.2"
__version__ = "0.12.0"
QWT_VERSION_STR = "6.1.5"

import warnings
Expand Down
15 changes: 15 additions & 0 deletions qwt/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,18 @@ def extend(self, value):
if not self.isValid():
return self
return QwtInterval(min([value, self.__minValue]), max([value, self.__maxValue]))

def extend_fraction(self, value):
"""
Extend the interval by a fraction of its width
:param float value: Fraction
:return: extended interval
"""
if not self.isValid():
return self
return QwtInterval(
self.__minValue - value * self.width(),
self.__maxValue + value * self.width(),
self.__borderFlags,
)
44 changes: 41 additions & 3 deletions qwt/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def __init__(self):
self.scaleDiv = None # QwtScaleDiv
self.scaleEngine = None # QwtScaleEngine
self.scaleWidget = None # QwtScaleWidget
self.margin = None # Margin (float) in %


class QwtPlot(QFrame):
Expand Down Expand Up @@ -431,6 +432,7 @@ def initAxesData(self):
d.scaleWidget.setTitle(text)

d.doAutoScale = True
d.margin = 0.05
d.minValue = 0.0
d.maxValue = 1000.0
d.stepSize = 0.0
Expand Down Expand Up @@ -586,6 +588,19 @@ def axisStepSize(self, axisId):
else:
return 0

def axisMargin(self, axisId):
"""
:param int axisId: Axis index
:return: Margin in % of the canvas size
.. seealso::
:py:meth:`setAxisMargin()`
"""
if self.axisValid(axisId):
return self.__axisData[axisId].margin
return 0.0

def axisInterval(self, axisId):
"""
:param int axisId: Axis index
Expand Down Expand Up @@ -857,6 +872,24 @@ def setAxisMaxMajor(self, axisId, maxMajor):
d.isValid = False
self.autoRefresh()

def setAxisMargin(self, axisId, margin):
"""
Set the margin of the scale widget
:param int axisId: Axis index
:param float margin: Margin in % of the canvas size
.. seealso::
:py:meth:`axisMargin()`
"""
if self.axisValid(axisId):
d = self.__axisData[axisId]
if margin != d.margin:
d.margin = margin
d.isValid = False
self.autoRefresh()

def setAxisTitle(self, axisId, title):
"""
Change the title of a specified axis
Expand Down Expand Up @@ -910,15 +943,20 @@ def updateAxes(self):
intv[item.xAxis()] |= QwtInterval(rect.left(), rect.right())
if rect.height() >= 0.0:
intv[item.yAxis()] |= QwtInterval(rect.top(), rect.bottom())

for axisId in self.AXES:
d = self.__axisData[axisId]
minValue = d.minValue
maxValue = d.maxValue
stepSize = d.stepSize
if d.doAutoScale and intv[axisId].isValid():
if d.margin is not None:
intv_i = intv[axisId].extend_fraction(d.margin)
else:
intv_i = intv[axisId]
if d.doAutoScale and intv_i.isValid():
d.isValid = False
minValue = intv[axisId].minValue()
maxValue = intv[axisId].maxValue()
minValue = intv_i.minValue()
maxValue = intv_i.maxValue()
d.scaleEngine.autoScale(d.maxMajor, minValue, maxValue, stepSize)
if not d.isValid:
d.scaleDiv = d.scaleEngine.divideScale(
Expand Down

0 comments on commit 3cf995f

Please sign in to comment.