QCustomPlot Discussion and Comments

QColorMap with empty valuesReturn to overview

Hi,

Imagine that I have a nxm grid that I want to display, for that, I have followed the Color Map Demo (http://www.qcustomplot.com/index.php/demos/colormapdemo) and it is displayed perfectly. However, my grid has some empty cells that are painted as the lowest value of the map. Is there some way to overcome this, so I can leave some cells of the QColorMap uncoloured (or colored white if necessary)? I do not know how QColorMap and QColorGradient interact, but maybe I could create my own QColorGradient that is clipped at a certain value and pass it to the QColorMap?

After working a bit on that, I have found a way to do it although I do not really like it because it involves changing the sourcecode of the library. In the qcustomplot.h I have added a new enumtype to represent the new color gradient (arround line 4424) and int qcustomplot.cpp I have added a new case for the switch at line 15852 that runs the following code when the new enumtype is set:

    case gpJetTrunk:
      setColorInterpolation(ciRGB);
      setColorStopAt(0, Qt::transparent);
      setColorStopAt(1e-5, QColor(0, 0, 100));
      setColorStopAt(0.15, QColor(0, 50, 255));
      setColorStopAt(0.35, QColor(0, 255, 255));
      setColorStopAt(0.65, QColor(255, 255, 0));
      setColorStopAt(0.85, QColor(255, 30, 0));
      setColorStopAt(1-1e-5, QColor(100, 0, 0));
      setColorStopAt(1, Qt::transparent);
      break; 

After this I have realised that mybe the good solution is to create a new class inheriting from QCPColorGradient and reimplement the function QCPColorGradient::loadPresets(GradientPreset preset). Here is what I did:
class MyQCPColorGradient: public QCPColorGradient{
public:
    MyQCPColorGradient(){}
    ~MyQCPColorGradient(){}
    void loadPreset(MyQCPColorGradient::GradientPreset preset){
        QCPColorGradient::setColorInterpolation(MyQCPColorGradient::ciRGB);
        QCPColorGradient::setColorStopAt(0, Qt::transparent);
        QCPColorGradient::setColorStopAt(1e-5, QColor(0, 0, 100));
        QCPColorGradient::setColorStopAt(0.15, QColor(0, 50, 255));
        QCPColorGradient::setColorStopAt(0.35, QColor(0, 255, 255));
        QCPColorGradient::setColorStopAt(0.65, QColor(255, 255, 0));
        QCPColorGradient::setColorStopAt(0.85, QColor(255, 30, 0));
        QCPColorGradient::setColorStopAt(1-1e-5, QColor(100, 0, 0));
        QCPColorGradient::setColorStopAt(1, Qt::transparent);
    }
};

Then I use this new color gradient to set it in the QColorMap using the QColorMap::setGradient(const QCPColorGradient &gradient) method. Although everithing compiles, I only see black. any good idea on why that might happen?

I have solved this question. The problem was that when I was using this newly created class I was not calling loadPresets to set the colors.

You don't need to sublcass QCPColorGradient. The class supports setting up your own gradient, and not just the presets, out of the box (also see documentation):

QCPColorGradient myGradient;
myGradient.clearColorStops();
myGradient.setColorStopAt(0.0, QColor(...));
myGradient.setColorStopAt(0.5, QColor(...));
myGradient.setColorStopAt(1.0 QColor(...));

and then just

colorMap->setGradient(myGradient);

It makes much more sense! Thank you!

Hello, I am also trying the same thing but

QCPColorGradient myGradient;
myGradient.clearColorStops();
myGradient.setColorStopAt(0.0, QColor(...));
myGradient.setColorStopAt(0.5, QColor(...));
myGradient.setColorStopAt(1.0 QColor(...));

This gives me just black color on my screen.

In order to answer my first question. The proper way to not show specific data is by setting the alpha for the cell to 0. This is:

QCPColorMap* colorMap = new QCPColorMap(customPlot->xAxis,customPlot->yAxis);
// Set the alpha for a cell
colorMap->data()->setAlpha(10,10,0);

Shyivam, are you using some color in the QColor(...)?