MATLAB Answers

## Can we use more than one colormap in one plot ?

Asked by Jujhar Khurana

### Jujhar Khurana (view profile)

on 30 Mar 2013

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
```

## Products

No products are associated with this question.

## 2 Answers

### per isakson (view profile)

Answer by per isakson

### per isakson (view profile)

on 30 Mar 2013

Yes, but it is not simple. See the links in my answer http://www.mathworks.se/matlabcentral/answers/68939#answer_80274

Jujhar Khurana

### Jujhar Khurana (view profile)

on 31 Mar 2013

I tried to freeze the colors, it is not giving any error but taking forever to run. And finally I have to stop the process.

per isakson

### per isakson (view profile)

on 31 Mar 2013

Questions on multiple colormaps appears on a regular basis here and in the Newsgroup.

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.

And one more link: multiple colormaps in matlab

Jujhar Khurana

### Jujhar Khurana (view profile)

on 31 Mar 2013

Might be possible that I am doing something wrong, I will check it again. Thanks per isakson and Walter

### Image Analyst (view profile)

Answer by Image Analyst

### Image Analyst (view profile)

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;
```

Jujhar Khurana

### Jujhar Khurana (view profile)

on 2 Apr 2013

Well, I am using Patches only ( you can in my code above ) and you only suggested me to use it in one of my other questions.

Anyways, please give me one more advice:

What changes do I need to do in my current code(given in the question) ? Becuase the code you suggested above( ofcourse, it will work) but it is completely new for me, so I am not getting how to incorporate it in to my problem....

Image Analyst

### Image Analyst (view profile)

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

### Jujhar Khurana (view profile)

on 2 Apr 2013

Thanks All of you.

#### Join the 15-year community celebration.

Play games and win prizes!

Learn more

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

### Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

### MATLAB Academy

New to MATLAB?

Learn MATLAB today!