Qt QCustomPlot 绘制子轴

这篇具有很好参考价值的文章主要介绍了Qt QCustomPlot 绘制子轴。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

抄大神杰作:QCustomplot(五)QCPAxisRect进行子绘图-CSDN博客文章浏览阅读5.9k次,点赞7次,收藏60次。文中介绍了QCustomPlot 子绘图需要掌握的类,也就是Matlab中的subplot,最后给出了一个完整的例子。_qcpaxisrecthttps://blog.csdn.net/weixin_39258979/article/details/122008568

需求来源:试验数据需要多轴对比。之前有过初步尝试​​​​​​​还是大佬们写的好。

实现多Y轴、单X轴、X轴是时间轴、X轴range联动、rect之间的间距是0,每个图上有legend(这里有个疑问,每添加个rect在这个rect上添加graph,再添加legend,第一个rect上就有多个legend,其他rect上就只有一个。);Qt QCustomPlot 绘制子轴,信息可视化

实现多Y轴、多X轴,x轴不联动。

Qt QCustomPlot 绘制子轴,信息可视化

频谱图,多Y轴,单X轴

Qt QCustomPlot 绘制子轴,信息可视化

关键代码如下,请大佬们多多指正:文章来源地址https://www.toymoban.com/news/detail-808551.html

//时域图
void MultiAxisWidget::recvRawData(int iRow, QString oStrLabel, QVector<double> adX, QVector<double> adY)
{
    QCPAxisRect* poAxisRect = new QCPAxisRect(ui->plot);
    poAxisRect->setAutoMargins(QCP::msNone);
    poAxisRect->setMargins(QMargins(100, 0, 0, 0));

    ui->plot->plotLayout()->addElement(iCntIndex, 0, poAxisRect);

    QCPAxis* poAxisX = poAxisRect->axis(QCPAxis::atBottom);
    QCPAxis* poAxisY = poAxisRect->axis(QCPAxis::atLeft);

    poAxisX->setVisible(false);

    poAxisY->grid()->setZeroLinePen(QPen(Qt::red));

    poAxisX->grid()->setSubGridVisible(true);
    poAxisY->grid()->setSubGridVisible(true);

    QList<QCPAxis*> aaxisField;
    aaxisField << poAxisX;
    poAxisRect->setRangeZoomAxes(aaxisField);

    poAxisY->setVisible(true);
    poAxisY->setLabel("振幅(mV)");

    auto poGraph = ui->plot->addGraph(poAxisX, poAxisY);

    poGraph->setName(oStrLabel);
    poGraph->setData(adX, adY);
    poGraph->setLineStyle(QCPGraph::lsLine);
    poGraph->setPen(QPen(Qt::blue, 2));

    poGraph->rescaleKeyAxis(true);
    poGraph->rescaleValueAxis(true, true);
    poGraph->rescaleAxes();

    auto poThread = new QThread;
    poGraph->setParent(nullptr);
    poGraph->moveToThread(poThread);
    poGraph->setVisible(true);

    QPair<QCPAxisRect*, QCPGraph*> pairContent(poAxisRect, poGraph) ;

    aopContent.append(pairContent);

    ++iCntIndex;

    QSharedPointer<QCPAxisTickerDateTime> dateTicker(new QCPAxisTickerDateTime);//日期做X轴
    dateTicker->setDateTimeFormat("HH:mm:ss");//日期格式(可参考QDateTime::fromString()函数)
    dateTicker->setTickCount(12);

    switch(eAxisType)
    {
        case AXIS_X_M:
            poAxisRect->setAutoMargins(QCP::msBottom);
            poAxisX->setVisible(true);
            poAxisX->setTicker(dateTicker);//设置X轴为时间轴

            break;

        case AXIS_X_S:

            if(iCntIndex == iCnt)
            {
                poAxisRect->setAutoMargins(QCP::msBottom);
                poAxisX->setVisible(true);
                poAxisX->setTicker(dateTicker);//设置X轴为时间轴

                QTimer::singleShot(1000, this, [ = ]()
                {
                    this->linkage();
                });
            }
            break;
    }

    if(iCntIndex == iCnt)
    {
        for(QPair<QCPAxisRect*, QCPGraph*> pairContent : aopContent)
        {
            QCPLegend* poLegend = new QCPLegend;
            pairContent.first->insetLayout()->addElement(poLegend, Qt::AlignTop | Qt::AlignRight);
            poLegend->setLayer("legend");
            poLegend->addItem(new QCPPlottableLegendItem(poLegend, pairContent.second));
        }
    }

    ui->plot->replot();
}

