PyQtGraph:如何在 BarGraphItem 中命名条形图

PyQtGraph: How to name Bars in BarGraphItem

我需要能够在 PyQtGraph 中创建一个图形,它可以像这样在 x 行上显示字符串:

或者像这样在酒吧内部:

这些是我的价值观:

y = [5.509, 5.509, 5.414, 5.414, 5.414, 5.289, 5.289, 5.289, 5.289, 5.289, 5.289, 5.289, 5.174, 5.174]
x = ['RUS', 'VET', 'OCO', 'MSC', 'MAZ', 'VER', 'HAM', 'BOT', 'GAS', 'STR', 'SAI', 'RAI', 'NOR', 'PER']

这是它的外观示例。 “请注意,这不起作用”

# importing QtGui to use QIcon
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QApplication
 
# importing pyqtgraph as pg
import pyqtgraph as pg
 
# importing QtCore and QtGui from the pyqtgraph module
from pyqtgraph.Qt import QtCore

 
# creating a pyqtgraph plot window
window = pg.plot()
 
# setting window geometry
window.setGeometry(100, 100, 600, 500)
 
# title for the plot window
title = "Test"
 
# setting window title to plot window
window.setWindowTitle(title)

y = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
x = ['RUS', 'VET', 'OCO', 'MSC', 'MAZ', 'VER', 'HAM', 'BOT', 'GAS', 'STR', 'SAI', 'RAI', 'NOR', 'PER']

bargraph = pg.BarGraphItem(x=x, height=y, width=0.5)
window.addItem(bargraph)

# main method
if __name__ == '__main__':
     
    # importing system
    import sys
     
    # Start Qt event loop unless running in interactive mode or using
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QApplication.instance().exec_()

我需要我能得到的所有帮助,因为我自己什么也找不到。

这比我想象的要复杂一点。当您 运行 来自命令行的示例时,没有刻度标签的示例:

python -m pyqtgraph.examples

This question and the documentation for AxisItem 提供了如何设置刻度标签的线索。

要制作合适的条形图,您必须传入 x values。然后你传入替换刻度标签。对于 x 值,我只是从 1 到标签数:list(range(1, len(xlab)+1))。要获得对 x-axis 的引用,您可以使用 window.getAxis('bottom')。最后,从文档“刻度的格式看起来像:”

[
    [ (majorTickValue1, majorTickString1), (majorTickValue2, majorTickString2), ... ],
    [ (minorTickValue1, minorTickString1), (minorTickValue2, minorTickString2), ... ],
    ...
]

将它们放在一起我们得到:

# importing QtGui to use QIcon
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QApplication

# importing pyqtgraph as pg
import pyqtgraph as pg

# importing QtCore and QtGui from the pyqtgraph module
from pyqtgraph.Qt import QtCore


# creating a pyqtgraph plot window
window = pg.plot()

# setting window geometry
window.setGeometry(100, 100, 600, 500)

# title for the plot window
title = "Test"

# setting window title to plot window
window.setWindowTitle(title)

y = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
xlab = ['RUS', 'VET', 'OCO', 'MSC', 'MAZ', 'VER', 'HAM', 'BOT', 'GAS', 'STR', 'SAI', 'RAI', 'NOR', 'PER']
xval = list(range(1,len(xlab)+1))
print(xval)

ticks=[]
for i, item in enumerate(xlab):
    ticks.append( (xval[i], item) )
ticks = [ticks]

bargraph = pg.BarGraphItem(x=xval, height=y, width=0.5)
window.addItem(bargraph)
ax = window.getAxis('bottom')
ax.setTicks(ticks)

# main method
if __name__ == '__main__':

    # importing system
    import sys

    # Start Qt event loop unless running in interactive mode or using
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QApplication.instance().exec_()

情节是

值得指出的是,这个练习在 Matplotlib 中要容易得多。根据 this example,Matplotlib 中的等效绘图命令是 one-liner

win.bar(xlab, y)