pyqtgraph:如何在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_()
我需要我能得到的所有帮助,因为我自己找不到任何东西。
I need to be able to create a graph in PyQtGraph that either displays strings on the x line like this:
Or inside of the bar itself like this:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这比我想象的要复杂一些。从命令行中运行示例时,没有示例标签的示例:
此问题和 axisItem 提供了如何设置tick标签的线索。
要制作适当的条形图,您必须传递x values 。然后,您将传递替换标签。对于x值,我刚刚从1到标签数:
list(range(1,len(xlab)+1))
。要获取对X轴的引用,请使用window.getAxis('bottons')
。最后,从文档中“滴答的格式看起来像:”将所有内容放在一起:
绘图是
值得指出的是,Matplotlib中此练习在Matplotlib中更容易。根据此示例 Matplotlib中的等效绘图命令是单线
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:
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 usewindow.getAxis('bottom')
. And finally, from the docs "The format for ticks looks like:"Putting it all together we get:
and the plot is
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