如何绘制在 x 轴上左右移动的轨迹图?
How to draw graph for a trajectory which goes left and right in x axis?
我想在停车场的汽车的 x 和 y 方向绘制轨迹。
x 中的轨迹并不总是同一个方向。有时车会向左走。
这里的问题是:有时(不总是!)图形不会在 x 轴上向左移动。你可以在图像上看到两个不同的结果https://imgur.com/Z53fNkt
知道为什么吗?
左图是我所期望的。右边是相同的值,但我继续绘制数据的时间更长一些。
void TrackingResultsView::setupTrajectoryPlot()
{
QCustomPlot *customPlot = ui->qcp_trajectory;
customPlot->xAxis2->setVisible(true);
customPlot->xAxis2->setLabel("X-Position (pixel)");
customPlot->xAxis2->setRange(0, mModelPtr->frameSize().width());
customPlot->xAxis2->grid()->setVisible(true);
customPlot->xAxis->setRange(0, mModelPtr->frameSize().width());
customPlot->yAxis->setLabel("Y-Position (pixel)");
customPlot->yAxis->setRange(0, mModelPtr->frameSize().height());
customPlot->yAxis->setRangeReversed(true);
customPlot->yAxis2->setVisible(true);
customPlot->yAxis2->setRange(0, mModelPtr->frameSize().height());
customPlot->yAxis2->grid()->setVisible(true);
customPlot->yAxis2->setRangeReversed(true);
customPlot->addGraph(customPlot->xAxis2, customPlot->yAxis);
QVector<QVector<double>> data = createDataMap(mModelPtr->points());
customPlot->graph()->setData(data.at(0), data.at(1), true);
setTheme(customPlot, false);
}
谢谢
(英语不是我的第一语言)
QCPGraph
似乎用于每个键只有值的排序数据。从 QCustomPlot 文档来看,QCPCurve 看起来更适合绘制轨迹图(同一键的多个值)。
来自QCPCurve
描述:
Unlike QCPGraph, plottables of this type may have multiple points with the same key coordinate, so their visual representation can have loops. This is realized by introducing a third coordinate t, which defines the order of the points described by the other two coordinates x and y.
这是我在 olivier 帮助下的新代码。它的工作!
QCustomPlot *customPlot = ui->qcp_trajectory;
customPlot->xAxis2->setVisible(true);
customPlot->xAxis2->setLabel("X-Position (pixel)");
customPlot->xAxis2->setRange(0, mModelPtr->frameSize().width());
customPlot->xAxis2->grid()->setVisible(true);
customPlot->xAxis->setRange(0, mModelPtr->frameSize().width());
customPlot->yAxis->setLabel("Y-Position (pixel)");
customPlot->yAxis->setRange(0, mModelPtr->frameSize().height());
customPlot->yAxis->setRangeReversed(true);
customPlot->yAxis2->setVisible(true);
customPlot->yAxis2->setRange(0, mModelPtr->frameSize().height());
customPlot->yAxis2->grid()->setVisible(true);
customPlot->yAxis2->setRangeReversed(true);
customPlot->addGraph(customPlot->xAxis2, customPlot->yAxis);
// create empty curve objects:
QCPCurve *trajectory = new QCPCurve(customPlot->xAxis2, customPlot->yAxis);
// generate the curve data points:
const int pointCount = mModelPtr->points().size();
QVector<QCPCurveData> datatrajectory(pointCount);
QVector<QVector<double>> data = createDataMap(mModelPtr->points());
for (int i = 0; i < pointCount ; ++i)
{
datatrajectory[i] = QCPCurveData(i, data.at(0).at(i), data.at(1).at(i));
}
trajectory->data()->set(datatrajectory, true);
setTheme(customPlot, false);
我想在停车场的汽车的 x 和 y 方向绘制轨迹。
x 中的轨迹并不总是同一个方向。有时车会向左走。
这里的问题是:有时(不总是!)图形不会在 x 轴上向左移动。你可以在图像上看到两个不同的结果https://imgur.com/Z53fNkt
知道为什么吗?
左图是我所期望的。右边是相同的值,但我继续绘制数据的时间更长一些。
void TrackingResultsView::setupTrajectoryPlot()
{
QCustomPlot *customPlot = ui->qcp_trajectory;
customPlot->xAxis2->setVisible(true);
customPlot->xAxis2->setLabel("X-Position (pixel)");
customPlot->xAxis2->setRange(0, mModelPtr->frameSize().width());
customPlot->xAxis2->grid()->setVisible(true);
customPlot->xAxis->setRange(0, mModelPtr->frameSize().width());
customPlot->yAxis->setLabel("Y-Position (pixel)");
customPlot->yAxis->setRange(0, mModelPtr->frameSize().height());
customPlot->yAxis->setRangeReversed(true);
customPlot->yAxis2->setVisible(true);
customPlot->yAxis2->setRange(0, mModelPtr->frameSize().height());
customPlot->yAxis2->grid()->setVisible(true);
customPlot->yAxis2->setRangeReversed(true);
customPlot->addGraph(customPlot->xAxis2, customPlot->yAxis);
QVector<QVector<double>> data = createDataMap(mModelPtr->points());
customPlot->graph()->setData(data.at(0), data.at(1), true);
setTheme(customPlot, false);
}
谢谢
(英语不是我的第一语言)
QCPGraph
似乎用于每个键只有值的排序数据。从 QCustomPlot 文档来看,QCPCurve 看起来更适合绘制轨迹图(同一键的多个值)。
来自QCPCurve
描述:
Unlike QCPGraph, plottables of this type may have multiple points with the same key coordinate, so their visual representation can have loops. This is realized by introducing a third coordinate t, which defines the order of the points described by the other two coordinates x and y.
这是我在 olivier 帮助下的新代码。它的工作!
QCustomPlot *customPlot = ui->qcp_trajectory;
customPlot->xAxis2->setVisible(true);
customPlot->xAxis2->setLabel("X-Position (pixel)");
customPlot->xAxis2->setRange(0, mModelPtr->frameSize().width());
customPlot->xAxis2->grid()->setVisible(true);
customPlot->xAxis->setRange(0, mModelPtr->frameSize().width());
customPlot->yAxis->setLabel("Y-Position (pixel)");
customPlot->yAxis->setRange(0, mModelPtr->frameSize().height());
customPlot->yAxis->setRangeReversed(true);
customPlot->yAxis2->setVisible(true);
customPlot->yAxis2->setRange(0, mModelPtr->frameSize().height());
customPlot->yAxis2->grid()->setVisible(true);
customPlot->yAxis2->setRangeReversed(true);
customPlot->addGraph(customPlot->xAxis2, customPlot->yAxis);
// create empty curve objects:
QCPCurve *trajectory = new QCPCurve(customPlot->xAxis2, customPlot->yAxis);
// generate the curve data points:
const int pointCount = mModelPtr->points().size();
QVector<QCPCurveData> datatrajectory(pointCount);
QVector<QVector<double>> data = createDataMap(mModelPtr->points());
for (int i = 0; i < pointCount ; ++i)
{
datatrajectory[i] = QCPCurveData(i, data.at(0).at(i), data.at(1).at(i));
}
trajectory->data()->set(datatrajectory, true);
setTheme(customPlot, false);