1.Qwt库

QwtPlot是用来绘制二维图像的widget。在它的画板上可以无限制的显示绘画组件。绘画组件可以是曲线(QwtPlotCurve)、标记(QwtPlotMarker)、网格(QwtPlotGrid)、或者其它从QwtPlotItem继承的组件。

2.简单介绍原文链接,原作者辛苦........

QwtPlot拥有4个axes(轴线):

yLeft 
Y axis left of the canvas.
yRight  Y axis right of the canvas.
xBottom  X axis below the canvas.
xTop  X axis above the canvas.

常用函数接口:

setAxisTitle 设置轴标题
enableAxis 主要是显示xTop,yRight坐标轴
setAxisMaxMajor 设置某个某个坐标轴扩大比例尺的最大间隔数目
setAxisMaxMinor 设置某个某个坐标轴缩小比例尺的最大间隔数目
setAxisScale 禁用自动缩放比例尺,为某个坐标轴指定一个修改的比例尺
insertLegend 添加图例(标注)

常用组件:

QwtPlotCurve 曲线  可以用曲线来完成散点图等等......
QwtPlotMarker 标记
QwtPlotGrid 网格
QwtPlotHistogram 直方图
other 从QwtPlotItem继承的组件
 
QwtPlotItem plot能显示的类,如果想要实现自己绘画图形,要继承此类实现rtti和draw接口
QwtPlotPanner 平移器    (用鼠标左键平移)
QwtPlotMagnifier  放大器    (用鼠标滚轮缩放)
QwtPlotCanvas 画布
QwtScaleMap 比例图---可以提供一个逻辑区域到实际区域的坐标转换
QwtScaleWidget 比例窗口
QwtScaleDiv 比例布局
QwtLegent 标注
QwtPlotLayout 布局管理器
QwtScaleDraw 自画坐标轴
 
常见接口:
setPen 设置画笔
setData 设置曲线的数据
setStyle 设置曲线形式,点、直线、虚线等等
setCurveAttribute 设置曲线属性,一般设置Fitted
attch 把曲线附加到QwlPlot上
 
下面看一个小例子,结果如下:
 
 
源代码:
  1. #include <QtGui/QApplication>
  2. #include <Qt/qmath.h>
  3. #include <QVector>
  4. #include <qwt_plot.h>
  5. #include <qwt_plot_curve.h>
  6. #include <qwt_plot_magnifier.h>
  7. #include <qwt_plot_panner.h>
  8. #include <qwt_legend.h>
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12. QApplication a(argc, argv);
  13.  
  14. QwtPlot plot(QwtText("CppQwtExample1"));
  15. plot.resize(640,400);
  16.  
  17. plot.setAxisTitle(QwtPlot::xBottom, "x->"); //设置坐标轴的名称
  18. plot.setAxisTitle(QwtPlot::yLeft, "y->");
  19. plot.setAxisScale(QwtPlot::xBottom, 0.0, 2.0 * M_PI); //设置坐标轴的范围
  20. plot.setAxisScale(QwtPlot::yLeft, -1.0, 1.0);
  21. plot.insertLegend(new QwtLegend(), QwtPlot::RightLegend); //设置右边标注
  22. (void) new QwtPlotMagnifier( plot.canvas() ); //使用滚轮放大/缩小
  23. (void) new QwtPlotPanner( plot.canvas() ); //使用鼠标左键平移
  24.  
  25. //计算曲线数据
  26. QVector<double> xs;
  27. QVector<double> ys;
  28. for (double x = 0; x < 2.0 * M_PI; x+=(M_PI / 10.0)) {
  29. xs.append(x);
  30. ys.append(qSin(x));
  31. }
  32. //构造曲线数据
  33. QwtPointArrayData * const data = new QwtPointArrayData(xs, ys);
  34. QwtPlotCurve curve("Sine");
  35. curve.setData(data);//设置数据
  36. curve.setStyle(QwtPlotCurve::Lines);//直线形式
  37. curve.setCurveAttribute(QwtPlotCurve::Fitted, true);//是曲线更光滑
  38. curve.setPen(QPen(Qt::blue));//设置画笔
  39.  
  40. curve.attach(&plot);//把曲线附加到plot上
  41. plot.show();
  42. return a.exec();
  43. }

3.使用QWT绘制科学图表、绘图

解释QwtSimple:simple是qwt自带的例子中最简单的一个, 一共只有一百来行的代码, 实现了数学中的正弦函数(sin())和余弦函数(cos())曲线。


4.我使用到的两种情况


