Search code examples
pythonpyqtpyqt5qtcharts

How do I get the axis labels in QtChart QLineSeries


I am able to generate graphs using the QtCharts library in Python but I can't seem to figure out how to add axis labels to my graphs...

I see the ability to setLabelFormat() for a QValueAxis, but nothing on how to set the text itself. I'm sure there's something simple I am missing.

Here's the code I have to generate the graphs:

def plotLossesChart(self):

      set0 = QBarSet('Conduction Loss')
      set1 = QBarSet('Switching Loss')

      set0.append([random.randint(0, 10) for i in range(5)])
      set1.append([random.randint(0, 10) for i in range(5)])

      series = QStackedBarSeries()
      series.append(set0)
      series.append(set1)

      chart = QChart()
      chart.addSeries(series)
      chart.setTitle('Loss Summary')
      chart.setAnimationOptions(QChart.SeriesAnimations)

      months = ('Upper FET', 'Lower FET', 'Driver', 'Inductor', 'Cap ESR')

      axisX = QBarCategoryAxis()
      axisX.append(months)

      axisY = QValueAxis()
      axisY.setRange(0, 15)

      chart.addAxis(axisX, Qt.AlignBottom)
      chart.addAxis(axisY, Qt.AlignLeft)

      chart.legend().setVisible(True)
      chart.legend().setAlignment(Qt.AlignBottom)

      return chart

def plotEfficiencyChart(self):

      series = QLineSeries()

      load = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 30]
      eff = [0, 0.5726, 0.7321, 0.7978, 0.8398, 0.8616, 0.8798, 
             0.8903, 0.9002, 0.9062, 0.9127, 0.9267, 0.9379, 0.9430, 0.9448]

      for i, e in zip(load, eff):
        series.append(QPointF(float(i), float(e)*100))

      chart = QChart()
      chart.addSeries(series)
      chart.setTitle('Efficiency vs. Load')
      chart.setAnimationOptions(QChart.SeriesAnimations)

      axisX = QValueAxis()
      axisX.setRange(0, 30)
      axisX.setLabelFormat("%.1f")
      axisX.setTickCount(7)

      axisY = QValueAxis()
      axisY.setRange(0, 100)
      axisY.setLabelFormat("%d")
      axisY.setMinorTickCount(5)

      chart.addAxis(axisX, Qt.AlignBottom)
      chart.addAxis(axisY, Qt.AlignLeft)

      series.attachAxis(axisX)
      series.attachAxis(axisY)

      chart.legend().setVisible(False)

      return chart

In the main program I add the chart to a ChartView and attach it to a QGridLayout.

This is what I get:

This is what I get:

This is what I want (add labels to my axis):

enter image description here

I can't seem to find how to do this in the documentation... can anyone help me?


Solution

  • You have to use the setTitleText() method of the axis:

    def plotLossesChart(self):
        # ...
        axisY = QValueAxis()
        axisY.setRange(0, 15)
        axisY.setTitleText("Loss (W)")
        # ...
    
    def plotEfficiencyChart(self):
        # ...
        axisX = QValueAxis()
        axisX.setRange(0, 30)
        axisX.setLabelFormat("%.1f")
        axisX.setTickCount(7)
        axisX.setTitleText("Load (A)")
    
        axisY = QValueAxis()
        axisY.setRange(0, 100)
        axisY.setLabelFormat("%d")
        axisY.setMinorTickCount(5)
        axisY.setTitleText("Efficiency (%)")
        # ...

    enter image description here