Sofie
Sofie

Reputation: 13

Axis and points not corresponding with QChart

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.

Upvotes: 0

Views: 374

Answers (1)

Jonas Vieira de Souza
Jonas Vieira de Souza

Reputation: 389

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_())

Upvotes: 0

Related Questions