2

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!

tonytonov
  • 25,060
  • 16
  • 82
  • 98
rodms
  • 371
  • 3
  • 12

3 Answers3

3

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).

Pierre
  • 31
  • 1
2

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) ]);
baptiste
  • 75,767
  • 19
  • 198
  • 294
  • did that help? I don't use Matlab anymore so can't test it – baptiste Jun 04 '13 at 10:56
  • Thank you @baptiste the code works, but haven't figure out the exact parameters to get exact ggplot2 colors. I explored SO a bit and found some [R code that does exactly what I want](http://stackoverflow.com/a/8197703/916928). Is there a way to modify 'colorscale' to emulate the output of this function? Thank you very much! – rodms Jun 05 '13 at 18:46
0

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?).

dlaehnemann
  • 671
  • 5
  • 17
  • MATLAB colors are simply a 3-vector of [R G B], so for instance pure red is [1 0 0]. A colormap/palette is then a 3xn matrix of color values. – craigim May 31 '13 at 21:22
  • Well then, that sounds easy enough. So the RGB values really are all that @eserod needs! :) – dlaehnemann Jun 01 '13 at 17:25
  • Thank you @thunk Indeed, what I need are the RBG values, but more specifically a function that can generate these values automatically. Just as ggplot2 does! :) – rodms Jun 05 '13 at 18:50