QCustomPlot Discussion and Comments

Associate multiple colorMaps to colorScaleReturn to overview

I want to connect the new color Map that is created every time the function is called to the color Scale but it only is connected to the first map that was created for some reason , I cannot change the colors of the next color Map when I slide the color Scale.Everything else works just the sliding part to change the colors of the next Map doesn't work. I would be really grateful if someone could help me out.

QCPColorScale *colorScale;
    // set up the QCPColorMap:N
    colorMap = new QCPColorMap(ui->customPlot->xAxis, ui->customPlot->yAxis);

    int nx = samplesToPlot * 2;
    int ny = (nx/2) * 0.55522; //best fit policy for angle || tan29.04 = 0.55522
    colorMap->data()->setSize(nx, ny); // we want the color map to have nx * ny data points

    //added 5 to extend the length of the color to accomodate the traces of various boat speeds
    colorMap->data()->setRange(QCPRange(map_x1, map_x2), QCPRange(lastY - map_y1, lastY + numOfTraces * res_fwd + 5 ));
    QCPRange MapRange = QCPRange(0,4000);
    colorMap->setInterpolate(false);//to get visible pixel pattern


    // add a color scale:
    colorScale = new QCPColorScale(ui->customPlot);
    QCPRange colorRange = QCPRange(0,4000);
    ui->customPlot->plotLayout()->addElement(0, 1, colorScale); // add it to the right of the main axis rect
    colorScale->setType(QCPAxis::atRight); // scale shall be vertical bar with tick/axis labels right (actually atRight is already the default)
    colorMap->setColorScale(colorScale); // associate the color map with the color scale
    // colorMapNum_queue.last()->setColorScale(colorScale); // associate the color map with the color scale

With the code above, you create a new color scale every time. Then you insert it into the (0, 1) cell of your plot layout. This works for the first color scale, but of course fails for all following tries because cell (0, 1) is occupied. QCustomPlot notifies you of this error in your code via the debug output.

The solution is simple: Set up a QCPColorScale only once, and use it as color scale for all the color maps (via setColorScale) once you create them.

Thanks a lot it worked. I defined the colorScale in header file and added the colorScale = new QCPColorScale(ui->customPlot); after ui->setupUi(this);