QScatterSeries在QCharts上绘制时点不可见

2024-10-01 13:32:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用QScatterSeries绘制一个numpy数组,但是只更新轴而不显示点。我不知道为什么它不起作用。你知道吗

  • 你知道吗投影窗口.py你知道吗
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()

这是我现在打电话的结果

  • 你知道吗主.py你知道吗
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_())

获得的结果: result


Tags: fromimportselfnumpynoneappdatasys
1条回答
网友
1楼 · 发布于 2024-10-01 13:32:45

您有2个错误:

  • 标记尺寸很小,肉眼无法分辨。你知道吗
  • 当第一次建立一个系列时,QChart取最小矩形,这样在你的例子中它就在角落里,所以解决方法是考虑到足够的余量来改变轴的最小值和最大值。你知道吗
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)

输出:

enter image description here

相关问题 更多 >