0

I want to display xticks only at the locations of data points (so the xticks == wavelengths). How can I do this?

Here is my code:

from PyQt6.QtWidgets import QApplication
import pyqtgraph as pg

app = QApplication([])

wavelengths = [610, 680, 730, 760, 810, 860]
data = [239.23, 233.81, 187.27, 176.41, 172.35, 173.78]

pw = pg.plot(wavelengths, data, symbol="o")
pw.getPlotItem().getAxis('bottom').setTicks([wavelengths])

app.exec()

I tried using AxisItem.setTicks(), but it results in an error:

    for val, strn in level:
TypeError: cannot unpack non-iterable int object
Traceback (most recent call last):
  File "/home/jure/.local/share/virtualenvs/serial-plotting-gui-dWOiQ7Th/lib/python3.10/site-packages/pyqtgraph/graphicsItems/AxisItem.py", line 608, in paint
    specs = self.generateDrawSpecs(painter)
  File "/home/jure/.local/share/virtualenvs/serial-plotting-gui-dWOiQ7Th/lib/python3.10/site-packages/pyqtgraph/graphicsItems/AxisItem.py", line 936, in generateDrawSpecs

P.S. I also tried this SO answer, but it doesn't work for me

Jurc192
  • 85
  • 2
  • 14

1 Answers1

1

It's always bit tricky with ticks in pyqtgraph. It works with tick spacing between two ticks on the axis. On top of it, it defines levels of MajorTick, MinorTicks and so on. With setTicks You have to specify list of levels (we have only MajorTicks) and each level with list of (tick value, "tick string").

So in order for Your example to work, You have to use such a list as a parameter for setTicks method.

Here is Your modified code:

from PyQt6.QtWidgets import QApplication
import pyqtgraph as pg

app = QApplication([])

wavelengths = [610, 680, 730, 760, 810, 860]
data = [239.23, 233.81, 187.27, 176.41, 172.35, 173.78]

pw = pg.plot(wavelengths, data, symbol="o")

pw.getPlotItem().getAxis('bottom').setTicks([[(wavelength, str(wavelength)) for wavelength in wavelengths]])

app.exec()
Domarm
  • 2,360
  • 1
  • 5
  • 17