How do I set a fixed color scheme for pcolor?

I'm making a graph of measurements, and pcolor is a very good way to display these data, but everytime the data is slightly different, the color scheme changes, which is annoying. I've tried some solutions with CData, but my inexperience has left me frustrated and with no solution when I try to extrapolate how to use CData based on other people's questions.
Basically, I want the colors to be the same every time in terms of order, not number, (.0143in can't always be blue, but blue always has to be the largest number).
Any help would be great. Here's my current code for the graph:
"figure
pcolor(postnomtable)
colorbar"
where postnomtable is an m by m matrix

1 Comment

I'm not sure why you get different ordering of colors in your plots (I don't). The default colorscheme ('jet') is normally auto-scaled to have the highest number as red and the lowest as blue. The value right in between is somewhere around greenish.
You can use caxis([cmin cmax]) so specify your limits (everything outside of those limits gets the outermost color).
You can also use colormap('default') to set the current colorscheme as the default one for all future plots.

Sign in to comment.

 Accepted Answer

You do know that the colors depend not on the value of a particular element, but on the slope of a plane fitted between the 4 elements at the corner, don't you? Did you see this in the help: "With shading interp, each cell is colored by bilinear interpolation of the colors at its four vertices, using all elements of C." And you know that you're "missing" the last row and column because you read this: "The last row and column of C are not used" What is the exact call to pcolor that you used? Then, explain to me why you want to use pcolor() instead of imshow() or image().
Anyway, if you want blue to be the largest value, just flip jet. See this demo:
m = randi(3, [5 5])
pcolor(m)
flippedJetColorMap = flipud(jet(256));
colormap(flippedJetColorMap);
colorbar
Whoa! Wait a minute! Did you notice that all the 1's don't have the same color, and all the 2's don't have the same color, and that all the 3's don't have the same color? And did you notice that there are only 4 by 4 cells displayed, not 5 by 5 like the size of my "m" matrix? That doesn't seem intuitive! Now you know why I don't recommend pcolor. But at least the colormap is flipped like you want, though it doesn't really apply to anything.

6 Comments

The reason I didn't use imshow() or image() is because I didn't know about them; I haven't done much with matlab, so I've just been shooting from the hip with the internet on my side for this entire project.
Is there a way to set a value of 0 to green and make the scale relative to that?
Sure. Run this demo (the few key lines of code on colormap are way at the end.)
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
format long g;
format compact;
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'moon.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Create a green colormap
greenColorMap = zeros(256, 3); % Initialize to all black.
greenColorMap(:,2) = linspace(1, 0, 256); % Make green a ramp.
colormap(greenColorMap);
colorbar;
I couldn't get this demo to run without error, but I did try inputting
>>"greenColorMap = zeros(256, 3);
>>% Initialize to all black.
>>greenColorMap(:,2) = linspace(1, 0, 256);
>>% Make green a ramp.
>>colormap(greenColorMap);
>>colorbar;"
Which made the entire graph shades of green and black. I meant that I wanted to make 0 green, and then the full range of colors fit to the surrounding values; 0 is always green, higher values are shades of red, and lower values are shades of blue.
What was the error? Try this code:
% Create a jet colormap with zero being pure green.
myColorMap = jet(256);
% Replace 0 with pure green.
myColorMap(1, :) = [0 1 0];
colormap(myColorMap);
colorbar;
That code did exactly what I was thinking of. Good job. Thanks.
"Undefined function or method 'imtool' for input arguments of type 'char'.
Error in ==> DemoForGreenScale at 5 imtool close all; % Close all imtool figures."
I don't need that code though. The one you just posted works great.
Strange. imtool is included in the Image Processing Toolbox, which I guess you must not have. If you took out that line, then you would have hit the line saying you don't have the Image Processing Toolbox installed. If you took that out also, then it would have complained about imshow(). But you could have fixed that by using image() instead of imshow(). Anyway, glad it's working for you.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!