pyqtgraph / pyqtgraph

Fast data visualization and GUI tools for scientific / engineering applications
https://www.pyqtgraph.org
Other
3.88k stars 1.1k forks source link

LabelItem inverted in Y-axis (upside-down) #2804

Open stan-servaenergy opened 1 year ago

stan-servaenergy commented 1 year ago

Short description

LabelItem inverted in Y-axis, possibly also at wrong coordinates.

Code to reproduce

import pyqtgraph as pg

app = pg.mkQApp()
plt = pg.plot([1, 2, 3], [10, 2, 1])
lbl = pg.LabelItem(text="testing")

plt.addItem(lbl)
app.exec()

Expected behavior

Label has normal orientation.

Real behavior

Label is upside down and probably at wrong coordinates too.

image

Tested environment(s)

Additional context

I need to use LabelItem instead of TextItem, because for my use case I need my plot labels to scale with plot (e.g. zoom in/out).

pijyoi commented 1 year ago
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui, QtWidgets

app = pg.mkQApp()
plt = pg.PlotWidget()
plt.show()

plt.plot([1, 2, 3], [10, 2, 1])

text = QtWidgets.QGraphicsSimpleTextItem("testing")
text.setBrush(QtCore.Qt.GlobalColor.white)
y = text.boundingRect().height() + 2
text.setTransform(QtGui.QTransform(1,0,0,-1,0,y))
plt.addItem(text)

# debug placement
rectitem = QtWidgets.QGraphicsRectItem(text.boundingRect())
rectitem.setPen(pg.mkPen('r', style=QtCore.Qt.PenStyle.DashLine))
plt.addItem(rectitem)

app.exec()
stan-servaenergy commented 1 year ago

Thank you, pijyoi for your example on how to workaround the issue. Following it, I was able to resolve LabelItem placement for my use case.

Is there anything I can help with to create a permanent fix for everyone?

pijyoi commented 1 year ago

Could you provide a scenario where plotting scaled text would be useful? The following snippet surely isn't one of them.

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

app = pg.mkQApp()
plt = pg.PlotWidget()
plt.show()

x = np.linspace(-10,10,201)
y = np.sinc(x)
plt.plot(x, y)

text = QtWidgets.QGraphicsSimpleTextItem("testing")
text.setBrush(QtCore.Qt.GlobalColor.white)
y = text.boundingRect().height() + 2
text.setTransform(QtGui.QTransform(1,0,0,-1,0,y))
pt = QtCore.QPointF(0, 1)
text.setPos(pt)
plt.addItem(text)

# debug placement
rect = text.boundingRect()
rect.moveTo(pt)
rectitem = QtWidgets.QGraphicsRectItem(rect)
rectitem.setPen(pg.mkPen('r', style=QtCore.Qt.PenStyle.DashLine))
plt.addItem(rectitem)

app.exec()
stan-servaenergy commented 1 year ago

In my use case, I have a DAG (Directed acyclic graph) and use ScatterPlotItem with large size argument to display nodes as oversized "bubbles". I'm using LabelItem to place node description labels inside those "bubbles". The width of edges between nodes also conveys information.

image

I'm using pxMode set to False, so that "bubbles" shrink/expand as you zoom in/out. Without it I was getting "casino chip" effect of nodes staying same screen size and obscuring edges or even whole plot when zoomed out.

Perhaps this is bit abusing the components and I should have developed custom GraphItem, but initially this looked like a easy way to go.