# 3.3.16 Contour Plot from Matrix

## Version Info

Minimum Origin Version Required: Origin 7.5

## Example

This example to show how to add contour plotting and then use the following two functions to custom Z levels and fill colors. Please refer to DataPlot::SetColormap for more information.

void plot_contour()
{
MatrixPage mp;
mp.Create("origin");
MatrixLayer ml = mp.Layers(0);
Matrix mat(ml);
mat.SetSize(10, 10);
for(int ii=0; ii<mat.GetNumRows();ii++)
for(int jj=0; jj<mat.GetNumCols();jj++)
mat[ii][jj] = sin(ii+jj);

MatrixObject mobj = ml.MatrixObjects();

GraphPage gp;
gp.Create("contour", CREATE_HIDDEN); // use template, create as hidden to avoid unneeded drawing
GraphLayer glay = gp.Layers();
glay.Rescale();

DataPlot dp = glay.DataPlots(nPlot);
if(dp)
{
set_contour_colormap(dp);
}
gp.SetShow();
}

void set_contour_colormap(DataPlot& dp)
{
vector vLevels;
BOOL bLogScale;
if( !dp.GetColormap(vLevels, bLogScale) )
return;

// shrink or extend z levels
int nLevels = vLevels.GetSize();
if( nLevels >= 5)
nLevels -= 2;
else
nLevels += 2;

double min = vLevels[0], max = vLevels[nLevels-1];
double step = (max - min)/(nLevels-1);
vLevels.Data(min, max, step);

// set z levels
if( !dp.SetColormap(vLevels) )
return;

vector<uint> vColors(nLevels-1); // the size  of vColors must be nLevels-1
for(int ii = 0; ii < vColors.GetSize(); ii++)
{
int gg = 255* ii / (nLevels-1);
int bb = 255 * (nLevels-ii-1)/(nLevels-1);
int nRGB = RGB(0, gg, bb);
vColors[ii] = RGB2OCOLOR(nRGB);
}

// call DataPlot::SetColormap(const vector<double>& vz, BOOL bLogScale=FALSE) to set z levels
if( !dp.SetColormap(vLevels) )
{
printf("Fail to set z levels\n");
return;
}

// call DataPlot::SetColormap(TreeNode& trColormap) to set colormap
Tree tr;
tr.ColorMap.Details.Colors.nVals = vColors;
if(!dp.SetColormap(tr))
{
printf("Fail to set colormap\n");
return;
}

}