如何设置pyqtgraph轴标签偏移量?
Posted
技术标签:
【中文标题】如何设置pyqtgraph轴标签偏移量?【英文标题】:How to set pyqtgraph axis label offset? 【发布时间】:2019-11-15 08:17:41 【问题描述】:我需要在 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】:一种解决方案是创建一个自定义 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_()
【讨论】:
这是一个非常聪明的解决方案!我可能会实施它,直到可以通过AxisItem.setStyle
访问“轻推”。谢谢!以上是关于如何设置pyqtgraph轴标签偏移量?的主要内容,如果未能解决你的问题,请参考以下文章