Results 1 to 1 of 1

Thread: QwtPlotSpectrogram with log scales

  1. #1

    Default QwtPlotSpectrogram with log scales

    I have 2d histogram data that I want to display. This essentially requires that I can set an arbitrary scale to match the binning used to calculate the data set with QwtMatrixRasterData::setValueMatrix. I posted about this on SO. I also managed to make this work the way that I want. I posted this answer that explains my solution.

    Uwe suggested that I usw a QwtRasterData based solution but my previous experiments had shows that this does not lead to the result that I want as it re-scales the image to the scales of the plot.

    As I have a working solution, I guess my only questions are:

    a) is this the right way to approach the situation?
    b) does a different solution exists that works OOTB?
    c) is there some other way to get Qwt to draw a density plot so that I don't have to do the binning myself?

    Qt Code:
    1. #include <QApplication>
    2. #include <QMainWindow>
    3. #include <QRect>
    4. #include <QStyleFactory>
    5.  
    6. #include <qwt_color_map.h>
    7. #include <qwt_matrix_raster_data.h>
    8. #include <qwt_plot.h>
    9. #include <qwt_plot_curve.h>
    10. #include <qwt_plot_spectrogram.h>
    11. #include <qwt_scale_engine.h>
    12.  
    13. class PlotSpectrogram : public QwtPlotSpectrogram {
    14. public:
    15.  
    16. void draw(
    17. QPainter* painter,
    18. const QwtScaleMap& xMap,
    19. const QwtScaleMap& yMap,
    20. const QRectF& canvasRect ) const override {
    21.  
    22. QwtScaleMap xMapLin( xMap );
    23. QwtScaleMap yMapLin( yMap );
    24.  
    25. auto const xi = data()->interval( Qt::XAxis );
    26. auto const yi = data()->interval( Qt::YAxis );
    27.  
    28. auto const dx = xMapLin.transform( xMap.s1() );
    29. xMapLin.setScaleInterval( xi.minValue(), xi.maxValue() );
    30. auto const dy = yMapLin.transform( yMap.s2() );
    31. yMapLin.setScaleInterval( yi.minValue(), yi.maxValue() );
    32.  
    33. xMapLin.setTransformation( new QwtNullTransform() );
    34. yMapLin.setTransformation( new QwtNullTransform() );
    35.  
    36. painter, xMapLin, yMapLin, canvasRect.translated( dx, -dy ) );
    37. }
    38. };
    39.  
    40. int
    41. main( int argc, char* argv[] ) {
    42.  
    43. QApplication app( argc, argv );
    44.  
    45. QVector<double> heat_values( 100 * 100 );
    46. for( int n = 0; n < 100 * 100; ++n ) {
    47. heat_values[n] = ( n % 100 ) + n / 100;
    48. };
    49.  
    50. //QwtPlotSpectrogram heat;
    51. PlotSpectrogram heat;
    52. auto heat_data = std::make_unique<QwtMatrixRasterData>();
    53. heat_data->setValueMatrix( heat_values, 100 );
    54. heat_data->setInterval( Qt::XAxis, QwtInterval( 0, 100.0 ) );
    55. heat_data->setInterval( Qt::YAxis, QwtInterval( 0, 100.0 ) );
    56. heat_data->setInterval( Qt::ZAxis, QwtInterval( 0, 200.0 ) );
    57.  
    58. heat.setDisplayMode( QwtPlotSpectrogram::DisplayMode::ImageMode, true );
    59. heat.setColorMap( new QwtLinearColorMap( Qt::white, Qt::black ) );
    60. heat.setData( heat_data.release() );
    61.  
    62. p.setAxisScaleEngine( QwtPlot::yLeft, new QwtLogScaleEngine() );
    63. p.setAxisScale( QwtPlot::yLeft, 20.0, 50.0 );
    64. p.setAxisScaleEngine( QwtPlot::xBottom, new QwtLogScaleEngine() );
    65. p.setAxisScale( QwtPlot::xBottom, 200, 500.0 );
    66. p.setAutoDelete( false );
    67. heat.attach( &p );
    68. p.repaint();
    69.  
    70. wnd.setCentralWidget( &p );
    71. wnd.resize( 400, 300 );
    72. wnd.show();
    73.  
    74. return QApplication::exec();
    75. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Thomas N; 5th August 2019 at 21:28.

Similar Threads

  1. Scales alignment
    By fruzzo in forum Qwt
    Replies: 6
    Last Post: 28th September 2011, 17:32
  2. logarithmic scales
    By sergio486 in forum Qwt
    Replies: 2
    Last Post: 22nd December 2010, 18:05
  3. fixed axis scales
    By oskarmellow in forum Qwt
    Replies: 1
    Last Post: 17th December 2008, 07:20
  4. Equal scales
    By sukram in forum Qwt
    Replies: 1
    Last Post: 22nd July 2008, 08:44
  5. Problem with scales
    By fear in forum Qwt
    Replies: 2
    Last Post: 5th May 2008, 23:18

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.