Skip to content

DateAxisItem will often not display the tick marks and other AxisItem strangeness #3185

@tblum

Description

@tblum

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

  1. create new AxisItem
  2. setAxisItem
  3. update plotdateitem with setData

Method 2) Does not work for DateAxisItem

  1. update plotdateitem with setData
  2. create new AxisItem
  3. setAxisItem

Method 3) Does not work for DateAxisItem

  1. clear() plotitem
  2. create new AxisItem
  3. setAxisItem
  4. create PlotDateItem
  5. 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):
        continue

For 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

  1. All Methods should yield the same result
  2. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions