垂直显示X标签

时间:2018-08-10 10:18:35

标签: python pyqt pyqt5 pyqtchart

我有一个PyQt图表,但X轴水平显示数据。

enter image description here

如何在X轴上垂直显示标签?

我想要的结果是这个(不好意思的photoshop技能!)

enter image description here

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtChart import *

#https://doc.qt.io/qt-5/qtcharts-datetimeaxis-example.html

if __name__ == '__main__':
    import sys

    if True:

        a = QApplication(sys.argv)

        x = ['2018-07-01 13:06:38', '2018-07-01 12:46:38', '2018-07-01 12:36:38', '2018-07-01 12:26:38', '2018-07-01 12:16:38', '2018-07-01 12:06:38', '2018-07-01 11:56:38', '2018-07-01 11:46:38', '2018-07-01 11:36:38', '2018-07-01 11:26:38', '2018-07-01 10:56:38', '2018-07-01 10:46:38', '2018-07-01 10:36:38'] 
        y = [23.5, 20.8, 28.0, 28.1, 28.0, 27.8, 27.3, 27.2, 25.7, 24.7, 25.0, 25.0, 24.9]

        num_ticks = 10

        time_len = 'Time'

        # Chart Type
        series = QLineSeries()
        for t, val in zip(x, y):
            series.append(QDateTime.fromString(
                t, "yyyy-MM-dd hh:mm:ss").toMSecsSinceEpoch(), val)

        # Create Chart and set General Chart setting
        chart = QChart()
        chart.addSeries(series)

        #Title Font size
        font = QFont("Sans Serif", )
        font.setPixelSize(18)
        chart.setTitleFont(font)

        chart.setTitle("Temperature in Celcius For Device:")
        chart.setAnimationOptions(QChart.SeriesAnimations)

        palette = QPalette()
        palette.setColor(QPalette.Text, Qt.red)
        chart.setPalette(palette)

        # Chart Background
        backgroundGradient = QLinearGradient(0, 0, 0, 400)
        backgroundGradient.setColorAt(0.0, QColor('#50a1dc'))
        backgroundGradient.setColorAt(1.0, QColor('#00a1de'))
        chart.setBackgroundBrush(backgroundGradient)

        #Plot area background
        plotAreaGradient = QLinearGradient(0, 100, 0, 400)
        plotAreaGradient.setColorAt(0.0, QColor('#f1f1f1'))
        plotAreaGradient.setColorAt(1.0, QColor('#ffffff'))
        chart.setPlotAreaBackgroundBrush(plotAreaGradient)
        chart.setPlotAreaBackgroundVisible(True)

        # X Axis Settings
        axisX = QDateTimeAxis()
        axisX.setTickCount(num_ticks)
        # https://doc.qt.io/qt-5/qdatetime.html#toString-2
        axisX.setFormat("dd-MM HH:mm")
        axisX.setTitleText(time_len)

        # Y Axis Settings
        axisY = QValueAxis()
        axisY.setLabelFormat("%i")
        axisY.setTitleText("Temperature C")

        # Customize axis label font
        Lfont = QFont("Sans Serif")
        Lfont.setPixelSize(16)
        axisX.setLabelsFont(Lfont)
        axisY.setLabelsFont(Lfont)

        # Customize axis colors
        axisPen = QPen(QColor('#151512'))
        axisPen.setWidth(2)
        axisX.setLinePen(axisPen)
        axisY.setLinePen(axisPen)

        # Customize axis label colors
        axisBrush = QBrush(QColor('#ffffff'))
        axisX.setLabelsBrush(axisBrush)
        axisY.setLabelsBrush(axisBrush)

        # Customize grid lines and shades
        axisX.setGridLineVisible(True)
        axisY.setGridLineVisible(True)
        axisY.setShadesBrush(QBrush(QColor('#ffffff')))
        # axisX.setShadesBrush(QBrush(QColor('#ffffff')))
        axisY.setShadesVisible(True)

        #add Axis
        chart.addAxis(axisX, Qt.AlignBottom)
        series.attachAxis(axisX)

        chart.addAxis(axisY, Qt.AlignLeft)
        series.attachAxis(axisY)

        # Create a QChartView object with QChart as a parameter. This way we don't need to create the QGraphicsView scene ourselves. We also set the Antialiasing on to have the rendered lines look nicer.
        chartView = QChartView(chart)
        chartView.setRenderHint(QPainter.Antialiasing)

        chart.axisY(series).setRange(min(y)-5, max(y)+5)
        chart.legend().setVisible(False)

        window = QMainWindow()
        window.setCentralWidget(chartView)
        window.resize(1280, 480)   
        window.show()
        sys.exit(a.exec_())

1 个答案:

答案 0 :(得分:4)

您必须使用轴的setLabelsAngle()方法,如下所示:

#add Axis
chart.addAxis(axisX, Qt.AlignBottom)
series.attachAxis(axisX)
axisX.setLabelsAngle(-90) # <--------

enter image description here