//频谱图
void MultiAxisWidget::recvSpectrum(QString oStrLabel, QVector<double> x, QVector<double> y, QList<double> adTargetF)
{
    QCPAxisRect* poAxisRect = new QCPAxisRect(ui->plot);
    poAxisRect->setAutoMargins(QCP::msNone);
    poAxisRect->setMargins(QMargins(100, 0, 0, 0));

    ui->plot->plotLayout()->addElement(iCntIndex, 0, poAxisRect);

    QCPAxis* poAxisX = poAxisRect->axis(QCPAxis::atBottom);
    QCPAxis* poAxisY = poAxisRect->axis(QCPAxis::atLeft);

    poAxisX->setVisible(false);

    QList<QCPAxis*> aaxisField;
    aaxisField << poAxisX;
    poAxisRect->setRangeZoomAxes(aaxisField);

    poAxisX->setLabel("频率(Hz)");          //X轴文字显示
    poAxisY->setLabel("振幅(mV)");          //Y轴文字显示

    poAxisY->grid()->setZeroLinePen(QPen(Qt::red));

    poAxisX->setRangeReversed(true);
    poAxisX->setScaleType(QCPAxis::stLogarithmic);
    poAxisY->setScaleType(QCPAxis::stLogarithmic);

    ui->plot->xAxis->grid()->setSubGridVisible(true);
    poAxisY->grid()->setSubGridVisible(true);

    auto poGraph = ui->plot->addGraph(poAxisX, poAxisY);

    poGraph->setName(oStrLabel);
    poGraph->setData(x, y);
    poGraph->setLineStyle(QCPGraph::lsLine);
    poGraph->setPen(QPen(Qt::blue, 2));

    poGraph->rescaleKeyAxis(true);
    poGraph->rescaleValueAxis(true, true);
    poGraph->rescaleAxes();

    auto poThread = new QThread;
    poGraph->setParent(nullptr);
    poGraph->moveToThread(poThread);
    poGraph->setVisible(true);

    for(int i = 0; i < adTargetF.count(); i++)
    {
        QCPItemTracer* poTracer = new QCPItemTracer(ui->plot);

        poTracer->setGraphKey(adTargetF.at(i));
        poTracer->setInterpolating(false);
        poTracer->setStyle(QCPItemTracer::tsCircle);
        poTracer->setPen(QPen(Qt::yellow));
        poTracer->setBrush(Qt::red);
        poTracer->position->setType(QCPItemPosition::ptPlotCoords);
        poTracer->setSize(8);

        poTracer->setClipAxisRect(poAxisRect);//设置裁剪的坐标轴
        poTracer->setGraph(poGraph);

        QCPItemStraightLine* poLine = new QCPItemStraightLine(ui->plot);

        poLine->setPen(QPen(Qt::red, 0.5, Qt::DotLine));
        //垂直参考线,就是两点一线
        //m_pHorReffer_DG->setClipToAxisRect(false);//裁剪,让外部也要看到
        poLine->setClipAxisRect(poAxisRect);//设置裁剪的坐标轴
        poLine->point1->setAxes(poAxisRect->axis(QCPAxis::atBottom), poAxisRect->axis(QCPAxis::atLeft)); //绑定坐标
        poLine->point2->setAxes(poAxisRect->axis(QCPAxis::atBottom), poAxisRect->axis(QCPAxis::atLeft));

        poLine->point1->setCoords(adTargetF.at(i), 1);
        poLine->point2->setCoords(adTargetF.at(i), 2);
    }

    QPair<QCPAxisRect*, QCPGraph*> pairContent(poAxisRect, poGraph) ;

    aopContent.append(pairContent);

    ++iCntIndex;

    if(iCntIndex == iCnt)
    {
        poAxisRect->setAutoMargins(QCP::msBottom);

        QSharedPointer<QCPAxisTickerText> textTicker = QSharedPointer<QCPAxisTickerText>(new QCPAxisTickerText);
        textTicker.data()->clear();

        foreach(double dF, adTargetF)
        {
            textTicker->addTick(dF, QString("%1").arg(dF));
        }

        poAxisX->setTicker(textTicker);

        poAxisX->setTickLabelRotation(34.38);

        poAxisX->setVisible(true);

        QTimer::singleShot(1000, this, [ = ]()
        {
            this->linkage();
        });

        for(QPair<QCPAxisRect*, QCPGraph*> pairContent : aopContent)
        {
            QCPLegend* poLegend = new QCPLegend;
            pairContent.first->insetLayout()->addElement(poLegend, Qt::AlignTop | Qt::AlignRight);
            poLegend->setLayer("legend");
            poLegend->addItem(new QCPPlottableLegendItem(poLegend, pairContent.second));
        }
    }

    ui->plot->replot();
}

//多  x轴联动
void MultiAxisWidget::linkage()
{
    for(QPair<QCPAxisRect*, QCPGraph*> pairContent : aopContent)
    {
        for(QPair<QCPAxisRect*, QCPGraph*> pairContentOther : aopContent)
        {
            if(pairContentOther.first == pairContent.first)
            {
                continue;
            }
            connect(pairContent.first->axis(QCPAxis::atBottom), QOverload<const QCPRange&>::of(&QCPAxis::rangeChanged),
                    pairContentOther.first->axis(QCPAxis::atBottom), QOverload<const QCPRange&>::of(&QCPAxis::setRange));
        }
    }

    //鼠标双击曲线,在颜色对话框中给被点击的曲线设置颜色
    connect(ui->plot, &QCustomPlot::plottableDoubleClick, this, [ = ](QCPAbstractPlottable * plottable, int dataIndex, QMouseEvent * event)
    {
        QColorDialog colorDialog;
        colorDialog.setCurrentColor(Qt::red);

        // 显示对话框
        int result = colorDialog.exec();

        // 检查用户是否选择了颜色
        if (result == QDialog::Accepted)
        {
            // 获取选择的颜色
            QColor selectedColor = colorDialog.selectedColor();

            plottable->setPen(QPen(selectedColor, 2));

            ui->plot->replot();
        }
    });

    ui->plot->replot();
}

