【问题标题】:How to tilt the datetime string displayed on the pyqtgraph X axis如何倾斜显示在pyqtgraph X轴上的日期时间字符串
【发布时间】:2019-11-24 08:20:16
【问题描述】:

我已经解决了pyqtgraph在X轴上显示日期时间字符串的问题,但是很多日期时间字符串会导致重叠。 如何倾斜显示在 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)

【问题讨论】:

    标签: python pyqt pyqt5 pyqtgraph


    【解决方案1】:

    您可以创建自己的 AxisItem 子类并重新实现其绘制方法。我只是从源代码中复制它并修改了引用蜱虫绘画的部分。诀窍是在绘制文本之前“保存”画家的状态并旋转它,然后“恢复”它。由于旋转是基于画家的源位置,我还必须转换到 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('  '))
    
    

    【讨论】:

      猜你喜欢
      • 2015-10-24
      • 2014-06-02
      • 2016-01-03
      • 2021-06-24
      • 1970-01-01
      • 2021-02-16
      • 2021-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多