Search code examples
c++c++11plotqt5qwt

Plotting a semilog chart with Qwt


I would like to plot a semilog chart using Qwt. I have not knowledge about Qwt but I was looking for some examples to guide my code. The problem is that at the moment I don't find someone. Could you help me with a simple code? I want to use a matrix where I can get the x-axis and y-axis values and use them to create the plot. Thank you!


Solution

  • Try this:

    QwtPlot *myPlot = new QwtPlot;
      QwtPlotCurve *curve1 = new QwtPlotCurve;
     
      QwtPointSeriesData* myData = new QwtPointSeriesData;
     
      QVector<QPointF>* samples = new QVector<QPointF>;
      samples->push_back(QPointF(1.0,1.0));
      samples->push_back(QPointF(2.0,2.0));
      samples->push_back(QPointF(3.0,3.0));
      samples->push_back(QPointF(4.0,5.0));
      myData->setSamples(*samples);
      curve1->setData(myData);
     
      curve1->attach(myPlot);
    

    I used here QVector but qwtplotcurve support double arrays and other things, but I like work with containers. You can choose the best for you. QPoint contains x and y values.

    Qwt also offer logarithmic scale engine: http://qwt.sourceforge.net/class_qwt_log_scale_engine.html

    I should to say that maybe something wrong with your Qwt, but next code works perfectly on my computer:

    #include "mainwindow.h"
    #include <QApplication>
    #include <qwt_plot.h>
    #include <qwt_plot_curve.h>
    
    int main(int argc, char *argv[])
    {
          QApplication a(argc, argv);
          QwtPlot *myPlot = new QwtPlot;
          QwtPlotCurve *curve1 = new QwtPlotCurve;
    
          QwtPointSeriesData* myData = new QwtPointSeriesData;
    
          QVector<QPointF>* samples = new QVector<QPointF>;
          samples->push_back(QPointF(1.0,1.0));
          samples->push_back(QPointF(2.0,2.0));
          samples->push_back(QPointF(3.0,3.0));
          samples->push_back(QPointF(4.0,5.0));
          myData->setSamples(*samples);
          curve1->setData(myData);
    
          curve1->attach(myPlot);
          myPlot->show();
    //    MainWindow w;
    //    w.show();
    
        return a.exec();
    }
    

    enter image description here