到了这里,关于Qt QCustomPlot 绘制子轴的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Python数据可视化(三)绘制统计图形大全

    以 Python 代码的形式讲解柱状图的绘制原理,这里重点讲解 bar()函数的使用方法。 代码: 运行结果: 为了展示图表里的中文字体,我们选择字体“SimHei”, 通 过 “mpl.rcParams[\\\"font.sans-serif\\\"] =[\\\"SimHei\\\"]”完成字体配置任务。不使用默认的“Unicode minus”模式来处理坐标轴轴线的刻

    2024年02月02日
    浏览(36)
  • pyecharts绘制各种数据可视化图表案例(效果+代码)

    1、pyecharts绘制饼图(显示百分比) 2、pyecharts绘制柱状图 3、pyecharts绘制折线图 4、pyecharts绘制柱形折线组合图 5、pyecharts绘制散点图 6、pyecharts绘制玫瑰图 7、pyecharts绘制词云图 8、pyecharts绘制雷达图 9、pyecharts绘制散点图 10、pyecharts绘制嵌套饼图 11、pyecharts绘制中国地图 12、

    2024年02月09日
    浏览(33)
  • 数据可视化——绘制带有时间线的柱状图

    我们已经学习了使用 pyecharts 包中的模块和相应的方法绘制了折线图和地图,那么今天我将为大家分享如何绘制带有时间线的柱状图。 绘制柱状图跟绘制折线图的步骤是大致相同的。 python 中绘制柱状图依赖于 pyecharts.charts 模块下的 Bar 方法。 我们还可以将横坐标与纵坐标颠

    2024年02月16日
    浏览(32)
  • 折线的可视化及不规则柱体的绘制

    开发环境: Windows 11 家庭中文版 Microsoft Visual Studio Community 2019 VTK-9.3.0.rc0 vtk-example demo解决问题 : 1.绘制一条多段线(折现),并可视化这段折现;2.根据折现绘制一个不规则柱体 关键点 : vtkRotationalExtrusionFilter是Visualization Toolkit(VTK)中的一个过滤器,用于沿着输入曲线生成

    2024年01月25日
    浏览(31)
  • 数据可视化python,绘制饼图,代码和解析

    使用matplotlib.pyplot.pie绘制 (1),该函数的定义如下: matplotlib.pyplot.pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None, radius=None, counterclock=True, wedgeprops=None, textprops=None, center=(0, 0), frame=False, rotatelabels=False, hold=None, data=None) 参数说明:

    2024年02月06日
    浏览(38)
  • 【数据可视化】动手用matplotlib绘制关联规则网络图

    下载文中数据、代码、绘图结果 如果想知道本文的关联规则数据是怎么来的,请阅读这篇文章 Python中似乎没有很方便的绘制网络图的函数。 下面是本人自行实现的绘图函数,如果想要运行,请点击上文的链接,下载数据和代码。 传入一个关联规则数据的DataFrame,这个DataF

    2024年03月11日
    浏览(73)
  • Streamlit 讲解专栏(十一):数据可视化-图表绘制详解(中)

    在上一篇博文《 Streamlit 讲解专栏(十):数据可视化-图表绘制详解(上)》中,我们学习了一些关于数据可视化的基础知识,探索了Streamlit库中的几个常见图表绘制函数,包括st.line_chart、st.area_chart、st.bar_chart和st.pyplot。通过这些函数,我们可以轻松地绘制不同类型的图表

    2024年02月10日
    浏览(36)
  • Streamlit 讲解专栏(十二):数据可视化-图表绘制详解(下)

    数据可视化在数据分析和数据科学领域中扮演着至关重要的角色。通过可视化数据,我们能够更好地理解其背后的模式和趋势,从而作出准确的决策和预测。然而,要将原始数据转化为有意义的图表并不容易。这就是为什么我们需要强大而灵活的工具来帮助我们实现这一目标

    2024年02月09日
    浏览(29)
  • Streamlit 讲解专栏(十):数据可视化-图表绘制详解(上)

    在数据可视化的世界中,绘制清晰、易于理解的图表是非常关键的。Streamlit 是一个流行的 Python 库,它提供了简单的界面和强大的功能,帮助用户轻松创建交互式应用程序和数据可视化。而其中的 Chart elements(图表元素)部分则为我们提供了多种图表类型来展示数据。 本文将

    2024年02月12日
    浏览(26)
  • 数据可视化——用python绘制简单的折线图

    前面我们已经学习了python的基础语法和面向对象,那么接下来我们将学习python编程语言的过人之处——数据的可视化之折线图。 说到数据可视化,我们需要先知道什么是JSON。 json是一种轻量级的数据交互格式,可以按照json指定的格式去组织和封装数据 json本质上是一个带有特

    2024年02月17日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包