简体   繁体   中英

Qt - QCustomPlot painting graph

I have a problem with drawing the graph in QCustomPlot library. I would like to draw a logarithm graph but I use drawing on the interval <-3;3>. Because logarithm is not defined from -3 to 0, I tried to do nothing while drawing on this interval.

I have this code:

QVector<double> x(10001), y(10001);
QVector<double> x1(10001), y1(10001);

double t=-3; //cas
double inkrement = 0.0006;
for (int i=0; i<10001; i++)//kvadraticka funkcia
{
  x[i] = t;
  y[i] = (-1)*t*t-2;
  t+=inkrement;
}

int g=0;
for(double l=-3;l<3; l+=inkrement) {
   if(l<=0.0) continue;
   else {
   //QMessageBox::warning(this, tr("note"), tr("l=%1\n").arg(l), QMessageBox::Ok);
   x1[g] = l;
   y1[g] = log10(l)/log10(exp(1.0));
   //QMessageBox::warning(this, tr("note"), tr("x1=%1\ny1=%2").arg(x1[g]).arg(y1[g]), QMessageBox::Ok);
   //break;
   g++;
   }
}

customPlot->addGraph();
customPlot->graph(0)->setData(x, y);

customPlot->addGraph();
customPlot->graph(1)->setData(x1, y1);

customPlot->xAxis->setLabel("x");
customPlot->yAxis->setLabel("y");

customPlot->xAxis->setRange(-3, 3);
customPlot->yAxis->setRange(-10, 5);
customPlot->replot();

where x1 and y1 are QVectors... But the graph is like the first point is in [0,0]. So I have then a line that connects point [0,0] with the logarithm graph and I dont know why :( When I put l=0.0006 before the cycle, everything is OK. Can you help me with that please?

It seems that you set count of x1 and y1 before this loop. QVector is initialized with zeros. So if you don't set any value for some items then x1 and y1 will contain zero values at their end.

You should use empty QVector's and add new values if g is OK:

QVector<double> x1, y1;
//...
x1 << l;
y1 << log10(l)/log10(exp(1.0));

g variable can be removed then. And I think it's better to remove i variable and use for(double l = -3; l <= 3; l+=increment) loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM