2

I'm trying to create a simple colored "spectrogram" style plot of some (x,y,value) data, using the QwtPlotSpectogram library. The source data is an array of doubles. I don't need contour lines, just a grid of colored pixels representing the value at each x,y point.

I found an example here which I have modified slightly, and I also wrote my own similar thing. In both cases, I get the same problem - I see contour lines (if I use "setDisplayMode(QwtPlotSpectrogram::ContourMode, on)" ), but I never get any colored spectrogram plot no matter what I set (e.g. "setDisplayMode(QwtPlotSpectrogram::ImageMode, on)" ). I do get empty axes and a color bar on the right. In the example below, I get some contours when the "showContour(true)" slot is called, but an empty plot if "showContour(false)" and "showSpectrogram(true)" are used.

I added some debug to my QwtRasterData class implementation, so that I got a message when "initRaster" was called - i.e. when the plotting engine asked for data to plot (I guess?). This message popped up when contours were enabled, but NEVER showed when only ImageMode was enabled - so it looks like there is no attempt to plot my data at all!

Am I missing something? I've looked at several examples (there aren't many and they are similar to what I have) and read through the QwtPlotSpectrogram reference but can't figure it out. Note that I have removed zoomer and panner from the example as I don't need them... hopefully they are not required?

Here is my example spectrogram class:

// TestSpectrogram.h
class SpectrogramData;

class TestSpectrogram: public QwtPlot
{
    Q_OBJECT

public:
    TestSpectrogram(QWidget * = NULL);

public slots:
    void showContour(bool on);
    void showSpectrogram(bool on);

private:
    QwtPlotSpectrogram *d_spectrogram;
    SpectrogramData *d_data;
    QwtLinearColorMap *colorMap;
    QwtScaleDraw *sd;

};

//---------------------------------
// TestSpectrogram.cpp
// ======= Raster Data ======================================================
class SpectrogramData: public QwtRasterData
{
public:
    SpectrogramData():
        QwtRasterData()
    {
    }

    virtual QwtRasterData *copy() const
    {
        return new SpectrogramData();
    }

    virtual QwtInterval range() const
    {
        return QwtInterval(0.0, 10.0);
    }

    virtual double value(double x, double y) const
    {
        // Some fake data:
        const double c = 0.842;
        const double v1 = x * x + (y-c) * (y+c);
        const double v2 = x * (y+c) + x * (y+c);
        return 1.0 / (v1 * v1 + v2 * v2);
    }
};


//========== Spectrogram Plot Example ====================================
TestSpectrogram::TestSpectrogram(QWidget *parent):
    QwtPlot(parent)
{
    d_spectrogram = new QwtPlotSpectrogram();

    colorMap = new QwtLinearColorMap(Qt::darkCyan, Qt::red);
    colorMap->addColorStop(0.1, Qt::cyan);
    colorMap->addColorStop(0.6, Qt::green);
    colorMap->addColorStop(0.95, Qt::yellow);

    d_spectrogram->setColorMap( colorMap );

    d_data = new SpectrogramData();
    d_spectrogram->setData(d_data);
    d_spectrogram->attach(this);

    QList<double> contourLevels;
    for ( double level = 0.0; level < 1.0; level += 0.5 ) contourLevels.append(level);
    d_spectrogram->setContourLevels(contourLevels);

    // A color bar on the right axis
    QwtScaleWidget *rightAxis = axisWidget(QwtPlot::yRight);
    rightAxis->setTitle("Intensity");
    rightAxis->setColorBarEnabled(true);
    rightAxis->setColorMap( d_data->range(), colorMap );

    setAxisScale(QwtPlot::yRight, 0.0,10.0 );
    enableAxis(QwtPlot::yRight);

    plotLayout()->setAlignCanvasToScales(true);
    replot();

}

void TestSpectrogram::showContour(bool on)
{
    d_spectrogram->setDisplayMode(QwtPlotSpectrogram::ContourMode, on);
    replot();
}

void TestSpectrogram::showSpectrogram(bool on)
{
    d_spectrogram->setDisplayMode(QwtPlotSpectrogram::ImageMode, on);
    d_spectrogram->setDefaultContourPen(on ? QPen() : QPen(Qt::NoPen));
    replot();
}

In my UI, I have a frame that I want to show the plot in, plus a couple of check boxes for testing. I set the plot and the check boxes up like this:

TestSpectrogram *d_plot;   // In header.

d_plot = new TestSpectrogram( ui->framePlot );
connect( ui->checkTestImage,   SIGNAL(toggled(bool)), d_plot, SLOT(showSpectrogram(bool)) );
connect( ui->checkTestContour, SIGNAL(toggled(bool)), d_plot, SLOT(showContour(bool))     );

Other information: Qt: 5.4.0 (MinGW 4.9 32 bit) Qwt: 6.1.2 Windows 7

Jeremy
  • 1,083
  • 3
  • 13
  • 25

1 Answers1

3

My solution turned out to be that I didn't call the setInterval method of the QwtRasterData object (the base of SpectrogramData in the example above).

I put the following in the constructor for SpectrogramData, and it worked (I got a colored spectrogram plot):

setInterval( Qt::XAxis, QwtInterval(-1.0, 1.0) );
setInterval( Qt::YAxis, QwtInterval(-1.0, 1.0) );
setInterval( Qt::ZAxis, QwtInterval(0.0, 10.0) );

It looks like you need to tell it the axis interval for z, y and z axes, otherwise it doesn't even try calling the value method of the raster data object - at least in Qwt 6.1.2 - and nothing gets plotted.

I looked at several examples and only one of them used setInterval; also, the Qwt docs are pretty vague: "Set the bounding interval for the x, y or z coordinates." - no further explanation of why this is needed.

Jeremy
  • 1,083
  • 3
  • 13
  • 25