QCustomPlot使用之绘制散点图

一、QCustomPlot概述

简单概述: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);
}
  • 11
    点赞
  • 84
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在 QCustomPlot绘制散点图,你可以按照以下步骤进行操作: 1. 创建一个 QCustomPlot 对象,并设置需要的坐标轴和绘图属性。 ```cpp QCustomPlot *customPlot = new QCustomPlot(this); ``` 2. 创建一个 QCPGraph 对象,并将其添加到 QCustomPlot 对象中。 ```cpp QCPGraph *scatterGraph = customPlot->addGraph(); ``` 3. 设置 QCPGraph 对象的图形类型为散点图。 ```cpp scatterGraph->setScatterStyle(QCPScatterStyle::ssCircle); ``` 4. 创建一个 QVector<QCPGraphData> 用于存储散点的数据点。 ```cpp QVector<QCPGraphData> scatterData; ``` 5. 向 scatterData 中添加散点数据。 ```cpp scatterData.append(QCPGraphData(x1, y1)); scatterData.append(QCPGraphData(x2, y2)); // 添加更多的散点数据... ``` 6. 将 scatterData 设置为 QCPGraph 对象的数据。 ```cpp scatterGraph->data()->set(scatterData); ``` 7. 根据需要,设置坐标轴的范围和刻度等属性。 ```cpp customPlot->xAxis->setRange(xMin, xMax); customPlot->yAxis->setRange(yMin, yMax); // 设置更多的坐标轴属性... ``` 8. 调用 QCustomPlot 对象的 replot() 函数,用于刷新绘图。 ```cpp customPlot->replot(); ``` 通过以上步骤,你可以在 QCustomPlot绘制散点图。你需要创建一个 QCPGraph 对象,并将其添加到 QCustomPlot 对象中。然后,创建一个 QVector<QCPGraphData> 来存储散点数据,并将其设置为 QCPGraph 对象的数据。最后,根据需要设置坐标轴的属性,并调用 replot() 函数刷新绘图。 希望以上步骤能够帮助你绘制散点图。如果你有任何进一步的问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值