0

I'm trying to create a plot using QChart with python. I have so far managed to plot and to create an x- and y-axis, however they do not seem to correspond. I assume there is link between the chart, the axis and the series I'm missing. My self object is a chart.

My code:

        self.axis_x = QValueAxis()
        self.axis_x.setTitleText("Second")
        self.axis_x.setRange(0, 10)
        self.setAxisX(self.axis_x)
        self.addAxis(self.axis_x, Qt.AlignBottom)

        self.axis_y = QValueAxis()
        self.axis_y.setTitleText(str(template.primary_y_axis.unit))
        self.axis_y.setRange(0, 10)
        self.setAxisY(self.axis_y)
        self.addAxis(self.axis_y, Qt.AlignLeft)

        self.series = QLineSeries()
        p1 = QPoint(2, 0)
        self.series.append(p1)
        p2 = QPoint(2, 1)
        self.series.append(p2)
        p3 = QPoint(4, 2)
        self.series.append(p3)
        p4 = QPoint(6, 3)
        self.series.append(p4)
        p5 = QPoint(8, 4)
        self.series.append(p5)
        self.createDefaultAxes()
        self.series.attachAxis(self.axis_x)
        self.series.attachAxis(self.axis_y)
        self.series.setName("hei")
        self.addSeries(self.series)

        self.legend().setVisible(True)

The plot i am recieving:

I have tried using QPoint to plot, and have also tried without. No difference.

musicamante
  • 41,230
  • 6
  • 33
  • 58
Sofie
  • 13
  • 1
  • See [ask]. It's unclear due to bad English. Also post a [mre]. You mean labels of axes are incorrect? It's because you mixed calls of the ```createDefaultAxes()``` and ```addAxis()```. – relent95 Jan 27 '23 at 05:38

1 Answers1

0

I'll try to help with a reproducible example. I tried to make the example look like your Sofia information.

Basically, I added the series to the chart chart.addSeries(series) before the attachAxis definition series.attachAxis(axis_x).

If the series is added after attachAxis, the series will be readjusted to the external

import sys
from PyQt5.Qt import Qt
from PyQt5.QtGui import QPainter
from PyQt5.QtCore import QPoint
from PyQt5.QtChart import QCategoryAxis, QValueAxis
from PyQt5.QtChart import QChart, QChartView, QLineSeries
from PyQt5.QtWidgets import QApplication, QMainWindow


class MyChart(QMainWindow):
    def __init__(self):
        super().__init__()

        axis_x = QValueAxis()
        axis_x.setRange(0, 10)
        axis_x.setTitleText("Second")
        
        axis_y = QValueAxis()    
        axis_y.setRange(0, 10)
        axis_y.setTitleText("Other")

        series = QLineSeries()
        p1 = QPoint(2, 0)
        series.append(p1)
        p2 = QPoint(2, 1)
        series.append(p2)
        p3 = QPoint(4, 2)
        series.append(p3)
        p4 = QPoint(6, 3)
        series.append(p4)
        p5 = QPoint(8, 4)
        series.append(p5)
        series.setName("hei")

        chart = QChart()
        chart.addSeries(series)                
        chart.legend().setVisible(True)
        chart.createDefaultAxes()
        
        chart.setAxisX(axis_x)
        chart.setAxisY(axis_y)
       
        #chart.addAxis(axis_x, Qt.AlignBottom)
        #chart.addAxis(axis_y, Qt.AlignLeft)

        series.attachAxis(axis_x)
        series.attachAxis(axis_y)
        
        chartView = QChartView(chart)
        chartView.setRenderHint(QPainter.Antialiasing)

        self.setCentralWidget(chartView)
        self.resize(1200, 800)


if __name__ == '__main__':
    app = QApplication(sys.argv)

    chartDemo = MyChart()
    chartDemo.show()

    sys.exit(app.exec_())
  • 1
    Answering an unclear question like this does not help SO. Let's either lead the OP to edit for clearness or just move on. Anyway, the key point is not the order of ```addSeries()``` and ```attachAxis()``` calls. It's the order of ```createDefaultAxes()``` and ```setAxisX()``` calls. And the setAxisX() is deprecated. Use the addAxis(). – relent95 Jan 30 '23 at 09:44