rodms
rodms

Reputation: 391

Emulate ggplot2 default color palette in MATLAB

I was wondering if anyone knew how to emulate the ggplot2 default color palette in MATLAB? i.e the one given by scale_color_hue() in ggplot2.

Or equivalently, does anyone know how to pick evenly spaced colors around the HCL color wheel in Matlab?

Some code would be nice. Thank you very much!

Upvotes: 2

Views: 2033

Answers (3)

Pierre
Pierre

Reputation: 31

I created a ggplot2-like plotting library for Matlab called gramm, which reproduces many of ggplot2 functionalities, including its Hue-Chroma-Lightness color palette. It's on gitHub/gramm and fileexchange/gramm. You can look inside for how HCL colormaps are created (This part of gramm uses code from the PandA – Perception and Action – toolbox).

Upvotes: 3

baptiste
baptiste

Reputation: 77116

Here's a function to get equidistant hsv colours, which is more or less the default scale_colour_hue in ggplot2 for discrete values,

%Color scale in hsv
%
%colorscale(n)
%colorscale(n, 'hue', [min max])
%colorscale(n, 'saturation', saturation)
%colorscale(n, 'value', value)
%
%Input: n
%Optional: hue in [0 1]x[0 1] range (default [0.1 0.9]), 
% saturation [0 1] (default 0.5), value in [0 1] (default 0.8)
%
%Output: nx3 rgb matrix
%
%Examples: 
% n = 10;
% cols = colorscale(n, 'hue', [0.1 0.8], 'saturation' , 1, 'value', 0.5);
% 
%for aa = 1:10;
%     plot(1:10, (1:10) + aa, 'Color', cols(aa,:), 'Linewidth',2)
%     hold on
%end;
%
% % plot a matrix
% v = transpose(1:10);
% set(gca, 'ColorOrder', colorscale(5));
% set(gca,'NextPlot','replacechildren')
% plot(v, [v, v+1, v+2, v+ 3, v+4, v+5]) ;
%
function cols = colorscale(n, varargin)
p = inputParser; 
p.addRequired('n', @isnumeric);
p.addOptional('hue', [0.1 0.9], @(x) length(x) == 2 & min(x) >=0 & max(x) <= 1);
p.addOptional('saturation', 0.5, @(x) length(x) == 1);
p.addOptional('value', 0.8, @(x) length(x) == 1);

p.parse(n, varargin{:});

cols = hsv2rgb([transpose(linspace(p.Results.hue(1), p.Results.hue(2), p.Results.n)), ...
    repmat(p.Results.saturation, p.Results.n, 1), repmat(p.Results.value, n,1) ]);

Upvotes: 2

dlaehnemann
dlaehnemann

Reputation: 701

I think in general ggplot2 relies heavily on the Brewer Colour Palettes, which should thus have pallettes like the one you are looking for. So maybe just go to the above link and get the RGB values of any set you like (and cite accordingly).

And Matlab should have some way of specifying RGB colours, I'm sure (although I have no clue how to do that - maybe worth a new question on its own?).

Upvotes: 0

Related Questions