如何设置pyqtgraph轴标签偏移量?

时间:2019-07-04 14:57:03

标签: python plot pyqtgraph

我需要在GUI中实现很小的pyqtgraph图。如果这样做,默认情况下轴的标签偏移量太大。如何设置轴标签的偏移量,而不是轴刻度。

以下代码示例创建一个基本的pyqtgraph图。我能够设置刻度文本的偏移量,但不能仅设置标签文本的偏移量。我只想使轴标签更靠近轴。

import numpy as np
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg

app = QtGui.QApplication([])

x = np.linspace(0, 1, 10000)
y = np.linspace(350, 2500, 10000)


win = pg.GraphicsWindow()
plot = win.addPlot(x=x, y=y, title="Plot")
label_style = {'color': '#EEE', 'font-size': '14pt'}
plot.setLabel('bottom', "some x axis label", **label_style)
plot.setLabel('left', "some y axis label")
plot.getAxis('left').setLabel(**label_style)

font=QtGui.QFont()
font.setPixelSize(14)
plot.getAxis("bottom").tickFont = font

# Here I increased the tickTextOffset of the x axis
plot.getAxis("bottom").setStyle(tickTextOffset=50)

plot.getAxis("left").tickFont = font
plot.getAxis("left").setStyle(tickTextOffset=14)

if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

非常感谢您的帮助!

更新

我在pyqtgraph.AxisItem.resizeEvent()中找到了一个pyqtgraph内部解决方案,但是该函数不接受任何传递的参数。

    def resizeEvent(self, ev=None):
        #s = self.size()

        ## Set the position of the label
        nudge = 5
        br = self.label.boundingRect()
        p = QtCore.QPointF(0, 0)
        if self.orientation == 'left':
            p.setY(int(self.size().height()/2 + br.width()/2))
            p.setX(-nudge)
        elif self.orientation == 'right':
            p.setY(int(self.size().height()/2 + br.width()/2))
            p.setX(int(self.size().width()-br.height()+nudge))
        elif self.orientation == 'top':
            p.setY(-nudge)
            p.setX(int(self.size().width()/2. - br.width()/2.))
        elif self.orientation == 'bottom':
            p.setX(int(self.size().width()/2. - br.width()/2.))
            p.setY(int(self.size().height()-br.height()+nudge))
        self.label.setPos(p)
        self.picture = None

对应的变量为nudge。不幸的是,它无法访问,或者是否有一种方法可以绕过resizeEvent()而无需更改pyqtgraph的源代码?

作为使nudge通过的建议,我在上创建了pyqtgraph问题 github:

1 个答案:

答案 0 :(得分:1)

一种解决方案是创建一个Custom AxisItem并覆盖该方法。要调用resizeEvent,您可以进行错误的大小调整:

import numpy as np
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg


class CustomAxis(pg.AxisItem):
    @property
    def nudge(self):
        if not hasattr(self, "_nudge"):
            self._nudge = 5
        return self._nudge

    @nudge.setter
    def nudge(self, nudge):
        self._nudge = nudge
        s = self.size()
        # call resizeEvent indirectly
        self.resize(s + QtCore.QSizeF(1, 1))
        self.resize(s)

    def resizeEvent(self, ev=None):
        # s = self.size()

        ## Set the position of the label
        nudge = self.nudge
        br = self.label.boundingRect()
        p = QtCore.QPointF(0, 0)
        if self.orientation == "left":
            p.setY(int(self.size().height() / 2 + br.width() / 2))
            p.setX(-nudge)
        elif self.orientation == "right":
            p.setY(int(self.size().height() / 2 + br.width() / 2))
            p.setX(int(self.size().width() - br.height() + nudge))
        elif self.orientation == "top":
            p.setY(-nudge)
            p.setX(int(self.size().width() / 2.0 - br.width() / 2.0))
        elif self.orientation == "bottom":
            p.setX(int(self.size().width() / 2.0 - br.width() / 2.0))
            p.setY(int(self.size().height() - br.height() + nudge))
        self.label.setPos(p)
        self.picture = None


app = QtGui.QApplication([])

x = np.linspace(0, 1, 10000)
y = np.linspace(350, 2500, 10000)


win = pg.GraphicsWindow()
plot = win.addPlot(
    x=x, y=y, title="Plot", axisItems={"bottom": CustomAxis(orientation="bottom")}
)

label_style = {"color": "#EEE", "font-size": "14pt"}
plot.setLabel("bottom", "some x axis label", **label_style)
plot.setLabel("left", "some y axis label")
plot.getAxis("left").setLabel(**label_style)
font = QtGui.QFont()
font.setPixelSize(14)
plot.getAxis("bottom").tickFont = font
plot.getAxis("bottom").setStyle(tickTextOffset=50)

plot.getAxis("left").tickFont = font
plot.getAxis("left").setStyle(tickTextOffset=14)


def on_timeout():
    plot.getAxis("bottom").nudge += 1

timer = QtCore.QTimer(timeout=on_timeout, interval=500)
timer.start() 

if __name__ == "__main__":
    import sys

    if (sys.flags.interactive != 1) or not hasattr(QtCore, "PYQT_VERSION"):
        QtGui.QApplication.instance().exec_()