QCustomPlot一、QCustomPlot基础及画图显示

本文详细介绍了如何下载QCustomPlot源码,包括不同版本,以及如何在Qt项目中集成和使用它进行绘图。内容涵盖了添加曲线、设置坐标轴、命名和调整范围,以及高级特性如颜色、线型和填充色的设置。
摘要由CSDN通过智能技术生成

1、QCustomPlot下载

QCustomPlot源码demo
根据需要选择需要的文件:
完整版。QCustomPlot.tar.gz 源代码+例子+帮助文档;
共享库。QCustomPlot-sharedlib.tar.gz 库编译和使用;
源代码。QCustomPlot-source.tar.gz 源代码
里面包含了很多QCustomplot的demo

2、创建项目并添加QCustomPlot相关文件

1、创建了一个空的工程
在这里插入图片描述
2、将cpp和h文件添加进工程
在这里插入图片描述在这里插入图片描述
3、编译报错解决
在这里插入图片描述
使用的是QT5,需要加上printsupport模块,方法是:QT +=printsupport
在这里插入图片描述
这样就编译完成了

4、ui文件增加QWidget并提升成QCustomPlot

添加QWidget
在这里插入图片描述
将QWideget提升为QCustomPlot
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述
提升完成后,就可以进行画图了

3、QCustomPlot画图

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <cmath>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QVector<double> x,y;//创建QVector,保存x和y的值
    //设置x和y的值
    for(double xi=-2*M_PI;xi<2*M_PI;xi+=0.1)
    {
        x.push_back(xi);
        y.push_back(sin(xi));
    }

    this->ui->widget->addGraph(0);  //新建一条曲线
    this->ui->widget->graph(0)->setData(x,y);//绘制曲线
}

MainWindow::~MainWindow()
{
    delete ui;
}

在这里插入图片描述
这样一个图就画出来了

4、QCustomPlot

1、QCPGraph曲线,上面的曲线就是QCPGraph
QCPGraph表示一个曲线,常见的方法有:

  • QCustomPlot::addGraph 增加一个曲线(一个曲线就需要手动addGraph);
  • QCustomPlot::graph 获得曲线实例;
  • setData/addData 设置/增加曲线数据;
  • setName 设置曲线名称,会在Legend显示的时候用到;
  • rescaleAxes 根据当前数据调整轴范围;
  • data 返回一个指向曲线数据的指针,可用于直接修改数据;

2、QCPAxisRect (矩形)轴
表示轴系统上的所有设置,如刻度,范围等。
QCustomPlot默认构造了四个轴,分别位于

  • 上(QCustomPlot::xAxis2)
  • 下(QCustomPlot::xAxis)
  • 左(QCustomPlot::yAxis)
  • 右(QCustomPlot::yAxis2)
    四个方位,默认显示左下两个轴,左(QCustomPlot::yAxis),下(QCustomPlot::xAxis)。
    设置轴上的表现:
    在这里插入图片描述在这里插入图片描述

3、QCPLegend 图线

QCustomPlot默认有成员QCustomPlot::legend实例(调用setVisible(true)显示),一般不需要自己构造QCPLengend,通过这个类你可以控制曲线说明的大小、颜色等属性

5、改进画图,显示坐标轴名称,设置轴的范围,显示曲线名称

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <cmath>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QVector<double> x,y;   //创建QVector,用来保存x轴和y轴的值
    //设置保存x轴和y轴的值
    for(double xi=-2*M_PI;xi<2*M_PI;xi+=0.1)
    {
        x.push_back(xi);
        y.push_back(sin(xi));
    }
    this->ui->widget->addGraph(0);  //新增一个曲线
    this->ui->widget->graph(0)->setData(x,y);//设置曲线的值,绘制曲线
    this->ui->widget->graph(0)->setName("y=sin(x)");//设置曲线名称
    this->ui->widget->rescaleAxes(true);  //根据当前数据自动设置轴的范围
    this->ui->widget->xAxis->setLabel("X下");  //设置轴的名称
    this->ui->widget->yAxis->setLabel("Y左");  //设置轴的名称
    this->ui->widget->xAxis2->setLabel("X上");  //设置轴的名称,默认不显示
    this->ui->widget->yAxis2->setLabel("Y右");  //设置轴的名称,默认不显示
    this->ui->widget->legend->setVisible(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}


在这里插入图片描述
QCustomPlot本身可以设置交互行为,如放大,缩小移动,选择曲线交互,方法是:customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);

