PDA

View Full Version : problem in plotting QwtMatrixRasterData using QwtPlotSpectrogram



johnMick
25th July 2011, 10:30
Hi;
I am new bie to qt and qwt. i was trying to understand rasterview example ,in this example values are plotted on XBottom and y left axis, but in my application I have to plot same type data on xTop and y left axis .(It means orientation of plot may change)
while going thru QWT ,i understood that by default QwtMatrixRasterData is plotted on xBottom and yleft axis ,CAn any body suggest how to plot values on XTop and yleft axis?

Uwe
25th July 2011, 10:40
spectrogram->setXAxis( QwtPlot::XTop );
Uwe

johnMick
25th July 2011, 11:36
6715
Hi ,
I tried your tip but i didnt get expected result(attached with this reply) code is as below:

#include "plot.h"
#include <qwt_color_map.h>
#include <qwt_plot_spectrogram.h>
#include <qwt_plot_layout.h>
#include <qwt_matrix_raster_data.h>
#include <qwt_scale_widget.h>
#include <qwt_plot_magnifier.h>
#include <qwt_plot_panner.h>
#include <qwt_plot_renderer.h>
#include <qwt_plot_grid.h>
#include <qfiledialog.h>
#include <qimagewriter.h>

class RasterData: public QwtMatrixRasterData
{
public:
RasterData()
{
const double matrix[] =
{
1.0000,0.0443,0.0098,0.0740,0.0030,0.0122,0.0001,
0.0443,1.0000,0.0122,0.0354,0.0013,0.0024,0.0001,
0.0098,0.0122,1.0000,0.1146,0.0520,0.0054,0.0068,
0.0740,0.0354,0.1146,1.0000,0.0131,0.0589,0.0011,
0.0030,0.0013,0.0520,0.0131,1.0000,0.0105,0.0169,
0.0122,0.0024,0.005,0.0589,0.0105,1.0000,0.0187,
0.0001,0.0001,0.0068,0.0011,0.0169,0.0187,1.0000,

};

QVector<double> values;
for ( uint i = 0; i < sizeof(matrix) / sizeof(double); i++ )
values += matrix[i];

// const int numColumns = 4;
const int numColumns = 7;
setValueMatrix(values, numColumns);

//setInterval( Qt::XAxis,
// QwtInterval( -0.5, 3.5, QwtInterval::ExcludeMaximum ) );
setInterval( Qt::XAxis,
QwtInterval( 0.5, 7.5, QwtInterval::ExcludeMaximum ) );
// setInterval( Qt::YAxis,
// QwtInterval( -0.5, 3.5, QwtInterval::ExcludeMaximum ) );
setInterval( Qt::YAxis,
QwtInterval( 0.5, 7.5, QwtInterval::ExcludeMaximum ) );
//setInterval( Qt::ZAxis, QwtInterval(1.0, 6.0) );
setInterval( Qt::ZAxis, QwtInterval(0.0, 1.0) );
}
};

class ColorMap: public QwtLinearColorMap
{
public:
ColorMap():
QwtLinearColorMap(Qt::darkBlue, Qt::darkRed)
{
addColorStop(0.2, Qt::blue);
addColorStop(0.4, Qt::cyan);
addColorStop(0.6, Qt::yellow);
addColorStop(0.8, Qt::red);
/* addColorStop(0.1, Qt::blue);
addColorStop(0.2, Qt::blue);
addColorStop(0.3, Qt::blue);
addColorStop(0.4, Qt::cyan);
addColorStop(0.6, Qt::yellow);
addColorStop(0.8, Qt::red);*/
}
};

Plot::Plot(QWidget *parent):
QwtPlot(parent)
{
#if 0
QwtPlotGrid *grid = new QwtPlotGrid();
grid->setPen(QPen(Qt::DotLine));
grid->attach(this);
#endif

d_spectrogram = new QwtPlotSpectrogram();
d_spectrogram->setRenderThreadCount(0); // use system specific thread count

d_spectrogram->setColorMap( new ColorMap() );

d_spectrogram->setData(new RasterData());
d_spectrogram->attach(this);

const QwtInterval zInterval = d_spectrogram->data()->interval( Qt::ZAxis );
// A color bar on the right axis
QwtScaleWidget *rightAxis = axisWidget(QwtPlot::yRight);
rightAxis->setColorBarEnabled(true);
rightAxis->setColorBarWidth(40);
rightAxis->setColorMap(zInterval, new ColorMap() );

setAxisScale(QwtPlot::yRight,zInterval.minValue(), zInterval.maxValue(),0.1 );
enableAxis(QwtPlot::yRight);

plotLayout()->setAlignCanvasToScales(true);

// setAxisScale(QwtPlot::xBottom, 0.0, 3.0);
setAxisScale(QwtPlot::xTop, 1.0, 7.0);
//setAxisMaxMinor(QwtPlot::xBottom, 0);
//setAxisMaxMinor(QwtPlot::xBottom, 0);
setAxisMaxMinor(QwtPlot::xTop, 0);
setAxisScale(QwtPlot::yLeft, 1.0, 7.0);
setAxisMaxMinor(QwtPlot::yLeft, 0);

// QwtPlotMagnifier *magnifier = new QwtPlotMagnifier( canvas() );
// magnifier->setAxisEnabled( QwtPlot::yRight, false);

QwtPlotPanner *panner = new QwtPlotPanner( canvas() );
panner->setAxisEnabled( QwtPlot::yRight, false);
}

