您的位置:首页 > 其它

QCustomPlot使用之绘制散点图

2017-12-18 10:36 1831 查看
一、简单概述:QCustomPlot是基于Qt封装的一个图形绘制库、可以绘制散点图、曲线图、柱状图等各种图形,并可以把各种图形导出为png、jpg等各种格式的图片,使用很方便,只有两个文件qcustomplot.h和qcustomplot.cpp,这两个文件直接加到工程中即可。需要说明的是这个库依赖Qt中的Qt5PrintSupport模块,使用时添加上该模块即可。

二、下载地址:

QCustomPlot库下载:http://www.qcustomplot.com/index.php/download



解压后里面有需要的说明文档、代码示例、源码等。

三、绘制散点图示例:

1、效果图示例



2、代码示例

CustomPoltWidget::CustomPoltWidget(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);

// 设置坐标范围、坐标名称
QCustomPlot *pCustomPlot = new QCustomPlot(this);
pCustomPlot->resize(this->width(), this->height());
pCustomPlot->xAxis->setRange(-180.00, 180.00);
pCustomPlot->yAxis->setRange(-90.00, 90.00);
pCustomPlot->xAxis->setLabel("latitude");
pCustomPlot->yAxis->setLabel("longitude");

// 增加图层
pCustomPlot->addGraph(pCustomPlot->yAxis, pCustomPlot->xAxis);

QVector<double> latVector, lonVector;
latVector << -75 << -50 << -50 << 0 << 50 << 100 << 75;
lonVector << -75 << -50 << -25 << 0 << 25 << 50 << 75;

// 设置画笔风格
QPen drawPen;
drawPen.setColor(Qt::red);
drawPen.setWidth(4);

// 绘制散点
QCPGraph * curGraph = pCustomPlot->addGraph();
curGraph->setPen(drawPen);
curGraph->setLineStyle(QCPGraph::lsNone);
curGraph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 2));
curGraph->setData(latVector, lonVector);

pCustomPlot->xAxis->setVisible(true);
pCustomPlot->yAxis->setVisible(true);

// 保存为png图片
QString path = QApplication::applicationDirPath() + "\\" + "test123.png";
pCustomPlot->savePng(path);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: