Ronald Schreiber
Ronald Schreiber

Reputation: 93

PyQtGraph 0.10.0: auto range of PlotItem tries to scale TextItem and crashes

Since version 0.10.0 of PyQtGraph I have problems with TextItems in plots: I have a diagram (PlotItem) with enabled auto-scaling and a TextItem. Under some circumstances the auto-scaling tries to scale the TextItem which is not scalable. The diagram is rescaled again and again. This happens when:

  1. the plotted curve has no height or
  2. the window height is reduced until the text does not fit anymore.

For my opinion this issue was not there in version 0.9.10 of PyQtGraph.

Example for case 1:

import PySide
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg

app = QtGui.QApplication([])

win = pg.GraphicsWindow(title="auto scaling bug when having a TextItem")
win.resize(1000,500)
win.setWindowTitle("auto scaling bug when having a TextItem")

p1 = win.addPlot(title="Plot with TextItem", y=np.zeros(10))
#p1.plot(y=[100]*10)
ti = pg.TextItem(text="My TextItem", color='r', anchor=(0.0, 1.0), angle=90)
p1.addItem(ti)
ti.setPos(0, 0)
p1.enableAutoRange('y', True)

## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

Example for case 2:

import PySide
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg

app = QtGui.QApplication([])

win = pg.GraphicsWindow(title="auto scaling bug when having a TextItem")
win.resize(1000,150)
win.setWindowTitle("auto scaling bug when having a TextItem")

p1 = win.addPlot(title="Plot with TextItem", y=np.zeros(10))
p1.plot(y=[100]*10)
ti = pg.TextItem(text="My TextItem", color='r', anchor=(0.0, 1.0), angle=90)
p1.addItem(ti)
ti.setPos(0, 0)
p1.enableAutoRange('y', True)

## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

Has anyone an idea (except going back to version 0.9.10)? Thank you in advance!

Upvotes: 2

Views: 2230

Answers (1)

Ronald Schreiber
Ronald Schreiber

Reputation: 93

Oh, I found a solution by myself: The addItem method allows an option ignoreBounds. When I add the TextItem and set this option to True then the autoscale works:

p1.addItem(ti, ignoreBounds = True)

Sorry for asking before debugging.

Upvotes: 7

Related Questions