Qwt--散点图/函数图
生活随笔
收集整理的這篇文章主要介紹了
Qwt--散点图/函数图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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上 |
#include <QtGui/QApplication> #include <Qt/qmath.h> #include <QVector> #include <qwt_plot.h> #include <qwt_plot_curve.h> #include <qwt_plot_magnifier.h> #include <qwt_plot_panner.h> #include <qwt_legend.h> int main(int argc, char *argv[]) { QApplication a(argc, argv); QwtPlot plot(QwtText("CppQwtExample1")); plot.resize(640,400); plot.setAxisTitle(QwtPlot::xBottom, "x->"); //設置坐標軸的名稱 plot.setAxisTitle(QwtPlot::yLeft, "y->"); plot.setAxisScale(QwtPlot::xBottom, 0.0, 2.0 * M_PI); //設置坐標軸的范圍 plot.setAxisScale(QwtPlot::yLeft, -1.0, 1.0); plot.insertLegend(new QwtLegend(), QwtPlot::RightLegend); //設置右邊標注 (void) new QwtPlotMagnifier( plot.canvas() ); //使用滾輪放大/縮小 (void) new QwtPlotPanner( plot.canvas() ); //使用鼠標左鍵平移 //計算曲線數據 QVector<double> xs; QVector<double> ys; for (double x = 0; x < 2.0 * M_PI; x+=(M_PI / 10.0)) { xs.append(x); ys.append(qSin(x)); } //構造曲線數據 QwtPointArrayData * const data = new QwtPointArrayData(xs, ys); QwtPlotCurve curve("Sine"); curve.setData(data);//設置數據 curve.setStyle(QwtPlotCurve::Lines);//直線形式 curve.setCurveAttribute(QwtPlotCurve::Fitted, true);//是曲線更光滑 curve.setPen(QPen(Qt::blue));//設置畫筆 curve.attach(&plot);//把曲線附加到plot上 plot.show(); return a.exec(); }
3.使用QWT繪制科學圖表、繪圖
解釋QwtSimple:simple是qwt自帶的例子中最簡單的一個, 一共只有一百來行的代碼, 實現了數學中的正弦函數(sin())和余弦函數(cos())曲線。
4.我使用到的兩種情況
4.1 對函數進行畫圖----曲線圖(這個功能很別致的!)
包含文件:#include <qlayout.h> #include <qwt_plot.h> #include <qwt_plot_marker.h> #include <qwt_plot_curve.h> #include <qwt_legend.h> #include <qwt_series_data.h> #include <qwt_plot_canvas.h> #include <qwt_plot_panner.h> #include <qwt_plot_magnifier.h> #include <qwt_text.h> #include <qwt_math.h> #include <math.h>把連續函數定義為全局函數,輸入/返回值為double:double funcMy( doublef) {return 0.15*f; }定義序列數據; class CArraySeriesDat:publicQwtSyntheticPointData { public:CArraySeriesDat(double(*y)(double)):QwtSyntheticPointData(100),d_y(y){ }virtual double y(double x) const { return d_y(x); }private:double(*d_y)(double);};自定義畫圖類:class Plot:publicQwtPlot { public:Plot( QWidget *parent = NULL);~Plot( );void drawArray();//畫圖函數 protected:virtual void resizeEvent( QResizeEvent * ); private:void populateCos();void updateGradient(); };畫圖類初始化:Plot::Plot(QWidget*parent):QwtPlot( parent ) {// panning with the left mouse button(void) new QwtPlotPanner( canvas() );// zoom in/out with the wheel(void) new QwtPlotMagnifier( canvas() );setAutoFillBackground( true );setPalette( QPalette( QColor( 165, 193, 228 ) ) );updateGradient();setTitle("A Simple QwtPlot Demonstration");insertLegend(new QwtLegend(), QwtPlot::RightLegend);// axessetAxisTitle(xBottom, "x -->" );setAxisScale(xBottom, 0.0, 10.0);setAxisTitle(yLeft, "y -->");setAxisScale(yLeft, -1.0, 1.0);// canvascanvas()->setLineWidth( 1 );canvas()->setFrameStyle( QFrame::Box | QFrame::Plain );canvas()->setBorderRadius( 15 );QPalette canvasPalette( Qt::white );canvasPalette.setColor( QPalette::Foreground, QColor( 133, 190, 232 ) );canvas()->setPalette( canvasPalette ); }私有成員函數實現:void Plot::updateGradient() {QPalette pal = palette();const QColor buttonColor = pal.color( QPalette::Button ); #ifdef Q_WS_X11// Qt 4.7.1: QGradient::StretchToDeviceMode is buggy on X11QLinearGradient gradient( rect().topLeft(), rect().bottomLeft() );gradient.setColorAt( 0.0, Qt::white );gradient.setColorAt( 0.7, buttonColor );gradient.setColorAt( 1.0, buttonColor );#elseQLinearGradient gradient( 0, 0, 0, 1 );gradient.setCoordinateMode( QGradient::StretchToDeviceMode );gradient.setColorAt( 0.0, Qt::white );gradient.setColorAt( 0.7, buttonColor );gradient.setColorAt( 1.0, buttonColor );#endifpal.setBrush( QPalette::Window, gradient );setPalette( pal ); }void Plot::resizeEvent( QResizeEvent *event ) {QwtPlot::resizeEvent( event ); #ifdef Q_WS_X11updateGradient(); #endif }畫圖函數實現:void Plot::drawArray() {// Insert new curvesQwtPlotCurve *cSin = new QwtPlotCurve("y = sin(x)");cSin->setRenderHint(QwtPlotItem::RenderAntialiased);cSin->setLegendAttribute(QwtPlotCurve::LegendShowLine, true);cSin->setPen(QPen(Qt::red));cSin->attach(this);QwtPlotCurve *cCos = new QwtPlotCurve("y = cos(x)");cCos->setRenderHint(QwtPlotItem::RenderAntialiased);cCos->setLegendAttribute(QwtPlotCurve::LegendShowLine, true);cCos->setPen(QPen(Qt::blue));cCos->attach(this);CArraySeriesDat* ArraySeriesDat = new CArraySeriesDat(funcMy); //以函數指針的形式獲取曲線cCos->setData(ArraySeriesDat);return;}函數測試:int main(intargc,char**argv) {QApplication a(argc, argv);Plot *plot = new Plot();// We put a dummy widget around to have// so that Qt paints a widget background// when resizingQWidget window;QHBoxLayout *layout = new QHBoxLayout( &window );layout->setContentsMargins( 0, 0, 0, 0 );layout->addWidget( plot ); plot->drawArray();//畫圖...........window.resize(600,400);window.show();return a.exec(); }4.2 散點圖(基本功能)
頭文件:
class CCruvePlot:publicQwtPlot { public:CCruvePlot();~CCruvePlot(void);public:void drawPlotCruve(); private:QwtPlotCurve * curve;QVector<double> xData;QVector<double> yData; };//實現文件: #include "cruvePlot.h" const int LineNum=7; const int PointNum=7; CCruvePlot::CCruvePlot(){} CCruvePlot::~CCruvePlot(void){}void CCruvePlot::drawPlotCruve() {//QMessageBox::information(this,"Running!","Running matlab Function.....");setTitle("A Simple QwtPlot Demonstration");//設置標題insertLegend(new QwtLegend(), QwtPlot::RightLegend);//設置標線的欄setAxisTitle(xBottom, "x -->");setAxisScale(xBottom, 0.0, 10.0);setAxisTitle(yLeft, "y -->");setAxisScale(yLeft, 0, 10.0);QwtPlotCurve *curve = new QwtPlotCurve("lineFirst");//實例化一條曲線curve->attach(this);double *x=new double[PointNum];double *y=new double[PointNum];for(int i=0;i<PointNum;i++) {x[i]=i;y[i]=i+3;}curve->setSamples (x,y,PointNum);//傳畫曲線的數據curve->setPen(QPen(Qt::red));QwtPlotCurve *curve2 = new QwtPlotCurve("lineSecond");//實例化另一條線curve2->attach(this);double *x2=new double[PointNum];double *y2=new double[PointNum];for(int i=0;i<PointNum;i++){x2[i]=i*3;y2[i]=i+3;}curve2->setSamples (x2,y2,PointNum);curve2->setPen(QPen(Qt::blue));return;}4.3 對于QwtSymbol的使用,詳細參考此博客:http://blog.csdn.net/qustdjx/article/details/7940896
??? 為什么不封裝成類似Matlab的用法呢?
總結
以上是生活随笔為你收集整理的Qwt--散点图/函数图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 会玩app怎么刷等级
- 下一篇: SciPy--数值计算