Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

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()
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.