Qt - Q图绘画图



我在QCustomPplot库中绘制图形时遇到问题。我想画一个对数图,但我使用在区间 <-3;3> 上画画。因为对数不是从 -3 到 0 定义的,所以我在这个区间上绘制时尝试什么都不做。

我有这个代码:

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=%1n").arg(l), QMessageBox::Ok);
   x1[g] = l;
   y1[g] = log10(l)/log10(exp(1.0));
   //QMessageBox::warning(this, tr("note"), tr("x1=%1ny1=%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();

其中 x1 和 y1 是 QVectors...但图形就像第一个点在 [0,0] 中。所以我有一条线将点 [0,0] 与对数图连接起来,我不知道为什么:(当我把 l=0.0006 放在周期之前时,一切都很好。你能帮我吗?

似乎您在此循环之前设置了 x1 和 y1 的计数。QVector 初始化为零。因此,如果您没有为某些项目设置任何值,则 x1y1 的末尾将包含零值。

你应该使用空的QVector,如果g正常,则添加新值:

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

然后g变量可以删除。我认为最好删除i变量并使用for(double l = -3; l <= 3; l+=increment)循环。

最新更新