QCustomPlot Discussion and Comments

plot gaussianReturn to overview

hello all :)

i need to plot a gaussian :
https://ibb.co/XtL1DRF

but I'm not comfortable with the example :

QVector<double> x(101), y(101); // initialize with entries 0..100
for (int i=0; i<101; ++i)
{
  x[i] = i/50.0 - 1; // x goes from -1 to 1
  y[i] = x[i]*x[i]; // let's plot a quadratic function
}

and I don't really know how to go about it, if anyone can help me I'd be happy.

What is it you're not comfortable with?
Just set the y[ i] to the respective values of the gaussian formula at the specific x[ i].

yes it's good

  for (int i=0; i<1001; ++i)
       {

         x[i] = p;
         y[i] = ((1/(1* qSqrt(2*M_PI)))*qExp(-(qPow((x[i])-2,2))/(2*1*1)));
         p++;
       }

Myself, I didn't understand what I didn't understand. Aha!

but if I want a smooth curve, how do I do it?

I think you have to add more points.

qcustomplot not implement smooth curve

you can see that https://www.jianshu.com/p/0d4b84bd611a

thank you lancelot

i have just a little problem with jianshu code for the mSmooth when i use this :

 graph->setSmooth(true); 

He finds the setmooth but i have this error :

error : undefined reference to `QCPGraph::setSmooth(bool)'

I added the smooth to my .h. like this :

class QCP_LIB_DECL QCPGraph : public QCPAbstractPlottable1D<QCPGraphData>
{
  Q_OBJECT
  /// \cond INCLUDE_QPROPERTIES
  Q_PROPERTY(LineStyle lineStyle READ lineStyle WRITE setLineStyle)
  Q_PROPERTY(QCPScatterStyle scatterStyle READ scatterStyle WRITE setScatterStyle)
  Q_PROPERTY(int scatterSkip READ scatterSkip WRITE setScatterSkip)
  Q_PROPERTY(QCPGraph* channelFillGraph READ channelFillGraph WRITE setChannelFillGraph)
  Q_PROPERTY(bool adaptiveSampling READ adaptiveSampling WRITE setAdaptiveSampling)
  Q_PROPERTY(bool smooth READ smooth WRITE setSmooth) //here
  /// \endcond
public:
  /*!
    Defines how the graph's line is represented visually in the plot. The line is drawn with the
    current pen of the graph (\ref setPen).
    \see setLineStyle
  */
  enum LineStyle { lsNone        ///< data points are not connected with any lines (e.g. data only represented
                                 ///< with symbols according to the scatter style, see \ref setScatterStyle)
                   ,lsLine       ///< data points are connected by a straight line
                   ,lsStepLeft   ///< line is drawn as steps where the step height is the value of the left data point
                   ,lsStepRight  ///< line is drawn as steps where the step height is the value of the right data point
                   ,lsStepCenter ///< line is drawn as steps where the step is in between two data points
                   ,lsImpulse    ///< each data point is represented by a line parallel to the value axis, which reaches from the data point to the zero-value-line
                 };
  Q_ENUMS(LineStyle)
  
  explicit QCPGraph(QCPAxis *keyAxis, QCPAxis *valueAxis);
  virtual ~QCPGraph();
  
  // getters:
  QSharedPointer<QCPGraphDataContainer> data() const { return mDataContainer; }
  LineStyle lineStyle() const { return mLineStyle; }
  QCPScatterStyle scatterStyle() const { return mScatterStyle; }
  int scatterSkip() const { return mScatterSkip; }
  QCPGraph *channelFillGraph() const { return mChannelFillGraph.data(); }
  bool adaptiveSampling() const { return mAdaptiveSampling; }
  bool smooth() const { return mSmooth; } // here
  
  // setters:
  void setData(QSharedPointer<QCPGraphDataContainer> data);
  void setData(const QVector<double> &keys, const QVector<double> &values, bool alreadySorted=false);
  void setLineStyle(LineStyle ls);
  void setScatterStyle(const QCPScatterStyle &style);
  void setScatterSkip(int skip);
  void setChannelFillGraph(QCPGraph *targetGraph);
  void setAdaptiveSampling(bool enabled);
  void setSmooth(bool enabled); // here
  
  // non-property methods:
  void addData(const QVector<double> &keys, const QVector<double> &values, bool alreadySorted=false);
  void addData(double key, double value);
  
  // reimplemented virtual methods:
  virtual double selectTest(const QPointF &pos, bool onlySelectable, QVariant *details=0) const Q_DECL_OVERRIDE;
  virtual QCPRange getKeyRange(bool &foundRange, QCP::SignDomain inSignDomain=QCP::sdBoth) const Q_DECL_OVERRIDE;
  virtual QCPRange getValueRange(bool &foundRange, QCP::SignDomain inSignDomain=QCP::sdBoth, const QCPRange &inKeyRange=QCPRange()) const Q_DECL_OVERRIDE;
  
protected:
  // property members:
  LineStyle mLineStyle;
  QCPScatterStyle mScatterStyle;
  int mScatterSkip;
  QPointer<QCPGraph> mChannelFillGraph;
  bool mAdaptiveSampling;
  bool mSmooth; // here

i try with a little cheat and for a gaussian is not realy good :

https://ibb.co/BL7q18d

but for a smaal curve it's okay :

https://ibb.co/5nFCkwb

show your gaussian code


float p;

      
    // generate some data:
       QVector<double> x(10001), y(10001); 
       //p=(x.size()/2)*(-1);
       
       double k=0.5;
       for (int i=0; i<10001; ++i)
       {

         x[i] = p;
         y[i] = ((1/(1* qSqrt(2*M_PI)))*qExp(-(qPow((x[i])-2,2))/(2*1*1)));
         p++;

       }

edit

float p;
 
       
    // generate some data:
       QVector<double> x(10001), y(10001); 
       p=(x.size()/2)*(-1);
        
       double k=0.5;
       for (int i=0; i<10001; ++i)
       {
 
         x[i] = p;
         y[i] = ((1/(1* qSqrt(2*M_PI)))*qExp(-(qPow((x[i])-2,2))/(2*1*1)));
         p++;
 
       }

From first glance that looks like a huge number of points in the flat wings of the gaussian, and only very few within the actual relevant inner couple of sigmas.

your code somewhere wrong, I can't get same image as you

https://ibb.co/DW8hddS

you use jianshu method or you add more point with a step of 0,1 ?

your code and jianshu code