Search code examples
c++qtplotqcustomplot

How to plot multiple points using QCustomPlot QT5?


Here are some example points:

(1,1),(2,3),(3,1),(4,2),(1,5),(3,4)

I want to plot these points with a line in turn, I've added them into the vector x and y. Then, setData(x,y) was performed.
However, the QCustomPlot seems like can only plot points by the order of x axis. I noticed the points were sorted automatically by the setData(x,y).

How can I do to plot these points by the original order?


Solution

  • What you are looking for is using QCPCurve instead of Graph.

    Define:

    QCPCurve *newCurve;
    

    And initiate it by doing:

    this->newCurve = new QCPCurve(ui->customPlot->xAxis, ui->customPlot->yAxis);
    ui->customPlot->addPlottable(this->newCurve);
    

    Then you can use it the same way as you did with the Graph:

    QVector<double> x, y;
    //...
    this->newCurve->setData(x, y);
    

    See also this example: Parametric Curves Demo.