Results 1 to 6 of 6

Thread: QWT- Help with plotting data from a vector.

  1. #1
    Join Date
    Jan 2014
    Location
    Singapore
    Posts
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QWT- Help with plotting data from a vector.

    Hi all,

    I am a newbie in QWT. Just wish to ask how can I plot data from a vector. The code below shows what I have done so far. Basically, the data I am storing into the vector are pixel values of an image along the axis. After that, I proceed to plot them, but there is always an error. I am not sure how to exactly do them either:/ I been trying to plot for almost 2 weeks plus already. But to no avail. I am still confused over the curve as well, why is there another vector needed for the setSamples() of curve?

    Qt Code:
    1. vector<byte> v_char; //values of pixels taken from y-axis //tried double and int as well.
    2.  
    3. //store pixel values into v_char
    4. v_char.reserve(image.rows * image.cols);
    5.  
    6. for (int i = 0; i < image.rows; i++)
    7. {
    8. int segment_start = *(uchar*)image.data + i * image.step;
    9. v_char.insert(v_char.end(), segment_start, segment_start + image.cols);
    10. }
    11.  
    12. //plotting of v_char vector
    13. QwtPlot *plota=new QwtPlot(); //Create plot widget
    14. plota->setTitle( "Plot Analysis" ); //Name the plot
    15. plota->setCanvasBackground( Qt::black ); //Set the Background colour
    16. plota->setAxisScale( QwtPlot::yLeft, 0, 1000000 ); //Scale the y-axis
    17. plota->setAxisScale(QwtPlot::xBottom,0,image.rows); //Scale the x-axis
    18. plota->insertLegend(new QwtLegend()); //Insert a legend
    19.  
    20. //attach curve to plota
    21. QwtPlotCurve *curvea = new QwtPlotCurve(); // Create a curve
    22. // curvea->setTitle("Count"); //Name the curve
    23. curvea->setPen( Qt::white, 2);//Set colour and thickness for drawing the curve
    24. //Use Antialiasing to improve plot render quality
    25. curvea->setRenderHint( QwtPlotItem::RenderAntialiased, true );
    26.  
    27. QPolygonF points;
    28. for( int h = 0; h < v_char.size(); ++h)
    29. {
    30. float bin_value = v_char[h];
    31. points << QPointF((float)h, bin_value);
    32. }
    33. curvea->setSamples(points); //pass points to be drawn on the curve
    34. curvea->attach( plota ); // Attach curve to the plot
    35.  
    36. plota->resize( 600, 400 ); //Resize the plot
    37. //plot.replot();
    38. plota->show(); //Show plot
    To copy to clipboard, switch view to plain text mode 

    If anybody do know, please please do help, I already at my wits end. I can't seem to find an example or tutorial online for QWT plots. I already tried lots of things... Is there a problem with my code? Or my whole programming flow is wrong? Please help. Thanks.

  2. #2
    Join Date
    Dec 2013
    Location
    Toronto, Canada
    Posts
    62
    Thanked 16 Times in 15 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QWT- Help with plotting data from a vector.

    If you have'nt built the examples in the examples directory of qwt, then do so. Next select a project from the examples directory and run it. This is a quick way to know if all is well and you are ready to go. Do this and give a feedback

  3. #3
    Join Date
    Jan 2014
    Location
    Singapore
    Posts
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QWT- Help with plotting data from a vector.

    Quote Originally Posted by Cah View Post
    If you have'nt built the examples in the examples directory of qwt, then do so. Next select a project from the examples directory and run it. This is a quick way to know if all is well and you are ready to go. Do this and give a feedback
    @Cah, Hi, I have done them. So the configurations should be all well. I have used QWT with QT with success. I am now using QWT via Microsoft Studio 2010 C++, QT add in. I have also managed to plot a histogram via the QT add in so far with QWT. So the configuration should be fine. Just that, I having problems when it come to plotting from a vector. Stackoverflow did have somebody pose the same question too, but the solution posted was not sufficient for me to solve my problem

    you have any idea how I can code it to successfully plot from the vector? Thanks(:

  4. #4
    Join Date
    Dec 2013
    Location
    Toronto, Canada
    Posts
    62
    Thanked 16 Times in 15 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QWT- Help with plotting data from a vector.

    Try this ....

    Qt Code:
    1. #include <QApplication>
    2. #include <qwt_plot.h>
    3. #include <qwt_plot_curve.h>
    4.  
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9.  
    10. QwtPlot plot;
    11.  
    12. QPolygonF data;
    13. data << QPointF(0, 9) << QPointF(1, 4.5) << QPointF(2,4.5) << QPointF(3,0);
    14.  
    15. QwtPlotCurve* curve = new QwtPlotCurve;
    16. curve->setSamples(data);
    17.  
    18. curve->attach(&plot);
    19. plot.show();
    20.  
    21. return a.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2014
    Location
    Singapore
    Posts
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QWT- Help with plotting data from a vector.

    delete.png

    Done. This is the result. Anyway, I made a quick change.

    Instead of using QwtPlot plot;, I used QwtPlot *plot=new QwtPlot();

    Quote Originally Posted by Cah View Post
    Try this ....

    Qt Code:
    1. #include <QApplication>
    2. #include <qwt_plot.h>
    3. #include <qwt_plot_curve.h>
    4.  
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9.  
    10. QwtPlot plot;
    11.  
    12. QPolygonF data;
    13. data << QPointF(0, 9) << QPointF(1, 4.5) << QPointF(2,4.5) << QPointF(3,0);
    14.  
    15. QwtPlotCurve* curve = new QwtPlotCurve;
    16. curve->setSamples(data);
    17.  
    18. curve->attach(&plot);
    19. plot.show();
    20.  
    21. return a.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,312
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QWT- Help with plotting data from a vector.

    Quote Originally Posted by rockinfresh View Post
    ... why is there another vector needed for the setSamples() of curve?
    You have the option to copy your samples into a type of buffer that can be handled by QwtPlotCurve out of the box - or you can tell the curve how to read from your vector<byte> buffer:
    simply derive from QwtSeriesData<QPointF>: http://qwt.sourceforge.net/class_qwt_series_data.html

    Uwe

Similar Threads

  1. Plotting 3d using non-grid data
    By warcraff123456 in forum Qt Programming
    Replies: 1
    Last Post: 15th March 2013, 18:40
  2. QT 4.5 and Plotting data
    By Lezer in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 6th September 2012, 13:11
  3. axes for plotting data
    By timmu in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2012, 12:19
  4. Replies: 3
    Last Post: 27th December 2009, 00:00
  5. Widget for data plotting
    By Benjamin in forum Qt Programming
    Replies: 3
    Last Post: 12th February 2009, 15:38

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.