pyqtgraph custom scaling issue
<p>I use the pyqtgraph library for plotting. I really like the mouse interaction on the plot (zoom, pan, ...).</p>

<p>For some of my plots I would like to change the zoom behaviour when you scroll the mousewheel. The standard implementation is a scaling in both x- and y-direction simultaneously. Scaling in the x-direction doesn't make sense on those plots, so I would like to disable that. I tried the following:</p>

<pre><code>###################################################################
#                                                                 #
#                     PLOTTING A LIVE GRAPH                       #
#                  ----------------------------                   #
#                                                                 #
###################################################################

import sys
import os
from PyQt4 import QtGui
from PyQt4 import QtCore
import pyqtgraph as pg
import numpy as np

# Override the pg.ViewBox class to add custom
# implementations to the wheelEvent
class CustomViewBox(pg.ViewBox):
    def __init__(self, *args, **kwds):
        pg.ViewBox.__init__(self, *args, **kwds)
        #self.setMouseMode(self.RectMode)


    def wheelEvent(self, ev, axis=None):
        # 1. Pass on the wheelevent to the superclass, such
        #    that the standard zoomoperation can be executed.
        pg.ViewBox.wheelEvent(ev,axis)

        # 2. Reset the x-axis to its original limits
        #
        # [code not yet written]
        #

class CustomMainWindow(QtGui.QMainWindow):

    def __init__(self):

        super(CustomMainWindow, self).__init__()

        # 1. Define look and feel of this window
        self.setGeometry(300, 300, 800, 400)
        self.setWindowTitle("pyqtgraph example")

        self.FRAME_A = QtGui.QFrame(self)
        self.FRAME_A.setStyleSheet("QWidget { background-color: %s }" % QtGui.QColor(210,210,235,255).name())
        self.LAYOUT_A = QtGui.QHBoxLayout()
        self.FRAME_A.setLayout(self.LAYOUT_A)
        self.setCentralWidget(self.FRAME_A)

        # 2. Create the PlotWidget(QGraphicsView)
        # ----------------------------------------
        self.vb = CustomViewBox()
        self.plotWidget = pg.PlotWidget(viewBox=self.vb, name='myPlotWidget')
        self.LAYOUT_A.addWidget(self.plotWidget)
        self.plotWidget.setLabel('left', 'Value', units='V')
        self.plotWidget.setLabel('bottom', 'Time', units='s')
        self.plotWidget.setXRange(0, 10)
        self.plotWidget.setYRange(0, 100)

        # 3. Get the PlotItem from the PlotWidget
        # ----------------------------------------
        self.plotItem = self.plotWidget.getPlotItem()

        # 4. Get the PlotDataItem from the PlotItem
        # ------------------------------------------
        # The plot() function adds a new plot and returns it.
        # The function can be called on self.plotWidget or self.plotItem
        self.plotDataItem = self.plotItem.plot()
        self.plotDataItem.setPen((255, 240, 240))
        self.plotDataItem.setShadowPen(pg.mkPen((70, 70, 30), width=2, cosmetic=True))


        # 5. Create the x and y arrays
        # -----------------------------
        n = np.linspace(0, 499, 500)
        self.y = 50 + 5 * (np.sin(n / 8.3)) + 7 * (np.sin(n / 7.5)) - 5 * (np.sin(n / 1.5))
        self.x = 10 * n / len(n)
        self.plotDataItem.setData(x=self.x, y=self.y)

        self.show()


if __name__== '__main__':
    app = QtGui.QApplication(sys.argv)
    QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Plastique'))
    myGUI = CustomMainWindow()


    sys.exit(app.exec_())
</code></pre>

<p>Just copy-paste this code into a fresh Python file, and you should get the following output:</p>

<p><a href="http://i.stack.imgur.com/E2ASR.png" rel="nofollow"><img src="http://i.stack.imgur.com/E2ASR.png" alt="enter image description here"></a></p>

<p>Unfortunately an error message pops up on every mouseWheel event:</p>

<pre><code>Traceback (most recent call last):
  File "pyTest.py", line 26, in wheelEvent
    pg.ViewBox.wheelEvent(ev,axis)
  File "C:\Anaconda3\lib\site-packages\pyqtgraph\graphicsItems\ViewBox\ViewBox.py", line 1206, in wheelEvent
    mask = np.array(self.state['mouseEnabled'], dtype=np.float)
AttributeError: 'QGraphicsSceneWheelEvent' object has no attribute 'state'
</code></pre>

<p>My system is as follows:</p>

<ul>
<li>Python version: 3.5.2</li>
<li>PyQt version: 4.11.4</li>
<li>Qt version: 4.8.7</li>
<li>pyqtgraph version: 0.9.10</li>
</ul>

<p>My collegue pointed out that I have to add <code>self</code> as first argument when overriding the <code>wheelEvent</code> function:</p>

<pre><code># Override the pg.ViewBox class to add custom
# implementations to the wheelEvent
class CustomViewBox(pg.ViewBox):
    def __init__(self, *args, **kwds):
        pg.ViewBox.__init__(self, *args, **kwds)
        #self.setMouseMode(self.RectMode)


    def wheelEvent(self, ev, axis=None):
        print(str(self.viewRange()))

        # 1. Pass on the wheelevent to the superclass, such
        #    that the standard zoomoperation can be executed.
        pg.ViewBox.wheelEvent(self,ev,axis) # &lt;- To override the function
                                                 properly, one should add
                                                 'self' as first argument

        # 2. Reset the x-axis to its original limits
        self.setXRange(0,10)
</code></pre>

<p>Now it works. But the only drawback is the following code line:</p>

<pre><code>    # 2. Reset the x-axis to its original limits
    self.setXRange(0,10)
</code></pre>

<p>It would be nicer to do this:</p>

<pre><code>def wheelEvent(self, ev, axis=None):
    # 1. Determine initial x-range
    initialRange = self.viewRange()

    # 2. Call the superclass method for zooming in
    pg.ViewBox.wheelEvent(self,ev,axis)

    # 3. Reset the x-axis to its original limits
    self.setXRange(initialRange[0][0],initialRange[0][1])
</code></pre>

<p>The problem is that the function <code>self.viewRange()</code> does not return [0,10] but [-0.37, 10.37] instead. The viewBox adds some margin on the left and the right. If you keep doing that, eventually those margins will drift over time:
[-0.37, 10.37] <code>-&gt;</code> [-0.74, 10.74] <code>-&gt;</code> ...</p>

python python-3.x pyqt4 pyqtgraph