MATLAB: How to change the colors of lines displayed on a contour plot in MATLAB

colorcolormapcontourMATLAB

I want to generate a contour plot using CONTOUR, but I want to set the line colors myself.

Best Answer

There are two ways to manually set the colors of a contour plot in MATLAB.
First, you can use a different colormap. Setting the colormap will change the colors that CONTOUR uses for each level. As an example, to create a contour plot in grayscale you can type:
colormap(gray)
contour(peaks)
The other way to set line colors in a contour plot is by changing the "ColorOrder" property of the axes in which it is plotted. CONTOUR will cycle through the "ColorOrder" matrix, assigning corresponding colors to each line. An example follows that will set the contour lines to alternate between red, green and blue.
co = [1 0 0;0 1 0;0 0 1]
set(gca,'nextplot','replacechildren')
set(gca,'colororder',co)
contour(peaks,'-')
Note that you must give '-' as the last argument to CONTOUR. This tells CONTOUR to use line objects instead of the default patch objects for contours. If this is left out, the colors will instead be assigned according to the current colormap.
If you are using MATLAB version 7.0 (R14) or higher, you can use the following procedures:
METHOD 1
co = [1 0 0;0 1 0;0 0 1]
set(gca,'nextplot','replacechildren')
set(gca,'colororder',co)
contour('v6',peaks,'-')
METHOD 2
To set all the contours at level 2 to black.
[C,h]=contour(peaks);
clabel(C,h)
set(findobj(gca,'Type','patch','UserData',2),'EdgeColor',[0 0 0])