如何使用 QCustomPlot QT5 绘制多个点?

How to plot multiple points using QCustomPlot QT5?

以下是一些示例点:

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

我想依次用一条线画出这些点,我把它们添加到向量xy中了。然后,setData(x,y)被执行了。
但是,QCustomPlot 似乎只能按 x 轴的顺序绘制点。 我注意到这些点是由 setData(x,y) 自动排序的。

如何按原始顺序绘制这些点?

您正在寻找的是使用 QCPCurve 而不是 Graph。

定义:

QCPCurve *newCurve;

并通过以下方式启动它:

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

然后您可以像使用图表一样使用它:

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

另请参阅此示例:Parametric Curves Demo

根据A.Sarid的帮助,在demo(11)中找到了QCPCurve的用法。 QCPCurve 和QCPGraph 的区别在于一个x 可以用QCPCurve 对应不同的y。 所以,只需添加代码:

QCPCurve *newCurve = new QCPCurve(ui->customPlot->xAxis, ui->customPlot->yAxis); newCurve->setData(x,y);

再次感谢 A. Sarid!