PDA

View Full Version : QWT- Help with plotting data from a vector.



rockinfresh
20th January 2014, 15:23
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?


vector<byte> v_char; //values of pixels taken from y-axis //tried double and int as well.

//store pixel values into v_char
v_char.reserve(image.rows * image.cols);

for (int i = 0; i < image.rows; i++)
{
int segment_start = *(uchar*)image.data + i * image.step;
v_char.insert(v_char.end(), segment_start, segment_start + image.cols);
}

//plotting of v_char vector
QwtPlot *plota=new QwtPlot(); //Create plot widget
plota->setTitle( "Plot Analysis" ); //Name the plot
plota->setCanvasBackground( Qt::black ); //Set the Background colour
plota->setAxisScale( QwtPlot::yLeft, 0, 1000000 ); //Scale the y-axis
plota->setAxisScale(QwtPlot::xBottom,0,image.rows); //Scale the x-axis
plota->insertLegend(new QwtLegend()); //Insert a legend

//attach curve to plota
QwtPlotCurve *curvea = new QwtPlotCurve(); // Create a curve
// curvea->setTitle("Count"); //Name the curve
curvea->setPen( Qt::white, 2);//Set colour and thickness for drawing the curve
//Use Antialiasing to improve plot render quality
curvea->setRenderHint( QwtPlotItem::RenderAntialiased, true );

QPolygonF points;
for( int h = 0; h < v_char.size(); ++h)
{
float bin_value = v_char[h];
points << QPointF((float)h, bin_value);
}
curvea->setSamples(points); //pass points to be drawn on the curve
curvea->attach( plota ); // Attach curve to the plot

plota->resize( 600, 400 ); //Resize the plot
//plot.replot();
plota->show(); //Show plot

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.

Cah
20th January 2014, 22:16
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

rockinfresh
21st January 2014, 02:02
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(:

Cah
21st January 2014, 02:20
Try this ....


#include <QApplication>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>


int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QwtPlot plot;

QPolygonF data;
data << QPointF(0, 9) << QPointF(1, 4.5) << QPointF(2,4.5) << QPointF(3,0);

QwtPlotCurve* curve = new QwtPlotCurve;
curve->setSamples(data);

curve->attach(&plot);
plot.show();

return a.exec();
}

rockinfresh
21st January 2014, 02:33
9953

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

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


Try this ....


#include <QApplication>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>


int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QwtPlot plot;

QPolygonF data;
data << QPointF(0, 9) << QPointF(1, 4.5) << QPointF(2,4.5) << QPointF(3,0);

QwtPlotCurve* curve = new QwtPlotCurve;
curve->setSamples(data);

curve->attach(&plot);
plot.show();

return a.exec();
}

Uwe
21st January 2014, 07:23
... 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