QCustomPlot之颜色图或热力图(QCPColorMap)实例1:图形横跨正负坐标轴(x、y)

// configure axis rect:配置轴矩形
customPlot->setInteractions(QCP::iRangeDrag|QCP::iRangeZoom); // 这也将允许通过拖拽/缩放尺度改变颜色范围
customPlot->axisRect()->setupFullAxesBox(true);
customPlot->xAxis->setLabel("x");
customPlot->yAxis->setLabel("y");
 
// set up the QCPColorMap:
QCPColorMap *colorMap = new QCPColorMap(customPlot->xAxis, customPlot->yAxis);
int nx = 200;
int ny = 200;
colorMap->data()->setSize(nx, ny); // 我们希望彩色地图有nx*ny的数据点
colorMap->data()->setRange(QCPRange(-4, 4), QCPRange(-4, 4)); // 并在键(x)和值(y)维上跨越坐标范围-4..4
// :现在,我们通过访问颜色贴图的QCPColorMapData实例来分配一些数据:
double x, y, z;
for (int xIndex=0; xIndex<nx; ++xIndex)
{
  for (int yIndex=0; yIndex<ny; ++yIndex)
  {
    colorMap->data()->cellToCoord(xIndex, yIndex, &x, &y);//这个函数是为了获得X值
    double r = 3*qSqrt(x*x+y*y)+1e-2;
    z = 2*x*(qCos(r+2)/r-qSin(r+2)/r); // the B field strength of dipole radiation (modulo physical constants)
    colorMap->data()->setCell(xIndex, yIndex, z);
  }
}
 
// 添加色标:
QCPColorScale *colorScale = new QCPColorScale(customPlot);
customPlot->plotLayout()->addElement(0, 1, colorScale); // 将其添加到主轴矩形的右侧
colorScale->setType(QCPAxis::atRight); // 刻度应为垂直条,刻度线/坐标轴标签右侧(实际上,右侧已经是默认值)
colorMap->setColorScale(colorScale); // 将颜色图与色标关联
colorScale->axis()->setLabel("Magnetic Field Strength");
 
// 将颜色贴图的“颜色渐变”设置为其中一个预设
colorMap->setGradient(QCPColorGradient::gpPolar);
// 我们还可以创建一个QCPColorGradient实例并向其中添加自己的颜色
// 渐变,请参阅QCPColorGradient的文档以获取可能的效果.
 
// 重新缩放数据维度(颜色),以使所有数据点都位于颜色渐变显示的范围内:
colorMap->rescaleDataRange();

//禁用插值,界面显示小方块 
colorMap->setInterpolate(false);                              
//确保轴rect和色标同步其底边距和顶边距(以便它们对齐):
QCPMarginGroup *marginGroup = new QCPMarginGroup(customPlot);
customPlot->axisRect()->setMarginGroup(QCP::msBottom|QCP::msTop, marginGroup);
colorScale->setMarginGroup(QCP::msBottom|QCP::msTop, marginGroup);
 
// 重新缩放键(x)和值(y)轴,以便可以看到整个颜色图:
customPlot->rescaleAxes();

customPlot->replot();

 

 

### 使用 QCustomPlot 绘制力图 #### 1. 准备工作 为了使用 `QCustomPlot` 绘制力图,首先需要确保安装并配置好 `QCustomPlot` 库。该库提供了专门用于绘制力图的类 `QCPColorMap`[^3]。 #### 2. 数据准备 力图的核心在于二维数据矩阵,其中每个元素表示对应位置的颜色强度。通常情况下,这些数据会被映射到颜色渐变上以形成视觉效果良好的表。 以下是构建力图的主要步骤: - 创建一个二维数组存储数据。 - 将此数据绑定至 `QCPColorMap` 对象。 - 配置坐标轴范围以及颜色梯度。 #### 3. 示例代码 下面是一个完整的 C++ 示例代码,展示如何利用 `QCustomPlot` 和其子类 `QCPColorMap` 来绘制力图。 ```cpp #include <QtWidgets/QApplication> #include "qcustomplot.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); // 初始化 QCustomPlot 实例 QCustomPlot customPlot; // 添加 Color Map QCPColorMap *colorMap = new QCPColorMap(customPlot.xAxis, customPlot.yAxis); colorMap->data()->setSize(50, 50); // 设置网格大小 (50x50) colorMap->data()->setRange(QCPRange(-4, 4), QCPRange(-4, 4)); // 定义 x,y 轴范围 // 构造模拟数据 for (int xIndex=0; xIndex<colorMap->data()->size().width(); ++xIndex) { double xKey = colorMap->data()->key(xIndex); for (int yIndex=0; yIndex<colorMap->data()->size().height(); ++yIndex) { double yKey = colorMap->data()->key(yIndex); colorMap->data()->setCell(xIndex, yIndex, qExp(-(xKey*xKey+yKey*yKey)/8.0)*10); // Gaussian peak } } // 设置颜色梯度 QCPColorGradient gradient = QCPColorGradient::gpColdHot; colorMap->setColorScaleData(&gradient); colorMap->rescaleData(true); // 显示颜色条 QCPColorScale *colorScale = new QCPColorScale(&customPlot); customPlot.plotLayout()->addElement(0, 1, colorScale); colorScale->setType(QCPAxis::atRight); colorMap->setColorScale(colorScale); colorScale->axis()->setLabel("Intensity"); // 自定义外观 customPlot.rescaleAxes(); customPlot.axisRect()->setupFullAxesBox(); // 展示窗口 customPlot.setWindowTitle("Heatmap Example with QCustomPlot"); customPlot.resize(800, 600); customPlot.show(); return a.exec(); } ``` 上述代码实现了以下功能: - 利用高斯分布函数生成了一个简单的二维数据集作为力图的基础。 - 设定了颜色梯度为冷暖色调 (`gpColdHot`) 并将其应用到了力图中。 - 同时还设置了右侧的颜色比例尺以便直观理解数值对应的色彩变化。 #### 4. 关键点解析 - **QCPColorMap 的初始化**: 这里我们指定了 X 和 Y 方向上的分辨率(即像素密度),并通过调用 `setData()` 方法填充实际的数据值。 - **颜色梯度的选择**: 可选多种预设好的颜色方案者自定义线性/离散型配色方案。 - **性能优化建议**: 如果处理大规模数据,则需注意内存消耗问题;可以考虑降低分辨率采用稀疏采样技术减少计算量。 --- ###
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值