6、改变曲线的颜色、线宽和线型并在曲线与坐标轴之间增加填充颜色

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <cmath>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QVector<double> x,y;   //创建QVector,用来保存x轴和y轴的值
    //设置保存x轴和y轴的值
    for(double xi=-2*M_PI;xi<2*M_PI;xi+=0.1)
    {
        x.push_back(xi);
        y.push_back(sin(xi));
    }
    this->ui->widget->addGraph(0);  //新增一个曲线
    this->ui->widget->graph(0)->setData(x,y);//设置曲线的值,绘制曲线
    this->ui->widget->graph(0)->setName("y=sin(x)");//设置曲线名称
    this->ui->widget->rescaleAxes(true);  //根据当前数据自动设置轴的范围
    this->ui->widget->xAxis->setLabel("X下");  //设置轴的名称
    this->ui->widget->yAxis->setLabel("Y左");  //设置轴的名称
    this->ui->widget->xAxis2->setLabel("X上");  //设置轴的名称,默认不显示
    this->ui->widget->yAxis2->setLabel("Y右");  //设置轴的名称,默认不显示
    this->ui->widget->legend->setVisible(true);

    //增加部分
    QPen pen;
    pen.setColor(Qt::red);  //设置曲线颜色为红色
    pen.setStyle(Qt::DotLine);//设置曲线为虚线
    pen.setWidth(3);//设置线宽
    this->ui->widget->graph(0)->setPen(pen);  //设置曲线使用QPen绘制
    this->ui->widget->graph(0)->setBrush(QBrush(QColor(255,50,30,20)));//使用QBrush给曲线之间填充颜色
}

MainWindow::~MainWindow()
{
    delete ui;
}

在这里插入图片描述

7、多个曲线、带状填充

  • 增加一个余弦曲线
  • 设置曲线间填充

增加多个曲线只需要在画布上调用addGraph方法,两个曲线设置方法完全一样,不过是通过graph序号来区分他们的属性设置。
为了显示坐标之间围成的区域,setChannelFillGraph(QCPGraph)将会被使用,将从调用曲线开始到指定曲线围成的面积

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <cmath>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QVector<double> x,ys,yc;

    for(double xi=-2*M_PI;xi<2*M_PI;xi+=0.1)
    {
        x.push_back(xi);
        ys.push_back(sin(xi));
        yc.push_back(cos(xi));
    }

    QPen pen;  //用来绘制的画笔类,QT中的类,可以设置颜色,虚线等
    pen.setColor(Qt::red);  //设置画笔为红色
    this->ui->widget->addGraph(0);  //增加一条曲线,序号为0
    this->ui->widget->graph(0)->setData(x,ys);  //设置数据,用来绘制曲线
    this->ui->widget->graph(0)->setName("y=sin(x)");  //设置曲线名称
    this->ui->widget->graph(0)->setPen(pen); //使用QT的画笔进行绘图
    this->ui->widget->rescaleAxes(true);  //根据当前数据自动设置轴的范围,Qt自动设置轴的范围

    pen.setColor(Qt::blue);
    this->ui->widget->addGraph();  //新增一条曲线,自动增加一个序号
    this->ui->widget->graph(1)->setData(x,yc);  //序号1曲线设置数据,用来绘制曲线
    this->ui->widget->graph(1)->setName("y=cos(x)"); //设置曲线名称
    this->ui->widget->graph(1)->setPen(pen); //使用QT的画笔进行绘图
    this->ui->widget->rescaleAxes(true); //根据当前数据自动设置轴的范围,Qt自动设置轴的范围

    this->ui->widget->graph(0)->setBrush(QBrush(QColor(255,50,30,20)));  //使用QBrush给曲线之间填充颜色
    this->ui->widget->graph(0)->setChannelFillGraph( this->ui->widget->graph(1));  //设置为两条曲线之间

    this->ui->widget->xAxis->setLabel("X");  //设置轴的名称
    this->ui->widget->yAxis->setLabel("Y");  //设置轴的名称
    this->ui->widget->legend->setVisible(true);

}


MainWindow::~MainWindow()
{
    delete ui;
}


在这里插入图片描述

8、控制轴相关属性

数字格式和精度
我们可以通过void QCPAxis::setNumberFormat ( const QString & formatCode)设置轴数字属性。formatCode是一个字符串,

  • 第一个参数和QString::number()关于Format的规定一致:
FormatMeaning
eformat as [-]9.9e[+]
Eformat as [-]9.9E[+]
fformat as [-]9.9
guse e or f format, whichever is the most concise
Guse E or f format, whichever is the most concise
  • 第二个参数和第三个参数都是QCustomPlot的参数,它们是可选的。
    其中第二个参数是为了让第一个参数的科学计数法更加beatiful的,用b表示。乘法默认情况下是使用居中的dot表示,如果你想要用 × 表示,那么你可以用字符c(cross)表示。

轴颜色

  • 通过QPen来设置

标签

  • 字体 setLabelFonts

