Can we use more than one colormap in one plot ?
Show older comments
In my data, I have 6 types of roads and then for each road, I have 24 hours of data. In the function below, I am getting 24 plots as output which represent 1-24 hours respectively.
What I need to achieve is I need to show different roads (1-6) in different colorscale so that they can be differentiated. I have tried as under but it does not work. And also, how can print different colorbars associated with different ColorMap ?
PLease help.
function testplotLine
clear all; clc; close all; format short g;
% read data
InFilename = sprintf('Idontknow.csv');
LinkMat = csvread(InFilename);
UniqueHours = sortrows(unique(LinkMat(:,5)));
LinkMat(:,6) = log(LinkMat(:,6)+1);
fprintf('max of Uniquehours %u ', max(UniqueHours));
for j = 1:size(UniqueHours, 1)
fprintf('Processing hour:%u....',UniqueHours(j));
RowIndex = find(LinkMat(:,5) == UniqueHours(j));
fprintf('size of rowindex %u ', size(RowIndex));
% extract those record and store them into TempMat
TempMat = LinkMat(RowIndex,:);
hold on;
for i = 1:size(TempMat,1)
z = TempMat(i,6);
if(TempMat(i,7) == 1) % this is road type 1
patch('Vertices', [TempMat(i,1) TempMat(i,2); TempMat(i,3) TempMat(i,4)], 'Faces', [1 2], 'FaceVertexCData', [z; z], 'EdgeColor', 'interp', 'LineWidth', 1.5);
colormap bone;
end
if (TempMat(i,7) == 2) % this is road type 2
patch('Vertices', [TempMat(i,1) TempMat(i,2); TempMat(i,3) TempMat(i,4)], 'Faces', [1 2], 'FaceVertexCData', [z; z], 'EdgeColor', 'interp', 'LineWidth', 1.5, 'LineStyle', ' - ');
colormap copper;
end
if (TempMat(i,7) == 3)
patch('Vertices', [TempMat(i,1) TempMat(i,2); TempMat(i,3) TempMat(i,4)], 'Faces', [1 2], 'FaceVertexCData', [z; z], 'EdgeColor', 'interp','LineWidth', 1.5);
colormap autumn;
end
if (TempMat(i,7) == 4)
patch('Vertices', [TempMat(i,1) TempMat(i,2); TempMat(i,3) TempMat(i,4)], 'Faces', [1 2], 'FaceVertexCData', [z; z], 'EdgeColor', 'interp', 'LineWidth', 1.5);
colormap summer;
end
if (TempMat(i,7) == 5)
patch('Vertices', [TempMat(i,1) TempMat(i,2); TempMat(i,3) TempMat(i,4)], 'Faces', [1 2], 'FaceVertexCData', [z; z], 'EdgeColor', 'interp','LineWidth', 1.5);
colormap pink;
end
if (TempMat(i,7) == 6)
patch('Vertices', [TempMat(i,1) TempMat(i,2); TempMat(i,3) TempMat(i,4)], 'Faces', [1 2], 'FaceVertexCData', [z; z], 'EdgeColor', 'interp', 'LineWidth', 1.5);
colormap jet;
end
end
axis off;
set(gca,'position',[0,0, .9, .9]);
colorbar; % I need to print colorbars also....*
hold off;
RootDirectory = 'C:\Users\JSK\Desktop\25March\chutiyapa\more chutiyapa';
OutFileName = sprintf('ORTotHoursCO_Hour_%u.jpg',UniqueHours(j));
TotalFileName = [RootDirectory,'\',OutFileName];
print(TotalFileName)
print('-djpeg','-r300',TotalFileName);
close all;
end
end
Answers (2)
per isakson
on 30 Mar 2013
0 votes
Yes, but it is not simple. See the links in my answer http://www.mathworks.se/matlabcentral/answers/68939#answer_80274
4 Comments
Walter Roberson
on 31 Mar 2013
Or, more properly, "Not really, but you can fake it by following the links that Per gives."
Jujhar Khurana
on 31 Mar 2013
per isakson
on 31 Mar 2013
Edited: per isakson
on 2 Apr 2013
The tech-note, How do use multiple colormaps in a single figure?, is a solution proposed by The MathWorks. The FAQ refers to this tech-note. I rely on this approach.
There is a blog post, Using multiple colormaps in a single figure, which describes the use of freezeColors.
I cannot guess why you have problems with freezeColors. It is a highly rated FEX contribution.
Jujhar Khurana
on 31 Mar 2013
Image Analyst
on 1 Apr 2013
Jujhar, run my demo and I think you'll be able to see how you can do it for your image by making sure each range of your data occupies a different range of the 256 colors available for the pseudocolor lookup table (colormap).
clc;
clearvars;
close all;
imtool close all; % Close all imtool figures.
workspace;
format longg;
format compact;
fontSize = 20;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.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);
% The colormap can only be 256 rows long (otherwise it repeats - you can't assign them).
% So make each image only a portion of the 256 gray levels.
% Define a colormap that consists of 4 separate colormaps.
% So make each colormap section 256/4 = 64 rows long.
numberOfImages = 4;
cmap = [gray(256/numberOfImages);...
jet(256/numberOfImages);...
copper(256/numberOfImages);...
winter(256/numberOfImages)];
% Apply the colormap to the figure.
colormap(cmap);
% 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')
% if there are 4 images, each needs to be displayed in 256/4 or 64 gray levels.
% Use imadjust() to map the original gray image into that range.
% Generate the first image.
subplot(2, 2, 1);
% Map into 0-63.
firstImage = imadjust(grayImage, [], [0 1/numberOfImages]);
% Display image mapped into the new intensity range.
image(firstImage);
caption = sprintf('Gray Level Image mapped into the range 0-63\nso that the Gray colormap will be applied');
title(caption, 'FontSize', fontSize);
colorbar;
% Generate the other images such that
subplot(2, 2, 2);
% Map into 64-127.
secondImage = imadjust(grayImage, [], [1/numberOfImages 2/numberOfImages]);
% Display image mapped into the new intensity range.
image(secondImage);
caption = sprintf('Gray Level Image mapped into the range 64-127\nso that the Jet colormap will be applied');
title(caption, 'FontSize', fontSize);
colorbar;
subplot(2, 2, 3);
% Map into 128-191.
thirdImage = imadjust(grayImage, [], [2/numberOfImages, 3/numberOfImages]);
% Display image mapped into the new intensity range.
image(thirdImage);
caption = sprintf('Gray Level Image mapped into the range 128-191\nso that the Copper colormap will be applied');
title(caption, 'FontSize', fontSize);
colorbar;
subplot(2, 2, 4);
% Map into 192-255.
fourthImage = imadjust(grayImage, [], [3/numberOfImages, 4/numberOfImages]);
% Display image mapped into the new intensity range.
image(fourthImage);
caption = sprintf('Gray Level Image mapped into the range 192-255\nso that the Winter colormap will be applied');
title(caption, 'FontSize', fontSize);
colormap(cmap)
colorbar;
11 Comments
Image Analyst
on 1 Apr 2013
Jujhar - you still there? Did you try any of these answers?
Jujhar Khurana
on 1 Apr 2013
Jujhar Khurana
on 1 Apr 2013
Image Analyst
on 1 Apr 2013
Jujhar Khurana
on 2 Apr 2013
Walter Roberson
on 2 Apr 2013
Tricky. In order to do that you would have to use transparent figures that overlayed each other, with a different colormap for each road.
It would be much easier if you do not use pseudocolors and instead generated RGB for each item. You can calculate colormap indices for any data point as
min(number_of_colormap_entries, 1 + floor( (datapoint - min_for_this_kind_of_data) ./ (max_for_this_kind_of_data - min_for_this_kind_of_data) * number_of_colormap_entries) )
the min() is to adjust for the fact that values exactly equal to max_for_this_kind_of_data would become 1 exactly after the division, multiply that by the number of entries would give the number of entries exactly, then floor() of that would be the number of entries exactly, add 1 would be 1 more than the number of entries. Any other value than the exact maximum can use the numeric formula. It is possible to tweak the numeric formula instead using eps() appropriately, but it gets harder to understand whether all the boundary cases get done exactly right.
Once you have the indices into the colormap,
rgbimage = reshape( TheColormap(DataIndices(:), :), size(YourData,1), size(YourData,2), 3);
and create an array DataAlpha of the locations where the data is to show up (value 1) or not (value 0). Then,
image(rgbimage, 'AlphaData', DataAlpha)
Jujhar Khurana
on 2 Apr 2013
Walter Roberson
on 2 Apr 2013
Or if your roads are not thick, then use patches to draw them. You can adjust the patch color at each vertex or each edge.
Jujhar Khurana
on 2 Apr 2013
Image Analyst
on 2 Apr 2013
I'm with Walter - I'd just create a color image - a true color 3D RGB image made up of 3 color planes. You can use imline() to burn a line into each color plane with the appropriate R, G, and B values. I think that's easier than using patch() to create lines.
Jujhar Khurana
on 2 Apr 2013
Categories
Find more on Images in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!