设为首页 加入收藏

TOP

【QCustomPlot】绘制 x-y 曲线图(二)
2023-07-23 13:26:31 】 浏览:72
Tags:QCustomPlot 绘制 x-y
(yAxis2),并与xAxis/yAxis保持同步 customPlot->axisRect()->setupFullAxesBox(true); // 生成x-y数据,y1=x^2,y2=x^3,定义域[-1,1] QVector<double> x(101), y1(101), y2(101); for (int i = 0; i < 101; ++i) { x[i] = i/50.0 - 1; y1[i] = x[i]*x[i]; y2[i] = x[i]*x[i]*x[i]; } // 新建QCPGraph对象,并设置绘图数据x-y1 customPlot->addGraph(); customPlot->graph(0)->setPen(QPen(Qt::blue)); customPlot->graph(0)->setData(x, y1); customPlot->graph(0)->setName(QStringLiteral("二次曲线图例")); // 新建QCPGraph对象,并设置绘图数据x-y2 customPlot->addGraph(); customPlot->graph(1)->setPen(QPen(Qt::red)); customPlot->graph(1)->setData(x, y2); customPlot->graph(1)->setName(QStringLiteral("三次曲线图例")); // 显示图例 customPlot->legend->setVisible(true); // 设置标题 customPlot->plotLayout()->insertRow(0); customPlot->plotLayout()->addElement(0, 0, new QCPTextElement(customPlot, "Test-Title", QFont("sans", 17, QFont::Bold))); // 设置坐标轴标签 customPlot->xAxis->setLabel("x"); customPlot->yAxis->setLabel("y"); // 设置坐标轴范围 customPlot->xAxis->setRange(-1, 1); customPlot->yAxis->setRange(-1, 1); // 刷新显示 customPlot->replot(); }

绘制效果:

Oh Shit!-图片走丢了-打个广告-欢迎来博客园关注“木三百川”

5. 绘制往回走的 x-y 曲线

举个例子,若需要绘制 \(x=(y+0.8)\times y\times (y-0.8),y\in [-1,1]\) 曲线,有三种方法:

  • 方法一:新建 QCPGraph 对象时,指定 yAxiskeyAxis,指定 xAxisvalueAxis,即互换一下坐标轴的角色,这是最靠谱也最常用的方法。
  • 方法二:仍以 xAxiskeyAxisyAxisvalueAxis(默认情况),但在调用 setData() 时,需传入第三个参数 true。这是一种偷懒的做法,并且绘图的横轴数据(keyAxis)需满足一定的条件:keyData 必须先递增再减小、且减小时不得离 keyData[0] 太近,否则绘图会出错。
  • 方法三:导出绘图数据的内存地址,直接将数据写入内存中,这种做法常被用来提升 QCustomPlot 性能,缩短数据更新时间,但用此来绘制往回走的 x-y 曲线时,绘图的横轴数据也需要满足上面的条件,否则绘图会出错。

当曲线形成的环路很复杂时,一般采用绘制参数曲线的方法来表现,详见 Documentation - QCPCurve 或本人同系列文章。

5.1 靠谱方法:互换 x-y 轴

demoPlot() 函数如下:

void demoPlot(QCustomPlot *customPlot)
{
    // 显示上方横轴(xAxis2)与右方纵轴(yAxis2),并与xAxis/yAxis保持同步
    customPlot->axisRect()->setupFullAxesBox(true);

    // 生成y-x数据, x=(y+0.8)*y*(y-0.8), 定义域[-1,1]
    QVector<double> x(101), y(101);
    for (int i = 0; i < 101; ++i)
    {
        y[i] = i/50.0 - 1;
        x[i] = (y[i]+0.8)*y[i]*(y[i]-0.8);
    }

    // 新建QCPGraph对象(互换xAxis/yAxis),并设置绘图数据
    customPlot->addGraph(customPlot->yAxis, customPlot->xAxis);
    customPlot->graph(0)->setData(y, x);

    // 设置标题
    customPlot->plotLayout()->insertRow(0);
    customPlot->plotLayout()->addElement(0, 0, new QCPTextElement(customPlot, "Test-Title", QFont("sans", 17, QFont::Bold)));

    // 设置坐标轴标签
    customPlot->xAxis->setLabel("x");
    customPlot->yAxis->setLabel("y");

    // 设置坐标轴范围
    customPlot->xAxis->setRange(-0.5, 0.5);
    customPlot->yAxis->setRange(-1, 1);

    // 刷新显示
    customPlot->replot();
}

绘制效果:

Oh Shit!-图片走丢了-打个广告-欢迎来博客园关注“木三百川”

5.2 偷懒方法:设置 alreadySorted = true

demoPlot() 函数如下:

void demoPlot(QCustomPlot *customPlot)
{
    // 显示上方横轴(xAxis2)与右方纵轴(yAxis2),并与xAxis/yAxis保持同步
    customPlot->axisRect()->setupFullAxesBox(true);

    // 生成y-x数据, x=(y+0.8)*y*(y-0.8), 定义域[-1,1]
    QVector<double> x(101), y(101);
    for (int i = 0; i < 101; ++i)
    {
        y[i] = i/50.0 - 1;
        x[i] = (y[i]+0.8)*y[i]*(y[i]-0.8);
    }

    // 新建QCPGraph对象,并设置绘图数据以及 alreadySorted = true
    customPlot->addGraph();
    customP
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇MFC中使用多线程 下一篇C++面试八股文:用过std::set/std..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目