Python PyQtGraph - 如何使用 setTicks?

Python PyQtGraph - how to use setTicks?

我只想在数据点的位置显示 xticks(所以 xticks == 波长)。我该怎么做?

这是我的代码:

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

我尝试使用 AxisItem.setTicks(),但出现错误:

    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。我也试过,但它对我不起作用

在 pyqtgraph 中使用刻度总是有点棘手。它适用于轴上两个刻度之间的刻度间距。在它之上,它定义了 MajorTick、MinorTicks 等级别。使用 setTicks 您必须指定级别列表(我们只有 MajorTicks)并且每个级别都有 (tick value, "tick string").

列表

因此,为了让您的示例工作,您必须使用这样的列表作为 setTicks 方法的参数。

这是您修改后的代码:

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