英文:
Pyqtgraph ColorBarItem Add String Next to Number
问题
以下是代码的翻译部分:
import pyqtgraph as pg
from pyqtgraph.Qt import QtWidgets, mkQApp
class MyBar(pg.ColorBarItem):
def __init__(self, *args, **kargs):
super().__init__(*args, **kargs)
self.unit = "cm"
self.display_unit()
def display_unit(self):
pass
class MainWindow(QtWidgets.QMainWindow):
""" example application main window """
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
gr_wid = pg.GraphicsLayoutWidget(show=True)
self.setCentralWidget(gr_wid)
self.resize(600, 500)
self.show()
bar = MyBar(values=(-1, 1), colorMap=None)
gr_wid.addItem(bar)
mkQApp("")
main_window = MainWindow()
if __name__ == '__main__':
pg.exec()
请注意,这只是代码的翻译部分,没有包括问题中提到的功能添加部分。如果您需要帮助添加单位显示功能,请提出具体的问题。
英文:
The following code is a simple modification of the pyqtgraph
example "Matrix Display".
import pyqtgraph as pg
from pyqtgraph.Qt import QtWidgets, mkQApp
class MyBar(pg.ColorBarItem):
def __init__(self, *args, **kargs):
super().__init__(*args,**kargs)
self.unit = "cm"
self.display_unit()
def display_unit(self):
pass
class MainWindow(QtWidgets.QMainWindow):
""" example application main window """
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
gr_wid = pg.GraphicsLayoutWidget(show=True)
self.setCentralWidget(gr_wid)
self.resize(600,500)
self.show()
bar = MyBar( values=(-1,1), colorMap=None)
gr_wid.addItem(bar)
mkQApp("")
main_window = MainWindow()
if __name__ == '__main__':
pg.exec()
I have PyQt5
and pyqtgraph
installed in my environment. The code runs and gives:
I would like to write a method to add units to my bar. Given a string unit
, in this case "cm"
, I want to achieve any one of two, preferably within this class MyBar
, that:
- The unit gets displayed beside the top number:
- Add a string, as a title above the whole bar or closely above the top number, which says "unit = cm":
Remark that I am only asking to display an additional string. Nothing in terms of functionality of the bar should change. I tried to go through the source code but had found no way helpful to achieve this.
答案1
得分: 1
有一个“正确”的方法可以让它工作。pyqtgraph内置了自动处理轴单位的功能,它会自动将单位缩放到正确的SI前缀('m'代表毫,'k'代表千,等等...)。所以你只需要获取ColorBarItem
的主轴并给它一个适当的单位。
唯一的注意事项是,pyqtgraph不认为'c'代表厘米是一个SI前缀,它会自动选择毫米。因此,如果你想强制使用“cm”并放弃任何自动缩放的功能,你需要在轴上调用enableAutoSIPrefix(False)
。
import pyqtgraph as pg
from pyqtgraph.Qt import QtWidgets, mkQApp
class MyBar(pg.ColorBarItem):
def __init__(self, *args, units=None, **kargs):
super().__init__(*args, **kargs)
if units is not None:
self.set_units(units)
def set_units(self, units):
self.axis.setLabel(units=units)
class MainWindow(QtWidgets.QMainWindow):
""" example application main window """
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
gr_wid = pg.GraphicsLayoutWidget(show=True)
self.setCentralWidget(gr_wid)
self.resize(600, 500)
self.show()
bar = MyBar(values=(-0.01, 0.01), colorMap=None, units="m")
gr_wid.addItem(bar)
mkQApp("")
main_window = MainWindow()
if __name__ == '__main__':
pg.exec()
英文:
There actually is a "proper" way to get this to work. pyqtgraph has a built-in automatic handling for axis units, which automatically scales units to the correct SI prefix ('m' for milli-, 'k' for kilo-, etc...). So you just need to grab the ColorBarItem
's main axis and give it an appropriate unit.
The one caveat is that 'c' for centi- is not considered an SI prefix py pyqtgraph - it'll automatically go for milli- instead. So, if you want to force it to use "cm" and forego any autoscaling fanciness, you need to call enableAutoSIPrefix(False)
on the axis.
import pyqtgraph as pg
from pyqtgraph.Qt import QtWidgets, mkQApp
class MyBar(pg.ColorBarItem):
def __init__(self, *args, units=None, **kargs):
super().__init__(*args, **kargs)
if units is not None:
self.set_units(units)
def set_units(self, units):
self.axis.setLabel(units=units)
class MainWindow(QtWidgets.QMainWindow):
""" example application main window """
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
gr_wid = pg.GraphicsLayoutWidget(show=True)
self.setCentralWidget(gr_wid)
self.resize(600, 500)
self.show()
bar = MyBar(values=(-0.01, 0.01), colorMap=None, units="m")
gr_wid.addItem(bar)
mkQApp("")
main_window = MainWindow()
if __name__ == '__main__':
pg.exec()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论