Azial
Azial

Reputation: 189

Matlab: same colormap for hist, plot and mesh

I have datasets for 5 different frequencies in a matrix and I want to illustrate them using plot, hist and mesh. However, each plot type is using different colormaps (see picture), so I need a legend for every plot.

Is there a way to set the same colormap for all plot types, or specify one for each of them? Another thing thats strange: I can set the colormap for hist using the figure tools for example, but not for the normal plot. For the mesh I have to use a hold on loop, so I guess setting the color here is different from defining a colormap?

enter image description here

Edit:

Here is a minimal example. Its still not working, see comments in the code below.

clear all;
close all;
clc;

% make up some data with the original format
freqLen = 5;
data = zeros(10, 3, 3, freqLen);
data(:, :, :, 1) = rand(10, 3, 3);
data(:, :, :, 2) = rand(10, 3, 3)+1;
data(:, :, :, 3) = rand(10, 3, 3)+2;
data(:, :, :, 4) = rand(10, 3, 3)+3;
data(:, :, :, 5) = rand(10, 3, 3)+4;
% reshape data so we get a vector for each frequency
dataF = reshape(data, [10*3*3, freqLen]);

% prepare colors for plot, try to get 5 colors over the range of colormap
% but I get wrong colors using both methods below!
%cols = colormap(jet);
%cols = cols(1:round(length(cols)/length(freqGHz)):end, :);
cols = jet(freqLen);

% plot samples in 3D
figure('Position', [0 0 1000 1000]);
subplot(211);
hold on;
for iF = 1:freqLen
    dataThisF = dataF(:, iF);
    data3D = reshape(dataThisF, [10*3, 3]);
    mesh(data3D);
    % try to give each "holded" mesh a different color. Not working!
    % after the loop, all meshes have the last color
    set(get(gca, 'child'), 'FaceColor', 'w', 'EdgeColor', cols(iF, :));
end
view(60, 20);

% plot samples
subplot(223);
hold on;
for iF = 1:freqLen
    % the loop is not avoidable
    % because matlab maps the colors wrong when plotting as a matrix
    % at least its not using the colormap colors
    plot(dataF(:, iF), 'Color', cols(iF, :));
end

% plot histogram
subplot(224);
% actually the only one which is working as intended, horray!
hist(dataF, 50);

enter image description here

How can I give a holded mesh a single color, different from the others? How can I map the correct jet colormap when plotting a matrix using simple line plot, or at least get 5 colors from the jet colormap (jet(5) give 5 different colors, but not from start to end)?

Upvotes: 1

Views: 1113

Answers (2)

Hoki
Hoki

Reputation: 11802

What you are talking about is mainly the ColorOrder property (and not the colormap of the figure).

The link for the colororder given just above will explain you how to force Matlab to use a given set of colors for all the plots. It works perfectly fine for plot. You wouldn't need a loop, just define the DefaultColororder property of the figure before the plots then plot all your series in one single call, Matlab will assign the color of each plot according to the order you defined earlier.

For mesh and hist it is not that simple unfortunately, so you will have to run a loop to specify the color or each graphic object. To modify a graphic object property (like the color) after it has been created, you have to use the set method, or even the direct dot notation if you're using a Matlab version >= 2014b. For both methods you needs to have the handle of the graphic object, so usually the easiest when you know you'll need that is to retrieve the graphic object handle at the time of creation*.

*instead of dirty hack like get(gca, 'child'). This is quite prone to errors and as a matter of fact was wrong in your case. Your code wouldn't color properly because you were not getting the right graphic handle this way.

The code below plots all your graphs, retrieve the handle of each graphic object, then assign the colors in the final loop.


%// Get a few colors
cols = jet(freqLen);

% plot samples in 3D
figure('Position', [0 0 1000 1000]);
set( gcf , 'DefaultAxesColorOrder',cols) %// set the line color order for this figure

subplot(2,1,1,'NextPlot','add');         %// 'NextPlot','add' == "hold on" ;
for iF = 1:freqLen
    dataThisF = dataF(:, iF);
    data3D = reshape(dataThisF, [10*3, 3]);
    h.mesh(iF) = mesh(data3D) ;         %// plot MESH and retrieve handles

    %// You can set the color here direct, or in the last final "coloring" loop
    %// set( h.mesh(iF) , 'FaceColor', 'w', 'EdgeColor', cols(iF, :));
end
view(60, 20);

%// plot samples
subplot(223);
h.plots = plot(dataF);                  %// plot LINES and retrieve handles

%// plot histogram
subplot(224);
[counts,centers] = hist(dataF, 50 ) ;   %// get the counts values for each series
h.hist = bar(centers,counts) ;          %// plot HISTOGRAM and retrieve handles

%// now color every series with the same color
for iF = 1:freqLen
    thisColor = cols(iF, :) ;
    set( h.mesh(iF)  , 'EdgeColor' , thisColor  , 'FaceColor', 'w' );
    set( h.hist(iF)  , 'EdgeColor' , thisColor  , 'FaceColor' , thisColor )
    %// this is actually redundant, the colors of the plots were already right from the
    %// beginning thanks to the "DefaultColorOrder" property we specified earlier
    set( h.plots(iF) , 'Color'     , thisColor ) 
end

Will land you the following figure: colororder

Upvotes: 2

IKavanagh
IKavanagh

Reputation: 6187

UPDATE: The original question asked

Is there a way to set the same colormap for all plot types, or specify one for each of them?

this answer, answers that question with colormap taken to mean colormap in MATLAB literally.


You can set the colormap for a figure using colormap. You could use one of the many built in colormaps or specify your own.


An example using mesh and the builtin hsv colormap could be

figure;
mesh(data);
colormap(hsv);

This would apply a colormap based on

MATLAB hsv color

to your figure. You can also create your own colormap like

map = [1, 0, 0,
       1, 1, 1,
       0, 1, 0,
       0, 0, 0];
colormap(map);

which would create a colormap with the colours Red, White, Blue and Black.

MATLAB Flag colormap


The MATLAB documentation contains extensive information on the use of colormap.

Upvotes: 0

Related Questions