在 pyqtgraph 中的 x 轴上显示字符串值
Show string values on x-axis in pyqtgraph
我想在 pyqtgraph
中的 x 轴上显示刻度的字符串值。现在我不知道该怎么做。
例如:
x = ['a', 'b', 'c', 'd', 'e', 'f']
y = [1, 2, 3, 4, ,5, 6]
pg.plot(x, y)
当我尝试将字符串数组传递给 x
变量时,它会尝试将其转换为浮点数并使用错误消息破坏 GUI。
通常在 pyqtgraph 中处理自定义轴字符串时,人们将 AxisItem and override tickStrings 子类化为他们想要显示的字符串。
参见例如pyqtgraph : how to plot time series (date and time on the x axis)?
Pyqtgraphs axisitem 也有一个内置的 setTicks 允许您指定将要显示的刻度,这可以解决像这样的简单问题而不是子类化 AxisItem。
可以像这样在 x 轴上绘制自定义字符串。
- 创建一个包含 x 值的 dict 以及要在轴上显示的字符串。
xdict = {0:'a', 1:'b', 2:'c', 3:'d', 4:'e', 5:'f'}
或使用
x = ['a', 'b', 'c', 'd', 'e', 'f']
xdict = dict(enumerate(x))
- 在 AxisItem 中使用 setTicks 或
子类 AxisItem 并在 tickStrings 中找到值对应的字符串。
1。使用标准的 pyqtgraph AxisItem 和 setTicks
from PyQt4 import QtCore
import pyqtgraph as pg
x = ['a', 'b', 'c', 'd', 'e', 'f']
y = [1, 2, 3, 4, 5, 6]
xdict = dict(enumerate(x))
win = pg.GraphicsWindow()
stringaxis = pg.AxisItem(orientation='bottom')
stringaxis.setTicks([xdict.items()])
plot = win.addPlot(axisItems={'bottom': stringaxis})
curve = plot.plot(list(xdict.keys()),y)
if __name__ == '__main__':
import sys
if sys.flags.interactive != 1 or not hasattr(QtCore, 'PYQT_VERSION'):
pg.QtGui.QApplication.exec_()
2。通过子类化 AxisItem
实现
这是一种更通用的方法,可以轻松更改为各种有趣的东西,例如将 unix 时间戳转换为日期。
from PyQt4 import QtCore
import pyqtgraph as pg
import numpy as np
class MyStringAxis(pg.AxisItem):
def __init__(self, xdict, *args, **kwargs):
pg.AxisItem.__init__(self, *args, **kwargs)
self.x_values = np.asarray(xdict.keys())
self.x_strings = xdict.values()
def tickStrings(self, values, scale, spacing):
strings = []
for v in values:
# vs is the original tick value
vs = v * scale
# if we have vs in our values, show the string
# otherwise show nothing
if vs in self.x_values:
# Find the string with x_values closest to vs
vstr = self.x_strings[np.abs(self.x_values-vs).argmin()]
else:
vstr = ""
strings.append(vstr)
return strings
x = ['a', 'b', 'c', 'd', 'e', 'f']
y = [1, 2, 3, 4, 5, 6]
xdict = dict(enumerate(x))
win = pg.GraphicsWindow()
stringaxis = MyStringAxis(xdict, orientation='bottom')
plot = win.addPlot(axisItems={'bottom': stringaxis})
curve = plot.plot(list(xdict.keys()),y)
if __name__ == '__main__':
import sys
if sys.flags.interactive != 1 or not hasattr(QtCore, 'PYQT_VERSION'):
pg.QtGui.QApplication.exec_()
示例截图:
我发现最简单的方法是准备一个索引列表和一个字符串列表,然后 zip
将它们放在一起:
ticks = [list(zip(range(5), ('a', 'b', 'c', 'd', 'e')))]
您可以像这样获取 PlotWidget 的现有 AxisItem:
pw = pg.PlotWidget()
xax = pw.getAxis('bottom')
最后像这样设置轴的刻度:
xax.setTicks(ticks)
据我所知,PlotWidgets 自动包含 'bottom' 和 'left' AxisItems,但您可以根据需要创建和添加其他项目。
我试图完成相同的任务,但在 'PyQt5' 上遇到了已弃用的错误。我几乎没有改变 luddek 的回答,但它是这样的:
import pyqtgraph as pg
from PyQt5 import QtWidgets
from pyqtgraph.Qt import QtGui
xdict = {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f'}
x=[1, 2, 3, 4, 5, 6]
y = [1, 2, 3, 4, 5, 6]
windows = pg.plot(x, y)
stringaxis = pg.AxisItem(orientation='bottom')
stringaxis.setTicks([xdict.items()])
windows.setAxisItems(axisItems = {'bottom': stringaxis})
QtWidgets.QApplication.exec_()
我想在 pyqtgraph
中的 x 轴上显示刻度的字符串值。现在我不知道该怎么做。
例如:
x = ['a', 'b', 'c', 'd', 'e', 'f']
y = [1, 2, 3, 4, ,5, 6]
pg.plot(x, y)
当我尝试将字符串数组传递给 x
变量时,它会尝试将其转换为浮点数并使用错误消息破坏 GUI。
通常在 pyqtgraph 中处理自定义轴字符串时,人们将 AxisItem and override tickStrings 子类化为他们想要显示的字符串。
参见例如pyqtgraph : how to plot time series (date and time on the x axis)?
Pyqtgraphs axisitem 也有一个内置的 setTicks 允许您指定将要显示的刻度,这可以解决像这样的简单问题而不是子类化 AxisItem。
可以像这样在 x 轴上绘制自定义字符串。
- 创建一个包含 x 值的 dict 以及要在轴上显示的字符串。
xdict = {0:'a', 1:'b', 2:'c', 3:'d', 4:'e', 5:'f'}
或使用
x = ['a', 'b', 'c', 'd', 'e', 'f']
xdict = dict(enumerate(x))
- 在 AxisItem 中使用 setTicks 或 子类 AxisItem 并在 tickStrings 中找到值对应的字符串。
1。使用标准的 pyqtgraph AxisItem 和 setTicks
from PyQt4 import QtCore
import pyqtgraph as pg
x = ['a', 'b', 'c', 'd', 'e', 'f']
y = [1, 2, 3, 4, 5, 6]
xdict = dict(enumerate(x))
win = pg.GraphicsWindow()
stringaxis = pg.AxisItem(orientation='bottom')
stringaxis.setTicks([xdict.items()])
plot = win.addPlot(axisItems={'bottom': stringaxis})
curve = plot.plot(list(xdict.keys()),y)
if __name__ == '__main__':
import sys
if sys.flags.interactive != 1 or not hasattr(QtCore, 'PYQT_VERSION'):
pg.QtGui.QApplication.exec_()
2。通过子类化 AxisItem
实现这是一种更通用的方法,可以轻松更改为各种有趣的东西,例如将 unix 时间戳转换为日期。
from PyQt4 import QtCore
import pyqtgraph as pg
import numpy as np
class MyStringAxis(pg.AxisItem):
def __init__(self, xdict, *args, **kwargs):
pg.AxisItem.__init__(self, *args, **kwargs)
self.x_values = np.asarray(xdict.keys())
self.x_strings = xdict.values()
def tickStrings(self, values, scale, spacing):
strings = []
for v in values:
# vs is the original tick value
vs = v * scale
# if we have vs in our values, show the string
# otherwise show nothing
if vs in self.x_values:
# Find the string with x_values closest to vs
vstr = self.x_strings[np.abs(self.x_values-vs).argmin()]
else:
vstr = ""
strings.append(vstr)
return strings
x = ['a', 'b', 'c', 'd', 'e', 'f']
y = [1, 2, 3, 4, 5, 6]
xdict = dict(enumerate(x))
win = pg.GraphicsWindow()
stringaxis = MyStringAxis(xdict, orientation='bottom')
plot = win.addPlot(axisItems={'bottom': stringaxis})
curve = plot.plot(list(xdict.keys()),y)
if __name__ == '__main__':
import sys
if sys.flags.interactive != 1 or not hasattr(QtCore, 'PYQT_VERSION'):
pg.QtGui.QApplication.exec_()
示例截图:
我发现最简单的方法是准备一个索引列表和一个字符串列表,然后 zip
将它们放在一起:
ticks = [list(zip(range(5), ('a', 'b', 'c', 'd', 'e')))]
您可以像这样获取 PlotWidget 的现有 AxisItem:
pw = pg.PlotWidget()
xax = pw.getAxis('bottom')
最后像这样设置轴的刻度:
xax.setTicks(ticks)
据我所知,PlotWidgets 自动包含 'bottom' 和 'left' AxisItems,但您可以根据需要创建和添加其他项目。
我试图完成相同的任务,但在 'PyQt5' 上遇到了已弃用的错误。我几乎没有改变 luddek 的回答,但它是这样的:
import pyqtgraph as pg
from PyQt5 import QtWidgets
from pyqtgraph.Qt import QtGui
xdict = {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f'}
x=[1, 2, 3, 4, 5, 6]
y = [1, 2, 3, 4, 5, 6]
windows = pg.plot(x, y)
stringaxis = pg.AxisItem(orientation='bottom')
stringaxis.setTicks([xdict.items()])
windows.setAxisItems(axisItems = {'bottom': stringaxis})
QtWidgets.QApplication.exec_()