Skip to content

Fix ViewBox.autoRange() for case of data with constant y-value#2489

Merged
j9ac9k merged 1 commit intopyqtgraph:masterfrom
bbc131:origin/auto_range
Oct 29, 2022
Merged

Fix ViewBox.autoRange() for case of data with constant y-value#2489
j9ac9k merged 1 commit intopyqtgraph:masterfrom
bbc131:origin/auto_range

Conversation

@bbc131
Copy link
Copy Markdown
Contributor

@bbc131 bbc131 commented Oct 13, 2022

Calling ViewBox.autoRange() multiple times lead to different results if the plotted data points have all identical y-values. For example, when you plot an array like [1.0, 1.0, ... 1.0], as I do in the mwe below.

The reason for that can be found in ViewBox.setRange(rect, padding, ...), where for this special case of 0 extent of the vertical axis, the current visible range shall be preserved, see also comment "If we requested 0 range, try to preserve previous scale." in line 594.
But with padding being not equal to 0.0, the current visible range is not preserved, but rather enlarged by the padding.

The only way I see to circumvent this is to add some other special handling here.

Here a mwe which simply plots some data as described above und further calls ViewBox.autoRange() when you click into the widget.

import pyqtgraph as pg
from PySide2 import QtCore


app = pg.mkQApp()

class ClickablePlotWidget(pg.PlotWidget):
    clicked: QtCore.Signal = QtCore.Signal()

    def mousePressEvent(self, ev):
        self.clicked.emit()
        super().mousePressEvent(ev)


p = ClickablePlotWidget()
p.show()

p.plot([1.0, 1.0, 1.0])  # bad
# p.plot([1.0, 10.0, 1.0])  # good

def _autorange():
    print("ViewBox.autoRange()")
    vb = p.plotItem.getViewBox()
    vb.autoRange()
    # vb.autoRange(padding=0.0)  # workaround

p.clicked.connect(_autorange)


if __name__ == '__main__':
    pg.exec()

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Oct 26, 2022

Hi @bbc131 Thanks for the PR, sorry it's been a while before someone replied, I was on vacation. The diff LGTM, do you know if this fixes any of the existing open issues involving autoRange such as #2328 ?

@bbc131
Copy link
Copy Markdown
Contributor Author

bbc131 commented Oct 27, 2022

do you know if this fixes any of the existing open issues involving autoRange such as #2328 ?

#2328 is a different problem and won't be fixed.
#2206 would be fixed.

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Oct 29, 2022

Thanks for the PR @bbc131 this LGTM, merging!

@j9ac9k j9ac9k merged commit 83611af into pyqtgraph:master Oct 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants