我在QCustomPlot库中绘制图形时遇到问题。我想画一个对数图,但我在间隔< -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=%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();
其中x1和y1是QVectors ......但是图形就像[0,0]中的第一个点。所以我有一条连接点[0,0]和对数图的线,我不知道为什么:( 当我在循环之前放置l = 0.0006时,一切正常。你可以帮帮我吗?
答案 0 :(得分:1)
似乎在此循环之前设置了x1和y1的计数。 QVector初始化为零。因此,如果您没有为某些项设置任何值,则x1
和y1
将在其末尾包含零值。
如果g没问题,你应该使用空QVector并添加新值:
QVector<double> x1, y1;
//...
x1 << l;
y1 << log10(l)/log10(exp(1.0));
然后可以删除 g
变量。我认为最好删除i
变量并使用for(double l = -3; l <= 3; l+=increment)
循环。