4.1 对函数进行画图----曲线图(这个功能很别致的!)

  1. 包含文件:
  2.  
  3. #include <qlayout.h>
  4. #include <qwt_plot.h>
  5. #include <qwt_plot_marker.h>
  6. #include <qwt_plot_curve.h>
  7. #include <qwt_legend.h>
  8. #include <qwt_series_data.h>
  9. #include <qwt_plot_canvas.h>
  10. #include <qwt_plot_panner.h>
  11. #include <qwt_plot_magnifier.h>
  12. #include <qwt_text.h>
  13. #include <qwt_math.h>
  14. #include <math.h>
  15.  
  16. 把连续函数定义为全局函数,输入/返回值为double
  17.  
  18. double funcMy( doublef)
  19. {
  20. return 0.15*f;
  21. }
  22.  
  23. 定义序列数据;
  24. class CArraySeriesDat:publicQwtSyntheticPointData
  25. {
  26. public:
  27. CArraySeriesDat(double(*y)(double)):
  28. QwtSyntheticPointData(100),d_y(y
  29. { }
  30.  
  31. virtual double y(double x) const
  32. { return d_y(x); }
  33.  
  34. private:
  35. double(*d_y)(double);
  36.  
  37. };
  38.  
  39. 自定义画图类:
  40.  
  41. class Plot:publicQwtPlot
  42. {
  43. public:
  44. Plot( QWidget *parent = NULL);
  45. ~Plot( );
  46. void drawArray();//画图函数
  47. protected:
  48. virtual void resizeEvent( QResizeEvent * );
  49. private:
  50. void populateCos();
  51. void updateGradient();
  52. };
  53.  
  54. 画图类初始化:
  55.  
  56. Plot::Plot(QWidget*parent):
  57. QwtPlot( parent )
  58. {
  59. // panning with the left mouse button
  60. (void) new QwtPlotPanner( canvas() );
  61. // zoom in/out with the wheel
  62. (void) new QwtPlotMagnifier( canvas() );
  63. setAutoFillBackground( true );
  64. setPalette( QPalette( QColor( 165, 193, 228 ) ) );
  65. updateGradient();
  66. setTitle("A Simple QwtPlot Demonstration");
  67. insertLegend(new QwtLegend(), QwtPlot::RightLegend);
  68. // axes
  69. setAxisTitle(xBottom, "x -->" );
  70. setAxisScale(xBottom, 0.0, 10.0);
  71. setAxisTitle(yLeft, "y -->");
  72. setAxisScale(yLeft, -1.0, 1.0);
  73. // canvas
  74. canvas()->setLineWidth( 1 );
  75. canvas()->setFrameStyle( QFrame::Box | QFrame::Plain );
  76. canvas()->setBorderRadius( 15 );
  77. QPalette canvasPalette( Qt::white );
  78. canvasPalette.setColor( QPalette::Foreground, QColor( 133, 190, 232 ) );
  79. canvas()->setPalette( canvasPalette );
  80. }
  81.  
  82. 私有成员函数实现:
  83.  
  84. void Plot::updateGradient()
  85. {
  86. QPalette pal = palette();
  87. const QColor buttonColor = pal.color( QPalette::Button );
  88. #ifdef Q_WS_X11
  89. // Qt 4.7.1: QGradient::StretchToDeviceMode is buggy on X11
  90. QLinearGradient gradient( rect().topLeft(), rect().bottomLeft() );
  91. gradient.setColorAt( 0.0, Qt::white );
  92. gradient.setColorAt( 0.7, buttonColor );
  93. gradient.setColorAt( 1.0, buttonColor );
  94.  
  95. #else
  96. QLinearGradient gradient( 0, 0, 0, 1 );
  97. gradient.setCoordinateMode( QGradient::StretchToDeviceMode );
  98. gradient.setColorAt( 0.0, Qt::white );
  99. gradient.setColorAt( 0.7, buttonColor );
  100. gradient.setColorAt( 1.0, buttonColor );
  101.  
  102. #endif
  103. pal.setBrush( QPalette::Window, gradient );
  104. setPalette( pal );
  105. }
  106.  
  107. void Plot::resizeEvent( QResizeEvent *event )
  108. {
  109. QwtPlot::resizeEvent( event );
  110. #ifdef Q_WS_X11
  111. updateGradient();
  112. #endif
  113. }
  114.  
  115. 画图函数实现:
  116.  
  117. void Plot::drawArray()
  118. {
  119. // Insert new curves
  120. QwtPlotCurve *cSin = new QwtPlotCurve("y = sin(x)");
  121. cSin->setRenderHint(QwtPlotItem::RenderAntialiased);
  122. cSin->setLegendAttribute(QwtPlotCurve::LegendShowLine, true);
  123. cSin->setPen(QPen(Qt::red));
  124. cSin->attach(this);
  125. QwtPlotCurve *cCos = new QwtPlotCurve("y = cos(x)");
  126. cCos->setRenderHint(QwtPlotItem::RenderAntialiased);
  127. cCos->setLegendAttribute(QwtPlotCurve::LegendShowLine, true);
  128. cCos->setPen(QPen(Qt::blue));
  129. cCos->attach(this);
  130. CArraySeriesDat* ArraySeriesDat = new CArraySeriesDat(funcMy); //以函数指针的形式获取曲线
  131. cCos->setData(ArraySeriesDat);
  132. return;
  133.  
  134. }
  135.  
  136. 函数测试:
  137.  
  138. int main(intargc,char**argv)
  139. {
  140. QApplication a(argc, argv);
  141. Plot *plot = new Plot();
  142. // We put a dummy widget around to have
  143. // so that Qt paints a widget background
  144. // when resizing
  145. QWidget window;
  146. QHBoxLayout *layout = new QHBoxLayout( &window );
  147. layout->setContentsMargins( 0, 0, 0, 0 );
  148. layout->addWidget( plot );
  149. plot->drawArray();//画图...........
  150. window.resize(600,400);
  151. window.show();
  152. return a.exec();
  153. }

  1.  

4.2 散点图(基本功能)

头文件:

  1. class CCruvePlot:publicQwtPlot
  2. {
  3. public:
  4. CCruvePlot();
  5. ~CCruvePlot(void);
  6.  
  7. public:
  8. void drawPlotCruve();
  9. private:
  10. QwtPlotCurve * curve;
  11. QVector<double> xData;
  12. QVector<double> yData;
  13. };
  14.  
  15. //实现文件:
  16. #include "cruvePlot.h"
  17. const int LineNum=7;
  18. const int PointNum=7;
  19. CCruvePlot::CCruvePlot(){}
  20. CCruvePlot::~CCruvePlot(void){}
  21.  
  22. void CCruvePlot::drawPlotCruve()
  23. {
  24. //QMessageBox::information(this,"Running!","Running matlab Function.....");
  25. setTitle("A Simple QwtPlot Demonstration");//设置标题
  26. insertLegend(new QwtLegend(), QwtPlot::RightLegend);//设置标线的栏
  27. setAxisTitle(xBottom, "x -->");
  28. setAxisScale(xBottom, 0.0, 10.0);
  29. setAxisTitle(yLeft, "y -->");
  30. setAxisScale(yLeft, 0, 10.0);
  31.  
  32. QwtPlotCurve *curve = new QwtPlotCurve("lineFirst");//实例化一条曲线
  33. curve->attach(this);
  34. double *x=new double[PointNum];
  35. double *y=new double[PointNum];
  36. for(int i=0;i<PointNum;i++) {
  37. x[i]=i;
  38. y[i]=i+3;
  39. }
  40.  
  41. curve->setSamples (x,y,PointNum);//传画曲线的数据
  42. curve->setPen(QPen(Qt::red));
  43. QwtPlotCurve *curve2 = new QwtPlotCurve("lineSecond");//实例化另一条线
  44. curve2->attach(this);
  45. double *x2=new double[PointNum];
  46. double *y2=new double[PointNum];
  47. for(int i=0;i<PointNum;i++){
  48. x2[i]=i*3;
  49. y2[i]=i+3;
  50. }
  51.  
  52. curve2->setSamples (x2,y2,PointNum);
  53. curve2->setPen(QPen(Qt::blue));
  54. return;
  55.  
  56. }

4.3 对于QwtSymbol的使用,详细参考此博客:http://blog.csdn.net/qustdjx/article/details/7940896

为什么不封装成类似Matlab的用法呢?

Qwt--散点图/函数图的更多相关文章

  1. MATLAB绘制函数图

    序言 Matlab可以根据用户给出的数据绘制相应的函数图.对于单个2D函数图,需要给出一个行向量x作为函数图上离散点集的横坐标,以及一个与x列数一样的横坐标y作为函数图上点集的纵坐标. 向量x和y的取 ...

  2. 用Html5制作的一款数学教学程序Function Graphics(绘制函数图的程序)

    最近我不仅对游戏开发感兴趣,还对函数图感兴趣,特此我开发了这个程序.以下是一些介绍和下载演示地址,喜欢的朋友可以看看: 一,产品名片 产品名:Function Graphics 版本: 0.1 开发者 ...

  3. JupyterLab绘制:柱状图,饼状图,直方图,散点图,折线图

    JupyterLab绘图 喜欢python的同学,可以到 https://v3u.cn/(刘悦的技术博客) 里面去看看,爬虫,数据库,flask,Django,机器学习,前端知识点,JavaScrip ...

  4. 06. Matplotlib 2 |折线图| 柱状图| 堆叠图| 面积图| 填图| 饼图| 直方图| 散点图| 极坐标| 图箱型图

    1.基本图表绘制 plt.plot() 图表类别:线形图.柱状图.密度图,以横纵坐标两个维度为主同时可延展出多种其他图表样式 plt.plot(kind='line', ax=None, figsiz ...

  5. 使用 NumPy 和 Matplotlib 绘制函数图

    Numpy是用python进行科学计算的基本程序包. 它主要包含以下功能: ♦强大的n维数组对象 ♦复杂(广播)函数工具 ♦用于集成c/c++和Fortran代码-有用的线性代数 ♦傅里叶变换和随机数 ...

  6. pyhton matplotlib可视化图像基础(二维函数图、柱状图、饼图、直方图以及折线图)

    //2019.07.22pyhton中matplotlib模块的应用pyhton中matplotlib是可视化图像库的第三方库,它可以实现图像的可视化,输出不同形式的图形1.可视化图形的输出和展示需要 ...

  7. 使用matplotlib库绘制函数图

    函数如下: z = x^2 * y / (x^4 +y^2) 代码如下: import numpy as np import matplotlib.pyplot as plt import mpl_t ...

  8. 函数图象(N=x+y*i)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. C语言绘制余弦函数图象

    #include"stdio.h" #include"math.h" void main() { double y; int x,m; for(y=1;y> ...

随机推荐

  1. selenium使用Xpath+CSS+JavaScript+jQuery的定位方法(治疗selenium各种定位不到,点击不了的并发症)

    跟你说,你总是靠那个firebug,chrome的F12啥的右击复制xpath绝对总有一天踩着地雷炸的你死活定位不到,这个时候就需要自己学会动手写xpath,人脑总比电脑聪明,开始把xpath语法给我 ...

  2. python之testlink模块

    1.安装:pip install TestLink-API-Python-client >>>>>>待续

  3. javascript 数组 常用方法

    前言  学学忘忘  闲来做个笔记 整理下数组常用方法. Array 数组常用方法  创建数组的基本方式有两种    1.第一种是使用Array构造函数,  var arr = new Array(); ...

  4. UDP、线程、mutex锁(day15)

    一.基于UDP的网络编程模型 服务器端 .创建socket. .将fd和服务器的ip地址和端口号绑定 .recvfrom阻塞等待接收客户端数据 .业务处理 .响应客户端 客户端: .创建socket ...

  5. 彻底弄清楚session是什么?

    搬运自博主xueqinna的CSDN博客,根据自己的理解修改了部分内容,介意者请前往原博主博客看原文. 今天就来彻底的学一下session是个啥,作者罗列了几个要点:1.session 是啥?2.怎么 ...

  6. mysql如何删除数据库指定ID段的数据库。比如删除id 1-500的数据。

    delete from tablename where id>=1 and id<=500或者DELETE FROM `数据库名称`.`数据表名称` WHERE `house_cs`.`i ...

  7. luogu P4512 多项式除法 (模板题、FFT、多项式求逆)

    手动博客搬家: 本文发表于20181206 14:42:53, 原地址https://blog.csdn.net/suncongbo/article/details/84853342 题目链接: ht ...

  8. Nexus私服的搭建

    1.nexus 介绍     是开源的,用该框架架设maven私有服务器   2.nexus私服环境搭建     把nexus.war包放到tomcat的webapps下面     浏览且登录     ...

  9. 1010针对一个binlog日志的分析

    针对一个BINLOG日志的分析 -- 当前binlog_format | ROW[root@109 mysql]# cat wang1010.txt/*!50530 SET @@SESSION.PSE ...

  10. HDU 3432

    水题,就是把一个矩形平分. 题意:一个wid*hei的矩形,过底边上的一点(dor,0)做m-1条射线,把这个矩形的面积平均分成m份,求这些射线和矩形的另外一个交点. 直接枚举,然而求三角形高底移动坐 ...