我已经解决了pyqtgraph在X轴上显示datetime字符串的问题,但是许多datetime字符串会导致重叠。 如何倾斜pyqtgraph X轴上显示的日期时间字符串以避免在整个显示上重叠,我深表感谢您的帮助
self.topFiller = QtWidgets.QWidget()
self.topFiller.setMinimumSize(2000, 9000)
graphicsView = QtWidgets.QGraphicsView(self.topFiller)
y1=[100,2,8,5,9,6,1,3,9,11,13,1,1,2,8,5,9,6,1,3,9,11,13,1]
x1=["2019-7-1\r\n11:11:11","2019-7-1\r\n12:11:11","2019-7-1\r\n13:11:11","2019-7-1\r\n14:11:11","2019-7-1\r\n15:11:11","2019-7-1\r\n16:11:11","2019-7-1\r\n11:11:11","2019-7-1\r\n12:11:11","2019-7-1\r\n13:11:11","2019-7-1\r\n14:11:11","2019-7-1\r\n15:11:11","2019-7-1\r\n16:11:11","2019-7-1\r\n11:11:11","2019-7-1\r\n12:11:11","2019-7-1\r\n13:11:11","2019-7-1\r\n14:11:11","2019-7-1\r\n15:11:11","2019-7-1\r\n16:11:11","2019-7-1\r\n11:11:11","2019-7-1\r\n12:11:11","2019-7-1\r\n13:11:11","2019-7-1\r\n14:11:11","2019-7-1\r\n15:11:11","2019-7-1\r\n16:11:11"]
xdict1=dict(enumerate(x1))
stringaxis1 = pg.AxisItem(orientation='bottom')
stringaxis1.setTicks([xdict1.items()])
pw = pg.PlotWidget(graphicsView, left="rate", bottom="time", title="g1/"+str(i)+" in rate",axisItems={'bottom': stringaxis1})
curvein=pw.plot(x=list(xdict1.keys()),y=y1)
pw.getAxis("bottom").setLabel( color='#0000ff')
pw.setXRange(0,10)
答案 0 :(得分:0)
您可以创建自己的AxisItem子类并重新实现其绘画方法。我只是从源代码复制而来,并修改了有关the虫绘画的部分。诀窍是在绘制文本之前“保存”绘画者的状态并旋转它,然后“还原”它。由于旋转是基于画家的原始位置,因此我还必须转换为textRect位置并交换其宽度和高度。
可能需要进行一些小的打磨以确保文本正确对齐,但是它应该可以工作,并且还会根据刻度文本设置轴项的最小高度。
class MyAxisItem(pg.AxisItem):
def drawPicture(self, p, axisSpec, tickSpecs, textSpecs):
p.setRenderHint(p.Antialiasing, False)
p.setRenderHint(p.TextAntialiasing, True)
## draw long line along axis
pen, p1, p2 = axisSpec
p.setPen(pen)
p.drawLine(p1, p2)
p.translate(0.5,0) ## resolves some damn pixel ambiguity
## draw ticks
for pen, p1, p2 in tickSpecs:
p.setPen(pen)
p.drawLine(p1, p2)
## Draw all text
if self.tickFont is not None:
p.setFont(self.tickFont)
p.setPen(self.pen())
for rect, flags, text in textSpecs:
# this is the important part
p.save()
p.translate(rect.x(), rect.y())
p.rotate(-90)
p.drawText(-rect.width(), rect.height(), rect.width(), rect.height(), flags, text)
# restoring the painter is *required*!!!
p.restore()
stringaxis1 = MyAxisItem(orientation='bottom')
stringaxis1.setTicks([xdict1.items()])
# find the maximum width required by the tick texts and add a small margin
fm = QtGui.QFontMetrics(stringaxis1.font())
minHeight = max(fm.boundingRect(QtCore.QRect(), QtCore.Qt.AlignLeft, t).width() for t in xdict1.values())
stringaxis1.setHeight(minHeight + fm.width(' '))