QCustomPlot使用手册

QCustomPlot使用手册一、基本画图首先,给个简单的例子:[cpp] viewplain copy print?// 生成数据,画出的是抛物线  QVectordouble> x(101), y(101); // initialize with entries 0..100  for (int i=0; i{    x[i] = i/50.

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

一、基本画图

首先,给个简单的例子:

[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. // 生成数据,画出的是抛物线  
  2. QVector<double> x(101), y(101); // initialize with entries 0..100  
  3. for (int i=0; i<101; ++i)  
  4. {  
  5.   x[i] = i/50.0 – 1; // x goes from -1 to 1  
  6.   y[i] = x[i]*x[i]; // let’s plot a quadratic function  
  7. }  
  8. // 添加数据曲线(一个图像可以有多个数据曲线)  
  9. customPlot->addGraph();  
[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. // graph(0);可以获取某个数据曲线(按添加先后排序)  
  2. // setData();为数据曲线关联数据  
  3. customPlot->graph(0)->setData(x, y);  
  4. // 为坐标轴添加标签  
  5. customPlot->xAxis->setLabel(“x”);  
  6. customPlot->yAxis->setLabel(“y”);  
  7. // 设置坐标轴的范围,以看到所有数据  
  8. customPlot->xAxis->setRange(-1, 1);  
  9. customPlot->yAxis->setRange(0, 1);  
[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. // 重画图像  
  2. customPlot->replot();  



上面代码生成的结果大致是这样的:

QCustomPlot使用手册

[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. 外观  
[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. QCustomPlot的外观由很多方面特性组成,都可以改变:  
[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. 坐标轴:  
[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. QCustomPlot有四个QCPAxis成员变量,分别代表四个坐标轴:xAxis(下)yAxis(左)xAxis2(上)yAxis2(右)  
[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. QCPAxis有相应的函数可以设置坐标轴的刻度、间距、范围等:  
[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. setTickStep(double step);//设置刻度间距  
  2. setTickVector(const QVector<double> &vec);//将坐标轴刻度设置为vec  
  3. setAutoTickStep(bool on);//设置是否自动分配刻度间距  
  4. setAutoTicks(bool on);//设置是否自动分配刻度  
  5. setAutoTickCount(int approximateCount);//设置是否自动分配刻度数量  
[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. 还有setBasePen、setTickPen、setTickLength、setSubTickLength、setSubTickPen、setTickLabelFont、setLabelFont、setTickLabelPadding、setLabelPadding、setRangeReversed等  
[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. 等后面专门讲QCPAxis的时候再详细介绍  

曲线风格:

[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. QCPGraph::setPen(const QPen &pen);  

曲线画笔:

[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. QCPGraph::setLineStyle(LineStyle ls);  


可以设置颜色、宽度、实虚等

曲线形状:

[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. QCPGraph::setScatterStyle(QCPScatterStyle &style);  

曲线形状像*、+、x、o等等

填充曲线方式:

[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. QCPGraph::setBrush(const QBrush &brush);  
[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. QCPGraph::setChannelFillGraph(otherGraph);//设置与某之间曲线填充  
[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. QCPGraph::setBrush(Qt::NoBrush);//移除填充  

以上会等到专门将QCPGraph和QCPScatterStyle类的时候再细讲网格:

[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. customPlot->yAxis->grid();  
[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. setPen、setZeroLinePen、setSubGridVisible等  
[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. 等讲QCPGrid类再细讲  

二、高级画图

[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. 1、多曲线与多风格  
[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. <pre name=“code” class=“cpp”>customPlot->setLocale(QLocale(QLocale::English, QLocale::UnitedKingdom)); // period as decimal separator and comma as thousand separator  
  2. customPlot->legend->setVisible(true);  
  3. QFont legendFont = font();  // start out with MainWindow’s font..  
  4. legendFont.setPointSize(9); // and make a bit smaller for legend  
  5. customPlot->legend->setFont(legendFont);  
  6. customPlot->legend->setBrush(QBrush(QColor(255,255,255,230)));  
  7. // by default, the legend is in the inset layout of the main axis rect. So this is how we access it to change legend placement:  
  8. customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignBottom|Qt::AlignRight);  
  9.    
  10. // setup for graph 0: key axis left, value axis bottom  
  11. // will contain left maxwell-like function  
  12. customPlot->addGraph(customPlot->yAxis, customPlot->xAxis);  
  13. customPlot->graph(0)->setPen(QPen(QColor(255, 100, 0)));  
  14. customPlot->graph(0)->setBrush(QBrush(QPixmap(“./dali.png”))); // fill with texture of specified png-image  
  15. customPlot->graph(0)->setLineStyle(QCPGraph::lsLine);  
  16. customPlot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 5));  
  17. customPlot->graph(0)->setName(“Left maxwell function”);  
  18.    
  19. // setup for graph 1: key axis bottom, value axis left (those are the default axes)  
  20. // will contain bottom maxwell-like function  
  21. customPlot->addGraph();  
  22. customPlot->graph(1)->setPen(QPen(Qt::red));  
  23. customPlot->graph(1)->setBrush(QBrush(QPixmap(“./dali.png”))); // same fill as we used for graph 0  
  24. customPlot->graph(1)->setLineStyle(QCPGraph::lsStepCenter);  
  25. customPlot->graph(1)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, Qt::red, Qt::white, 7));  
  26. customPlot->graph(1)->setErrorType(QCPGraph::etValue);  
  27. customPlot->graph(1)->setName(“Bottom maxwell function”);  
  28.    
  29. // setup for graph 2: key axis top, value axis right  
  30. // will contain high frequency sine with low frequency beating:  
  31. customPlot->addGraph(customPlot->xAxis2, customPlot->yAxis2);  
  32. customPlot->graph(2)->setPen(QPen(Qt::blue));  
  33. customPlot->graph(2)->setName(“High frequency sine”);  
  34.    
  35. // setup for graph 3: same axes as graph 2  
  36. // will contain low frequency beating envelope of graph 2  
  37. customPlot->addGraph(customPlot->xAxis2, customPlot->yAxis2);  
  38. QPen blueDotPen;  
  39. blueDotPen.setColor(QColor(30, 40, 255, 150));  
  40. blueDotPen.setStyle(Qt::DotLine);  
  41. blueDotPen.setWidthF(4);  
  42. customPlot->graph(3)->setPen(blueDotPen);  
  43. customPlot->graph(3)->setName(“Sine envelope”);  
  44.    
  45. // setup for graph 4: key axis right, value axis top  
  46. // will contain parabolically distributed data points with some random perturbance  
  47. customPlot->addGraph(customPlot->yAxis2, customPlot->xAxis2);  
  48. customPlot->graph(4)->setPen(QColor(50, 50, 50, 255));  
  49. customPlot->graph(4)->setLineStyle(QCPGraph::lsNone);  
  50. customPlot->graph(4)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 4));  
  51. customPlot->graph(4)->setName(“Some random data around\na quadratic function”);  
  52.    
  53. // generate data, just playing with numbers, not much to learn here:  
  54. QVector<double> x0(25), y0(25);  
  55. QVector<double> x1(15), y1(15), y1err(15);  
  56. QVector<double> x2(250), y2(250);  
  57. QVector<double> x3(250), y3(250);  
  58. QVector<double> x4(250), y4(250);  
  59. for (int i=0; i<25; ++i) // data for graph 0  
  60. {  
  61.   x0[i] = 3*i/25.0;  
  62.   y0[i] = exp(-x0[i]*x0[i]*0.8)*(x0[i]*x0[i]+x0[i]);  
  63. }  
  64. for (int i=0; i<15; ++i) // data for graph 1  
  65. {  
  66.   x1[i] = 3*i/15.0;;  
  67.   y1[i] = exp(-x1[i]*x1[i])*(x1[i]*x1[i])*2.6;  
  68.   y1err[i] = y1[i]*0.25;  
  69. }  
  70. for (int i=0; i<250; ++i) // data for graphs 2, 3 and 4  
  71. {  
  72.   x2[i] = i/250.0*3*M_PI;  
  73.   x3[i] = x2[i];  
  74.   x4[i] = i/250.0*100-50;  
  75.   y2[i] = sin(x2[i]*12)*cos(x2[i])*10;  
  76.   y3[i] = cos(x3[i])*10;  
  77.   y4[i] = 0.01*x4[i]*x4[i] + 1.5*(rand()/(double)RAND_MAX-0.5) + 1.5*M_PI;  
  78. }  
  79.    
  80. // pass data points to graphs:  
  81. customPlot->graph(0)->setData(x0, y0);  
  82. customPlot->graph(1)->setDataValueError(x1, y1, y1err);  
  83. customPlot->graph(2)->setData(x2, y2);  
  84. customPlot->graph(3)->setData(x3, y3);  
  85. customPlot->graph(4)->setData(x4, y4);  
  86. // activate top and right axes, which are invisible by default:  
  87. customPlot->xAxis2->setVisible(true);  
  88. customPlot->yAxis2->setVisible(true);  
  89. // set ranges appropriate to show data:  
  90. customPlot->xAxis->setRange(0, 2.7);  
  91. customPlot->yAxis->setRange(0, 2.6);  
  92. customPlot->xAxis2->setRange(0, 3.0*M_PI);  
  93. customPlot->yAxis2->setRange(-70, 35);  
  94. // set pi ticks on top axis:  
  95. QVector<double> piTicks;  
  96. QVector<QString> piLabels;  
  97. piTicks << 0  << 0.5*M_PI << M_PI << 1.5*M_PI << 2*M_PI << 2.5*M_PI << 3*M_PI;  
  98. piLabels << “0” << QString::fromUtf8(“½π”) << QString::fromUtf8(“π”) << QString::fromUtf8(“1½π”) << QString::fromUtf8(“2π”) << QString::fromUtf8(“2½π”) << QString::fromUtf8(“3π”);  
  99. customPlot->xAxis2->setAutoTicks(false);  
  100. customPlot->xAxis2->setAutoTickLabels(false);  
  101. customPlot->xAxis2->setTickVector(piTicks);  
  102. customPlot->xAxis2->setTickVectorLabels(piLabels);  
  103. // add title layout element:  
  104. customPlot->plotLayout()->insertRow(0);  
  105. customPlot->plotLayout()->addElement(0, 0, new QCPPlotTitle(customPlot, “Way too many graphs in one plot”));  
  106. // set labels:  
  107. customPlot->xAxis->setLabel(“Bottom axis with outward ticks”);  
  108. customPlot->yAxis->setLabel(“Left axis label”);  
  109. customPlot->xAxis2->setLabel(“Top axis label”);  
  110. customPlot->yAxis2->setLabel(“Right axis label”);  
  111. // make ticks on bottom axis go outward:  
  112. customPlot->xAxis->setTickLength(0, 5);  
  113. customPlot->xAxis->setSubTickLength(0, 3);  
  114. // make ticks on right axis go inward and outward:  
  115. customPlot->yAxis2->setTickLength(3, 3);  
  116. customPlot->yAxis2->setSubTickLength(1, 1);  

效果图:

QCustomPlot使用手册2、日期和时间数据曲线

[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. // set locale to english, so we get english month names:  
  2.   customPlot->setLocale(QLocale(QLocale::English, QLocale::UnitedKingdom));  
  3.   // seconds of current time, we’ll use it as starting point in time for data:  
  4.   double now = QDateTime::currentDateTime().toTime_t();  
  5.   srand(8); // set the random seed, so we always get the same random data  
  6.   // create multiple graphs:  
  7.   for (int gi=0; gi<5; ++gi)  
  8.   {  
  9.     customPlot->addGraph();  
  10.     QPen pen;  
  11.     pen.setColor(QColor(0, 0, 255, 200));  
  12.     customPlot->graph()->setLineStyle(QCPGraph::lsLine);  
  13.     customPlot->graph()->setPen(pen);  
  14.     customPlot->graph()->setBrush(QBrush(QColor(255/4.0*gi,160,50,150)));  
  15.     // generate random walk data:  
  16.     QVector<double> time(250), value(250);  
  17.     for (int i=0; i<250; ++i)  
  18.     {  
  19.       time[i] = now + 24*3600*i;  
  20.       if (i == 0)  
  21.         value[i] = (i/50.0+1)*(rand()/(double)RAND_MAX-0.5);  
  22.       else  
  23.         value[i] = fabs(value[i-1])*(1+0.02/4.0*(4-gi)) + (i/50.0+1)*(rand()/(double)RAND_MAX-0.5);  
  24.     }  
  25.     customPlot->graph()->setData(time, value);  
  26.   }  
  27.   // configure bottom axis to show date and time instead of number:  
  28.   customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);  
  29.   customPlot->xAxis->setDateTimeFormat(“MMMM\nyyyy”);  
  30.   // set a more compact font size for bottom and left axis tick labels:  
  31.   customPlot->xAxis->setTickLabelFont(QFont(QFont().family(), 8));  
  32.   customPlot->yAxis->setTickLabelFont(QFont(QFont().family(), 8));  
  33.   // set a fixed tick-step to one tick per month:  
  34.   customPlot->xAxis->setAutoTickStep(false);  
  35.   customPlot->xAxis->setTickStep(2628000); // one month in seconds  
  36.   customPlot->xAxis->setSubTickCount(3);  
  37.   // apply manual tick and tick label for left axis:  
  38.   customPlot->yAxis->setAutoTicks(false);  
  39.   customPlot->yAxis->setAutoTickLabels(false);  
  40.   customPlot->yAxis->setTickVector(QVector<double>() << 5 << 55);  
  41.   customPlot->yAxis->setTickVectorLabels(QVector<QString>() << “Not so\nhigh” << “Very\nhigh”);  
  42.   // set axis labels:  
  43.   customPlot->xAxis->setLabel(“Date”);  
  44.   customPlot->yAxis->setLabel(“Random wobbly lines value”);  
  45.   // make top and right axes visible but without ticks and labels:  
  46.   customPlot->xAxis2->setVisible(true);  
  47.   customPlot->yAxis2->setVisible(true);  
  48.   customPlot->xAxis2->setTicks(false);  
  49.   customPlot->yAxis2->setTicks(false);  
  50.   customPlot->xAxis2->setTickLabels(false);  
  51.   customPlot->yAxis2->setTickLabels(false);  
  52.   // set axis ranges to show all data:  
  53.   customPlot->xAxis->setRange(now, now+24*3600*249);  
  54.   customPlot->yAxis->setRange(0, 60);  
  55.   // show legend:  
  56.   customPlot->legend->setVisible(true);  



效果图:

QCustomPlot使用手册

三、曲线、柱形图、统计图…

到目前为止,我们为图像添加曲线都是使用

[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. QCustomPlot::addGraph();  
  2. QCustomPlot::graph();  

其实,除了
QCPGraph ,QCustomPlot 还提供了多个画图类:

QCPCurve:与QCPGraph 类似,差别在于它是用于展示参数化曲线,可以有循环。

QCPBars:柱形图,如果有多个QCPBars ,可以依次重叠。

QCPStatisticalBox、QCPColorMap、QCPFinancial
QCPGraph 不同的是,这些画图类在添加到QCustomPlot 的时候需要使用new创建一个实例,而不能直接

[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. <span style=“color: rgb(53, 53, 53); font-family: monospace; line-height: 19.5px; background-color: rgb(240, 240, 240);”>addPlottable</span>();  

简单例子如下:

[cpp] 
view plain  
copy

 
print
?
在CODE上查看代码片
派生到我的代码片

  1. QCPBars *myBars = new QCPBars(customPlot->xAxis, customPlot->yAxis);  
  2. customPlot->addPlottable(myBars);  
  3. // now we can modify properties of myBars:  
  4. myBars->setName(“Bars Series 1”);  
  5. QVector<double> keyData;  
  6. QVector<double> valueData;  
  7. keyData << 1 << 2 << 3;  
  8. valueData << 2 << 4 << 8;  
  9. myBars->setData(keyData, valueData);  
  10. customPlot->rescaleAxes();  
  11. customPlot->replot();  

好吧,这篇就到这里。水平有限,如有出错,敬请指出,互相学习。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/181167.html原文链接:https://javaforall.net

(0)
上一篇 2022年10月16日 下午11:46
下一篇 2022年10月16日 下午11:46


相关推荐

  • pytest skipif_pytest失败重跑

    pytest skipif_pytest失败重跑前言pytest.mark.skip可以标记无法在某些平台上运行的测试功能,或者您希望失败的测试功能Skip和xfail:处理那些不会成功的测试用例你可以对那些在某些特定平台上不能运行的测试用

    2022年7月31日
    6
  • Spark Streaming详解(重点窗口计算)

    Spark Streaming详解(重点窗口计算)前面有几篇关于SparkStreaming的博客,那会只是作为Spark入门,快速体验Spark之用,只是照着葫芦画瓢。本文结合Spark官网上SparkStreaming的编程指南对SparkStreaming进行介绍StreamingContext如同SparkContext一样,StreamingContext也是SparkStreaming应用程序通往Spark集群的通道,它的定义…

    2022年6月26日
    36
  • 1100000/1011模二除法_四位数除以两位数的除法算式

    1100000/1011模二除法_四位数除以两位数的除法算式原题链接这里所谓的“光棍”,并不是指单身汪啦~ 说的是全部由1组成的数字,比如1、11、111、1111等。传说任何一个光棍都能被一个不以5结尾的奇数整除。比如,111111就可以被13整除。 现在,你的程序要读入一个整数x,这个整数一定是奇数并且不以5结尾。然后,经过计算,输出两个数字:第一个数字s,表示x乘以s是一个光棍,第二个数字n是这个光棍的位数。这样的解当然不是唯一的,题目要求你输出最小的解。提示:一个显然的办法是逐渐增加光棍的位数,直到可以整除x为止。但难点在于,s可能是个非常大的数 ——

    2022年8月9日
    10
  • 腾讯混元发布并开源原生多模态生图模型“混元图像3.0

    腾讯混元发布并开源原生多模态生图模型“混元图像3.0

    2026年3月13日
    3
  • gfortran在linux下安装

    gfortran在linux下安装在 http gfortran meteodat ch download x86 64 nightlies gcc trunk tar xz 下载 gfortran nbsp wgethttp gfortran meteodat ch download x86 64 nightlies gcc trunk tar xz nbsp nbsp xz dgcc trunk tar xztar

    2026年3月17日
    2
  • mysql中exists的用法详解[通俗易懂]

    mysql中exists的用法详解[通俗易懂]前言在日常开发中,用mysql进行查询的时候,有一个比较少见的关键词exists,我们今天来学习了解一下这个exists这个sql关键词的用法,这样在工作中遇到一些特定的业务场景就可以有更加多样化的解决方案语法解释语法SELECTcolumn1FROMt1WHERE[conditions]andEXISTS(SELECT*FROMt2);说明括号中的子查询并不会返回具体的查询到的数据,只是会返回true或者false,如果外层sql的字段在子查询中存在则返回true,

    2025年8月12日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号