QT 绘制折线图,使得数据间间隔相同
时间: 2023-08-12 16:20:25 浏览: 60
在QT中,你可以使用QCustomPlot库来绘制折线图,并且可以通过设置x轴的刻度来控制坐标轴的显示。
例如,如果你想设置x轴的刻度间隔为2,可以使用以下代码:
```cpp
// 创建QCustomPlot对象
QCustomPlot *customPlot = new QCustomPlot(this);
// 创建数据
QVector<double> x(9), y(9);
for (int i=0; i<9; ++i)
{
x[i] = i+1;
y[i] = qrand()%35;
}
// 添加折线图
customPlot->addGraph();
customPlot->graph(0)->setData(x, y);
// 设置x轴刻度
customPlot->xAxis->setAutoTicks(false);
customPlot->xAxis->setTickVector(x);
customPlot->xAxis->setTickStep(2);
// 显示图形
customPlot->replot();
```
同样的,如果你想设置y轴的刻度间隔为5,可以使用以下代码:
```cpp
// 创建QCustomPlot对象
QCustomPlot *customPlot = new QCustomPlot(this);
// 创建数据
QVector<double> x(9), y(9);
for (int i=0; i<9; ++i)
{
x[i] = i+1;
y[i] = qrand()%35;
}
// 添加折线图
customPlot->addGraph();
customPlot->graph(0)->setData(x, y);
// 设置y轴刻度
customPlot->yAxis->setAutoTicks(false);
customPlot->yAxis->setTickVector(QVector<double>() << 0 << 5 << 10 << 15 << 20 << 25 << 30 << 35);
// 显示图形
customPlot->replot();
```
希望这能够帮助到你。