QCustomPlot多线同X轴不同Y轴

#include "waveform.h"
#include "qcustomplot.h"  
#include <QVector>  



WaveForm::WaveForm(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    this->resize(1550,800);
    
    


    //定义图像对象
    QCustomPlot* customPlot = new QCustomPlot(this);
    //设置图像位置和大小
    customPlot->setGeometry(1, 1, 1540, 350);

   


    // 添加五条线  
    QCPGraph* A = customPlot->addGraph();
    QCPGraph* B = customPlot->addGraph();
    QCPGraph* C = customPlot->addGraph();
    QCPGraph* D = customPlot->addGraph();
    QCPGraph* E = customPlot->addGraph();

    

    // 假设您已经有了一个QCustomPlot实例,名为customPlot  
    
    //AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    QCPAxis* yAxisA = customPlot->axisRect()->addAxis(QCPAxis::atLeft);
    yAxisA->setLabel("Y Axis A Label"); // 设置轴的标签文本  
    //yAxisA->setVisible(false);  //设置坐标轴不显示(默认显示)

    // 设置轴的刻度范围等其他属性  
    yAxisA->setRange(0, 10);

    // 设置Y轴标签的可见性和字体等(如果需要)  
    yAxisA->setLabelFont(QFont(QFont().family(), 10)); // 例如,设置字体大小和家族  
    yAxisA->setLabelColor(Qt::red); // 设置标签颜色  

    // 设置曲线Y坐标轴
    A->setValueAxis(yAxisA);

          //设置折线颜色
    A->setPen(QPen(Qt::red));
    A->setVisible(true);




    //BBBBBBBBBBBBBBBBBBBBBBBBBB
    QCPAxis* yAxisB = customPlot->axisRect()->addAxis(QCPAxis::atLeft);
    yAxisB->setLabel("Y Axis B Label"); // 设置轴的标签文本  
    //yAxisB->setVisible(false);  //设置坐标轴不显示(默认显示)

    // 设置轴的刻度范围等其他属性  
    yAxisB->setRange(0, 100);

    // 设置Y轴标签的可见性和字体等(如果需要)  
    yAxisB->setLabelFont(QFont(QFont().family(), 10)); // 例如,设置字体大小和家族  
    yAxisB->setLabelColor(Qt::green); // 设置标签颜色  

    // 设置曲线Y坐标轴
    B->setValueAxis(yAxisB);

    //设置折线颜色
    B->setPen(QPen(Qt::green));
    B->setVisible(true);


    //CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
    QCPAxis* yAxisC = customPlot->axisRect()->addAxis(QCPAxis::atLeft);
    yAxisC->setLabel("Y Axis C Label"); // 设置轴的标签文本  
    //yAxisC->setVisible(false);  //设置坐标轴不显示(默认显示)

    // 设置轴的刻度范围等其他属性  
    yAxisC->setRange(0, 100);

    // 设置Y轴标签的可见性和字体等(如果需要)  
    yAxisC->setLabelFont(QFont(QFont().family(), 10)); // 例如,设置字体大小和家族  
    yAxisC->setLabelColor(Qt::yellow); // 设置标签颜色  

    // 设置曲线Y坐标轴
    C->setValueAxis(yAxisC);
    //设置折线颜色
    C->setPen(QPen(Qt::yellow));
    C->setVisible(true);


    //DDDDDDDDDDDDDDDDDDDDDDDDDDDD
    QCPAxis* yAxisD = customPlot->axisRect()->addAxis(QCPAxis::atLeft);
    yAxisD->setLabel("Y Axis D Label"); // 设置轴的标签文本  
    //yAxisD->setVisible(false);  //设置坐标轴不显示(默认显示)

    // 设置轴的刻度范围等其他属性  
    yAxisD->setRange(0, 100);

    // 设置Y轴标签的可见性和字体等(如果需要)  
    yAxisD->setLabelFont(QFont(QFont().family(), 10)); // 例如,设置字体大小和家族  
    yAxisD->setLabelColor(Qt::black); // 设置标签颜色  

    // 设置曲线Y坐标轴
    D->setValueAxis(yAxisD);

    //设置折线颜色
    D->setPen(QPen(Qt::black));
    D->setVisible(true);



    //EEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
    QCPAxis* yAxisE = customPlot->axisRect()->addAxis(QCPAxis::atLeft);
    yAxisE->setLabel("Y Axis E Label"); // 设置轴的标签文本  
    //yAxisE->setVisible(false);  //设置坐标轴不显示(默认显示)

    // 设置轴的刻度范围等其他属性  
    yAxisE->setRange(0, 100);

    // 设置Y轴标签的可见性和字体等(如果需要)  
    yAxisE->setLabelFont(QFont(QFont().family(), 10)); // 例如,设置字体大小和家族  
    yAxisE->setLabelColor(Qt::blue); // 设置标签颜色  
    
    // 设置曲线Y坐标轴
    E->setValueAxis(yAxisE);

    //设置折线颜色
    E->setPen(QPen(Qt::blue));
    E->setVisible(true);



    

    // 模拟添加一些数据  
    QVector<double> x(100), yA(100), yB(100), yC(100), yD(100), yE(100);
    for (int i = 0; i < 100; ++i) {
        x[i] = i / 50.0 - 1;
        yA[i] = qSin(x[i]) * 5 + 5;  // A线数据  
        yB[i] = qCos(x[i]) * 5 + 150; // B线数据  
        yC[i] = qSin(x[i] / 0.5) * 5 + 250; // C线数据  
        yD[i] = qCos(x[i] / 0.3) * 5 + 350; // D线数据  
        yE[i] = qSin(x[i] * 2) * 5 + 450;  // E线数据  
    }
    A->setData(x, yA);
    B->setData(x, yB);
    C->setData(x, yC);
    D->setData(x, yD);
    E->setData(x, yE);

    // 根据需要调整图表布局和其他设置  
    customPlot->rescaleAxes();
    customPlot->replot();

}