刻度

  • setTickLabelColor 刻度颜色
  • setTickLabelRotation 倾斜度
  • setTickLength 主刻度长度
  • setSubTicks 次刻度
  • setSubTickLength 次刻度长度
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <cmath>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
#if 0
    QVector<double> x,ys,yc;

    for(double xi=-2*M_PI;xi<2*M_PI;xi+=0.1)
    {
        x.push_back(xi);
        ys.push_back(sin(xi));
        yc.push_back(cos(xi));
    }

    QPen pen;  //用来绘制的画笔类,QT中的类,可以设置颜色,虚线等
    pen.setColor(Qt::red);  //设置画笔为红色
    this->ui->widget->addGraph(0);  //增加一条曲线,序号为0
    this->ui->widget->graph(0)->setData(x,ys);  //设置数据,用来绘制曲线
    this->ui->widget->graph(0)->setName("y=sin(x)");  //设置曲线名称
    this->ui->widget->graph(0)->setPen(pen); //使用QT的画笔进行绘图
    this->ui->widget->rescaleAxes(true);  //根据当前数据自动设置轴的范围,Qt自动设置轴的范围

    pen.setColor(Qt::blue);
    this->ui->widget->addGraph();  //新增一条曲线,自动增加一个序号
    this->ui->widget->graph(1)->setData(x,yc);  //序号1曲线设置数据,用来绘制曲线
    this->ui->widget->graph(1)->setName("y=cos(x)"); //设置曲线名称
    this->ui->widget->graph(1)->setPen(pen); //使用QT的画笔进行绘图
    this->ui->widget->rescaleAxes(true); //根据当前数据自动设置轴的范围,Qt自动设置轴的范围

    this->ui->widget->graph(0)->setBrush(QBrush(QColor(255,50,30,20)));  //使用QBrush给曲线之间填充颜色
    this->ui->widget->graph(0)->setChannelFillGraph( this->ui->widget->graph(1));  //设置为两条曲线之间

    this->ui->widget->xAxis->setLabel("X");  //设置轴的名称
    this->ui->widget->yAxis->setLabel("Y");  //设置轴的名称
    this->ui->widget->legend->setVisible(true);
#endif

    // generate some data:
        std::vector<double> t;
        std::generate_n(std::back_inserter(t),2000,[n=-1000.0]()mutable {return (n++)*0.004;});
        std::vector<double> y1,y2;
        std::transform(t.begin(),t.end(),std::back_inserter(y1),[](double ele){return sin(ele);});
        std::transform(t.begin(),t.end(),std::back_inserter(y2),[](double ele){return cos(ele);});

        QPen pen;
        pen.setStyle(Qt::DotLine);  设置画笔风格
        ui->widget->addGraph();
        ui->widget->graph(0)->setName("y=sin(x)");
        ui->widget->graph(0)->setPen(QPen(Qt::red));
        ui->widget->graph(0)->setData(QVector<double>(t.begin(),t.end()),QVector<double>(y1.begin(),y1.end()));

        ui->widget->addGraph();
        ui->widget->graph(1)->setPen(QPen(Qt::blue));
        ui->widget->graph(1)->setName("y=cos(x)");
        ui->widget->graph(1)->setData(QVector<double>(t.begin(),t.end()),QVector<double>(y2.begin(),y2.end()));


        ui->widget->graph(0)->setBrush(QBrush(QColor(255,50,30,20)));
        ui->widget->graph(0)->setChannelFillGraph(ui->widget->graph(1));
        ui->widget->legend->removeItem(ui->widget->legend->itemCount()-1); // don't show two confidence band graphs in legend


        // give the axes some labels:
        ui->widget->xAxis->setLabel("x");
        ui->widget->xAxis->setSubTicks(true);
        ui->widget->xAxis->setTickLength(50);
        ui->widget->xAxis->setSubTickLength(30);
        ui->widget->xAxis->setTickLabelColor(Qt::darkGray);
        ui->widget->xAxis->setTickLabelRotation(30);
        ui->widget->xAxis->setNumberFormat("eb");
        ui->widget->yAxis->setNumberPrecision(1);
        ui->widget->yAxis->setLabel("y");

        // set axes ranges, so we see all data:
        ui->widget->xAxis->setRange(-3.14/2, 3.14);
        ui->widget->yAxis->setRange(0, 1);

        ui->widget->legend->setVisible(true);
}


MainWindow::~MainWindow()
{
    delete ui;
}

  • 10
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QCustomPlot中,要画封闭曲线,可以使用QCPCurve类。首先,你需要创建一个QCPCurve对象,并将其添加到QCustomPlot中的一个图层中。然后,使用QCPCurve的setData方法设置曲线的数据点。最后,调用QCPCurve的setClosed方法将曲线设置为封闭的。以下是一个示例代码: ```cpp // 创建一个QCPCurve对象 QCPCurve *curve = new QCPCurve(customPlot->xAxis, customPlot->yAxis); // 将曲线添加到图层中 customPlot->addPlottable(curve); // 设置曲线的数据点 QVector<QCPCurveData> data; // 添加数据点到data中 // ... // 设置曲线的数据 curve->setData(data); // 将曲线设置为封闭的 curve->setClosed(true); // 更新图表 customPlot->replot(); ``` 请注意,你需要根据你的具体需求修改代码中的数据点和其他参数。 #### 引用[.reference_title] - *1* [QCustomPlot之平滑曲线下(九)](https://blog.csdn.net/qq10097355/article/details/104890734)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [QTQCustomPlot绘制曲线图](https://blog.csdn.net/qq_39154864/article/details/124477945)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [QT绘图用QCustomPlot库,简单画一个曲线图,涉及了QCustomPlot的最基础知识(画图必用到的属性)](https://blog.csdn.net/m0_49456900/article/details/125071836)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值