龙空技术网

Qt 读取csv文件并且绘制折线图

程序员老舅 439

前言:

今天朋友们对“用python处理csv数据绘制折线图”大体比较关注,看官们都需要知道一些“用python处理csv数据绘制折线图”的相关知识。那么小编在网络上汇集了一些关于“用python处理csv数据绘制折线图””的相关内容,希望同学们能喜欢,大家快快来了解一下吧!

直接上代码:

 1 void MainWindow::readcsvfile() //读取csv 2 { 3  QFile csvFile("C:/Users/Administrator/Desktop/Demo/0702.CSV"); 4  QStringList csvList; 5  csvList.clear(); 6  if (csvFile.open(QIODevice::ReadWrite)) //对csv文件进行读写操作 7  { 8   QTextStream stream(&csvFile); 9   while (!stream.atEnd())10  {11  csvList.push_back(stream.readLine()); //保存到List当中12  }13  csvFile.close();14  }15  else16  {17      QMessageBox::about(NULL, "csv文件", "未打开该文件!");18  }19    int i = 0;20    Q_FOREACH(QString str, csvList)   //遍历List21   {22    i = i + 1;23    QStringList valsplit = str.split(","); //分隔字符串24    if(i > 2)25    {26     //得到深度、声速、温度27     QString depth = valsplit[0];28     QString sonicvelocity = valsplit[1];29     QString temperature = valsplit[2];30     double depthvalue = depth.toDouble();31     double sonicvalue = sonicvelocity.toDouble();32     double tempvalue = temperature.toDouble();33     //Q//MessageBox::warning(NULL, "dd", QString::number(tempvalue));34     QPointF point;35     point.setX(depthvalue);36     point.setY(sonicvalue);37     QPointF point2;38     point2.setX(depthvalue);39     point2.setY(tempvalue + 1510);40     vectors.append(point);41     vector2.append(point2);42    }43   }44 }45  46 void MainWindow::lineChart() //绘制图47 {48     //设置X,Y标题49     ui->qwtPlot->setAxisTitle(QwtPlot::xBottom, QString::fromLocal8Bit("深度(m)"));50     ui->qwtPlot->setAxisTitle(QwtPlot::yLeft, QString::fromLocal8Bit("声速(m/s)"));51     ui->qwtPlot->setAxisTitle(QwtPlot::yRight, QString::fromLocal8Bit("温度(°C)"));52     ui->qwtPlot->enableAxis(QwtPlot::yRight,true);53     ui->qwtPlot->setAxisScale(QwtPlot::yLeft,1538,1540,0.2);54     ui->qwtPlot->setAxisScale(QwtPlot::xBottom,0,30,2);55     ui->qwtPlot->setAxisScale(QwtPlot::yRight,28,30,0.2);56  57     //ui->qwtPlot->set58     //构造曲线数据59      QwtPointSeriesData* series = new QwtPointSeriesData(vectors);60      //设置网格61      QwtPlotGrid* grid = new QwtPlotGrid();62      grid->setPen(QColor(222, 222, 222), 1);63      grid->attach(ui->qwtPlot);64      //create plot item65      QwtPlotCurve* curve1 = new QwtPlotCurve(QString::fromLocal8Bit("声速"));66      //设置数据67      curve1->setData(series);68      //设置画笔颜色==就是图像颜色69      curve1->setPen(QColor(255, 0, 0), 2, Qt::SolidLine);70      //使曲线更光滑71      curve1->setCurveAttribute(QwtPlotCurve::Fitted, true);72      //把曲线附加到qwtPlot上73      curve1->attach(ui->qwtPlot);74      //添加温度-深度曲线75      //构造曲线数据76      QwtPointSeriesData* series2 = new QwtPointSeriesData(vector2);77      //create plot item78      QwtPlotCurve* curve2 = new QwtPlotCurve(QString::fromLocal8Bit("温度"));79      //设置数据80      curve2->setData(series2);81      //设置画笔颜色=图像颜色82      curve2->setPen(QColor(127, 222, 335), 2, Qt::SolidLine);83      //使曲线更光滑84      curve2->setCurveAttribute(QwtPlotCurve::Fitted, true);85      //把曲线附加到qwtPlot上86      curve2->attach(ui->qwtPlot);87  88      //设置图例89      QwtLegend *legend = new QwtLegend;90      legend->setDefaultItemMode(QwtLegendData::ReadOnly);91      ui->qwtPlot->insertLegend(legend,QwtPlot::BottomLegend);//插入图例92      ui->qwtPlot->replot();93      ui->qwtPlot->show();94 }

需要第三方库QWT

【领QT开发教程学习资料,点击下方链接莬费领取↓↓,先码住不迷路~】

点击→领取「链接」

运行结果:

标签: #用python处理csv数据绘制折线图