1. void MainWindow::setupQuadraticDemo(QCustomPlot *customPlot)
    2. {
    3. QVector<double> x(101), y(101);
    4. for (int i = 0; i < 101; ++i) {
    5. x[i] = i / 50.0 - 1; // -1 到 1
    6. y[i] = x[i] * x[i];
    7. }
    8. customPlot->addGraph(); // 添加一个曲线图QGraph
    9. customPlot->graph(0)->setData(x, y); // 为曲线图添加数据
    10. customPlot->graph(0)->setName("第一个示例"); // 设置曲线图的名字
    11. customPlot->xAxis->setLabel("x"); // 设置x轴的标签
    12. customPlot->yAxis->setLabel("y");
    13. customPlot->xAxis->setRange(-1, 1); // 设置x轴的范围为(-1,1)
    14. customPlot->yAxis->setRange(0, 1);
    15. customPlot->legend->setVisible(true); // 显示图例
    16. }

    效果图

    基础 - 图1