void Plot::exportPlot()
{
QString fileName = "rasterview.pdf";

#ifndef QT_NO_FILEDIALOG
const QList<QByteArray> imageFormats =
QImageWriter::supportedImageFormats();

QStringList filter;
filter += "PDF Documents (*.pdf)";
#ifndef QWT_NO_SVG
filter += "SVG Documents (*.svg)";
#endif
filter += "Postscript Documents (*.ps)";

if ( imageFormats.size() > 0 )
{
QString imageFilter("Images (");
for ( int i = 0; i < imageFormats.size(); i++ )
{
if ( i > 0 )
imageFilter += " ";
imageFilter += "*.";
imageFilter += imageFormats[i];
}
imageFilter += ")";

filter += imageFilter;
}

fileName = QFileDialog::getSaveFileName(
this, "Export File Name", fileName,
filter.join(";;"), NULL, QFileDialog::DontConfirmOverwrite);
#endif
if ( !fileName.isEmpty() )
{
QwtPlotRenderer renderer;
renderer.renderDocument(this, fileName, QSizeF(300, 200), 85);
}
}

void Plot::setResampleMode(int mode)
{
RasterData *data = (RasterData *)d_spectrogram->data();
data->setResampleMode( (QwtMatrixRasterData::ResampleMode) mode);

replot();
}


Please let me know where i am wrong and how to plot this graph

Uwe
25th July 2011, 11:51
Beside, that the screenshot is so small, that I can hardly see the scales: is it what you want to have or what you get ? And if it is not what you want to have- what exactly is wrong with it ?

Uwe

johnMick
25th July 2011, 12:00
Hi Uwe,
The attached image is expected result and scale is (xTop :1 to 7
YLeft: 1 to 7 (from up to down).

In my actual out put i am getting y axis divison as 1 to 7(from down to up ) and output images(red squares) as diagonally right up to left down( which should be diagonally left up to left down).
Can u pls see the code what modification is required for same values as given in code?
Thanks in advance

Uwe
25th July 2011, 12:54
If all you want is to invert the y axis:


//setAxisScale(QwtPlot::yLeft, 1.0, 7.0);
setAxisScale(QwtPlot::yLeft, 7.0, 1.0);
In case of using autoscaling this can be done with setting the QwtScaleEngine::Inverted attribute.

Uwe

johnMick
25th July 2011, 13:13
I also wanted that my red squares will start from 0.5 and end with 1.5 ? what values should i set in setInterval method?

Uwe
25th July 2011, 13:34
I also wanted that my red squares will start from 0.5 and end with 1.5 ?
That's what they do, but you are setting the scales, so that half of the first square is cut off.

I guess this is your code:


#if 0
setAxisScale( QwtPlot::xTop, 1.0, 7.0 );
setAxisScale( QwtPlot::yLeft, 7.0, 1.0 );
#else
for ( int i = 0; i < QwtPlot::axisCnt; i++ )
axisScaleEngine( i )->setAttribute( QwtScaleEngine::Floating, true );
axisScaleEngine( yLeft )->setAttribute( QwtScaleEngine::Inverted, true );
#endif
Uwe

johnMick
26th July 2011, 09:18
Hi,
I have one more doubt , in my output red image(color zone) is coming as rectangle but i want should be display as square
is it any way that i can set color zone as square?

Uwe
26th July 2011, 09:50
A spectrogram or any raster data object has no color zones, rectangles or squares !

I guess what you want to have is a plot with a fixed aspect ratio ( see qwt_plot_rescaler ).

Uwe

johnMick
26th July 2011, 11:18
But in thiscase, i have given 7*7 matrix value to QWTMatrixRaster Data, hence total row and column should be 7
hence 7 squares(red squares) should be displayed
But in output it is showing only 6 red squares it means row and column are 6 .


Can u pls explain why its happening? Is there any problem in coding?

Added after 53 minutes:

I tried to set set aspect ratio by using below code

QwtPlotRescaler *ptr = new QwtPlotRescaler(this->canvas(),QwtPlot::xTop);
ptr->setAspectRatio(QwtPlot::xTop,0.0);

where this is pointer to QWTPlot but when i used this lines ,all the plot gone and in output only x and y axis is displayed
what was the problem , how to set aspect ratio?

Uwe
27th July 2011, 07:59
But in thiscase, i have given 7*7 matrix value to QWTMatrixRaster Data, hence total row and column should be 7
hence 7 squares(red squares) should be displayed
But in output it is showing only 6 red squares it means row and column are 6 .
On my box I can see 7 rows and columns with your code ( with the changes I have posted ). Probably it helps to understand, that the current scales and the bounding rectangle of the plot items are different things - beside, that in case of autoscaling the scales are adjusted to the bounding rectangles.



QwtPlotRescaler *ptr = new QwtPlotRescaler(this->canvas(),QwtPlot::xTop);
ptr->setAspectRatio(QwtPlot::xTop,0.0);
This is what you wrote: dy = dx / 0.0;

What do you expect to get when you have a reference of [0-7] and a aspect ratio of 0.0 ? In fact a value of 0.0 disables rescaling of the axis.

Uwe

revellix
22nd August 2011, 17:21
do I understand it right, that MatrixRasterData divides a QVector in numColoumns, when I call setValues() ?? Finally I need a Vector instead of a Matrix (2D-vector) ?

Uwe
22nd August 2011, 20:12
Qt offers no container for matrices - and I don't want to introduce dependencies to the Boost library only because of this. That's all.

Uwe