pyqtgraph:如何在Bargraphitem中命名栏

发布于 2025-01-23 23:12:57 字数 1725 浏览 3 评论 0原文

我需要能够在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_()

我需要我能得到的所有帮助,因为我自己找不到任何东西。

I need to be able to create a graph in PyQtGraph that either displays strings on the x line like this:
enter image description here

Or inside of the bar itself like this:

enter image description here

These are my values:

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']

This is an example of what it could look like. "Note that this does not work"

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

I need all the help I can get, since I haven't been able to find out anything myself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞美少年 2025-01-30 23:12:57

这比我想象的要复杂一些。从命令行中运行示例时,没有示例标签的示例:

python -m pyqtgraph.examples

此问题 axisItem 提供了如何设置tick标签的线索。

要制作适当的条形图,您必须传递x values 。然后,您将传递替换标签。对于x值,我刚刚从1到标签数:list(range(1,len(xlab)+1))。要获取对X轴的引用,请使用window.getAxis('bottons')。最后,从文档中“滴答的格式看起来像:”

[
    [ (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中此练习在Matplotlib中更容易。根据此示例 Matplotlib中的等效绘图命令是单线

win.bar(xlab, y)

This was a little more complicated than I thought. There are no examples for tick labels when you run the examples from the command line:

python -m pyqtgraph.examples

This question and the documentation for AxisItem provided the clues for how to set tick labels.

To make a proper bar chart, you have to pass in x values. And then you pass in replacement tick labels. For x values I just went from 1 to the number of labels: list(range(1, len(xlab)+1)). To get a reference to the x-axis, you use window.getAxis('bottom'). And finally, from the docs "The format for ticks looks like:"

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

Putting it all together we get:

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

and the plot is

enter image description here

It's worth pointing out that this exercise is significantly easier in Matplotlib. According to this example the equivalent plotting command in Matplotlib is a one-liner

win.bar(xlab, y)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文