WaveForm::~WaveForm()
{}


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
qcustomplot是一个功能强大的开源C++绘图库,可用于绘制各种类型的图表。要实现在x上绘制一条线和多条具有不同刻度的y轴,可以按照以下步骤操作: 1. 使用QCustomPlot库,创建一个QCustomPlot对象,用于绘图。 2. 为xy轴设置刻度范围。可以使用setRange函数来设置x的范围。 3. 创建y轴的刻度,在QCustomPlot对象上调用addGraph函数,创建多个QCPGraph对象。分别为每个y轴设置不同的刻度范围,使用setRange函数来设置。 4. 使用setData函数为每个QCPGraph对象设置要绘制的数据点。可以通过传递x和y坐标数组给setData函数来设置数据点。 5. 根据需要,可以使用setPen函数为每个QCPGraph对象设置不同的线条颜色、样式和宽度。 6. 使用replot函数来执行绘图操作,将所有设置应用于图表。 下面是一个简单的示例代码: ```cpp #include <QApplication> #include "qcustomplot.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); QCustomPlot customPlot; // 设置x范围 customPlot.xAxis->setRange(0, 10); // 创建多个QCPGraph对象,并为每个y轴设置不同的刻度范围 QCPGraph *graph1 = customPlot.addGraph(); graph1->valueAxis()->setRange(-10, 10); QCPGraph *graph2 = customPlot.addGraph(); graph2->valueAxis()->setRange(-20, 20); // 设置数据点 QVector<double> xData = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; QVector<double> yData1 = {-5, -4, -3, -2, -1, 0, 1, 2, 3, 4}; QVector<double> yData2 = {-10, -8, -6, -4, -2, 0, 2, 4, 6, 8}; graph1->setData(xData, yData1); graph2->setData(xData, yData2); // 设置线条样式 graph1->setPen(QPen(Qt::red)); graph2->setPen(QPen(Qt::blue)); // 执行绘图操作 customPlot.replot(); customPlot.show(); return a.exec(); } ``` 运行上述代码,将会绘制一条x和两条具有不同刻度的y轴,其中红色的线表示y轴范围为-10到10,蓝色的线表示y轴范围为-20到20。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值