-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
Short description
My situation is that I need the user to be able to switch between different axes. E.g. relative times (seconds since start), and absolute time (DateTime)
This should be pretty straight forward in my mind, but has been the cause of a lot of frustration. So I have created a small example which demonstrates some of this
Most often the issue is that the tick labels will simply not be displayed when using the DateAxisItem
Method 1) Works
- create new AxisItem
- setAxisItem
- update plotdateitem with setData
Method 2) Does not work for DateAxisItem
- update plotdateitem with setData
- create new AxisItem
- setAxisItem
Method 3) Does not work for DateAxisItem
- clear() plotitem
- create new AxisItem
- setAxisItem
- create PlotDateItem
- add plotdateitem
In my mind all three methods should work (I am sure you can come up with more methods).
the reason the tick labels are not being displayed is that they are all being excluded due to line 1277 - 1279 in AxisItem.py:
br = self.boundingRect()
if not br.contains(rect):
continueFor some reason boundingRect() does not yield the same result when I call it from my script.
####Second Issue
Setting the same AxisItem (and data) twice results in the ticks being between 0 and 1, no matter the type of axis. This is also not the desired result (I am guessing)
Code to reproduce
from datetime import datetime
import numpy as np
from PySide6.QtCore import Qt
import pyqtgraph as pg
start = datetime(2024,7,7)
stop = datetime(2024,7,9)
x = np.linspace(start.timestamp(), stop.timestamp(), 24*60*60)
y = np.cumsum(np.random.rand(len(x))-0.5)
p: pg.PlotItem
pdi: pg.PlotDataItem
class GLW(pg.GraphicsLayoutWidget):
global p, pdi, x, y
def keyPressEvent(self, event):
match event.key():
case Qt.Key.Key_S:
p.setAxisItems({'bottom': pg.AxisItem(orientation='bottom')})
case Qt.Key.Key_D:
p.setAxisItems({'bottom': pg.DateAxisItem(orientation='bottom')})
# Method 1
case Qt.Key.Key_W:
p.setAxisItems({'bottom': pg.AxisItem(orientation='bottom')})
p.items[0].setData(x=x-x[0] , y=y)
case Qt.Key.Key_E:
p.setAxisItems({'bottom': pg.DateAxisItem(orientation='bottom')})
p.items[0].setData(x=x, y=y)
# Method 1
case Qt.Key.Key_X:
p.items[0].setData(x=x-x[0] , y=y)
p.setAxisItems({'bottom': pg.AxisItem(orientation='bottom')})
case Qt.Key.Key_C:
p.items[0].setData(x=x, y=y)
p.setAxisItems({'bottom': pg.DateAxisItem(orientation='bottom')})
# Method 1
case Qt.Key.Key_2:
p.clear()
p.setAxisItems({'bottom': pg.AxisItem(orientation='bottom')})
pdi = pg.PlotDataItem(x=x-x[0], y=y)
p.addItem(pdi)
case Qt.Key.Key_3:
p.clear()
p.setAxisItems({'bottom': pg.DateAxisItem(orientation='bottom')})
pdi = pg.PlotDataItem(x=x, y=y)
p.addItem(pdi)
case _:
pass
# print(f'boundingRect {p.getAxis('bottom').boundingRect()}')
w = GLW()
pdi = pg.PlotDataItem()
p = pg.PlotItem()
p.addItem(pdi)
w.addItem(p)
if __name__ == '__main__':
print(pg.__version__)
w.show()
pg.exec()Expected behavior
- All Methods should yield the same result
- Applying the same AxisItem twice (or more) should not result in wrong tick labels being displayed.
Real behavior
Strange results. Try it out for your self.
Tested environment(s)
- PyQtGraph version: 0.14.0dev0 and 0.13.7
- Qt Python binding: PySide6 6.8.0.2 Qt 6.8.0
- Python version: 3.12.7
- NumPy version: 2.1.2
- Operating system: Windows
- Installation method: pip