I'm trying to plot a numpy array using QScatterSeries, however only the axis are updated and the points are not displayed. I'm not sure why it is not working.
from PySide2.QtCore import Qt
from PySide2.QtWidgets import QWidget, QHBoxLayout
from PySide2.QtGui import QColor, QPen
from PySide2.QtCharts import QtCharts
class ProjectionWindow(QWidget):
"""
TODO
"""
def __init__(self, parent=None) -> 'None':
super().__init__()
self.setWindowTitle('Projection')
self.resize(800, 800)
self.chart = QtCharts.QChart()
self.chart_view = QtCharts.QChartView(self.chart)
self.layout = QHBoxLayout(self)
self.layout.addWidget(self.chart_view)
self.setLayout(self.layout)
self.show()
def loadCharts(self, data: 'ndarray') -> 'None':
points = QtCharts.QScatterSeries()
points.setMarkerSize(2.0)
for i in range(data.shape[0]):
points.append(data[i, 0], data[i, 1])
self.chart.addSeries(points)
self.chart.createDefaultAxes()
self.chart.show()
This is my current result when calling
import sys
import numpy as np
from PySide2.QtWidgets import QApplication
from ui.projectionwindow import ProjectionWindow
if __name__ == "__main__":
app = QApplication(sys.argv)
data = np.array([[1,2],
[3,4]])
window = ProjectionWindow(app)
window.loadCharts(data)
sys.exit(app.exec_())
You have 2 errors:
def loadCharts(self, data: "ndarray") -> "None":
points = QtCharts.QScatterSeries()
points.setMarkerSize(20)
for i in range(data.shape[0]):
points.append(data[i, 0], data[i, 1])
self.chart.addSeries(points)
self.chart.createDefaultAxes()
m_x, M_x = min(data[:, 0]), max(data[:, 0])
m_y, M_y = min(data[:, 1]), max(data[:, 1])
ax = self.chart.axes(Qt.Horizontal, points)[0]
ax.setMin(m_x - 1)
ax.setMax(M_x + 1)
ay = self.chart.axes(Qt.Vertical, points)[0]
ay.setMin(m_y - 1)
ay.setMax(M_y + 1)
Output: