記錄下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