Search code examples
pythonqtqmlqtcharts

How to plot data in QML Qt Charts (with python)


I want to plot data in Qt charts (QML) from python. The x,y data are saved in array x = np.array([0, 6]) y = np.array([0, 250]) . I am desperate how to pass these data to Qt Charts with one step. I can do it (step by step) with Signal/Slot, where the Signal is with parameters (x,y).

My working (very slow) code:

Creating signal from python to qml:

class AppWindow(QObject):

    # Signals from python to QML
    sigPlotData = Signal(int, int, arguments=['x','y'])

and later () I generate and send data to chart like that:

...      
for i in range(50):
   self.app.sigPlotData.emit(i, random.randint(0,150))
...

In QML file I do this:

 //connections from Python to QML via signals
    Connections {
        target: backend

        function onSigPlotData(x,y){
          
            lineSer.append(x, y);
        }
    }

ChartView {
 id: chartView
 title: "Line"
 anchors.fill: parent

ValueAxis{
     id: axisX
     min: 0
     max: maxX
     }

 ValueAxis{
    id: axisY
    min: 0
    max: 150
  }

 LineSeries {
 id: lineSer
 name: "data"
 axisX: axisX
 axisY: axisY
 }
}

Thank you very much for help.


Solution

  • For me the best solution:

    start = time.time()
    points = []
    for i in self.x:
         points.append(QPointF(i, self.y[i]))   # filling points with my prepared data 
    serie1.replace(points)  # fill list of points in one call
    end = time.time()
    print(end - start)
    

    for 50000 values it takes only 126 ms, for 5000 values it takes 13 ms