记录下QCustomPlot 热力图的用法
// 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);
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();
//确保轴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();
over