Pyqtgraph ColorBarItem 旁边添加字符串到数字

huangapple go评论75阅读模式
英文:

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:

Pyqtgraph ColorBarItem 旁边添加字符串到数字

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:

  1. The unit gets displayed beside the top number:

Pyqtgraph ColorBarItem 旁边添加字符串到数字

  1. Add a string, as a title above the whole bar or closely above the top number, which says "unit = cm":

Pyqtgraph ColorBarItem 旁边添加字符串到数字

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

Pyqtgraph ColorBarItem 旁边添加字符串到数字

英文:

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

Pyqtgraph ColorBarItem 旁边添加字符串到数字

huangapple
  • 本文由 发表于 2023年6月13日 00:41:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458690.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定