Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

linspecer.m 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. % function lineStyles = linspecer(N)
  2. % This function creates an Nx3 array of N [R B G] colors
  3. % These can be used to plot lots of lines with distinguishable and nice
  4. % looking colors.
  5. %
  6. % lineStyles = linspecer(N); makes N colors for you to use: lineStyles(ii,:)
  7. %
  8. % colormap(linspecer); set your colormap to have easily distinguishable
  9. % colors and a pleasing aesthetic
  10. %
  11. % lineStyles = linspecer(N,'qualitative'); forces the colors to all be distinguishable (up to 12)
  12. % lineStyles = linspecer(N,'sequential'); forces the colors to vary along a spectrum
  13. %
  14. % % Examples demonstrating the colors.
  15. %
  16. % LINE COLORS
  17. % N=6;
  18. % X = linspace(0,pi*3,1000);
  19. % Y = bsxfun(@(x,n)sin(x+2*n*pi/N), X.', 1:N);
  20. % C = linspecer(N);
  21. % axes('NextPlot','replacechildren', 'ColorOrder',C);
  22. % plot(X,Y,'linewidth',5)
  23. % ylim([-1.1 1.1]);
  24. %
  25. % SIMPLER LINE COLOR EXAMPLE
  26. % N = 6; X = linspace(0,pi*3,1000);
  27. % C = linspecer(N)
  28. % hold off;
  29. % for ii=1:N
  30. % Y = sin(X+2*ii*pi/N);
  31. % plot(X,Y,'color',C(ii,:),'linewidth',3);
  32. % hold on;
  33. % end
  34. %
  35. % COLORMAP EXAMPLE
  36. % A = rand(15);
  37. % figure; imagesc(A); % default colormap
  38. % figure; imagesc(A); colormap(linspecer); % linspecer colormap
  39. %
  40. % See also NDHIST, NHIST, PLOT, COLORMAP, 43700-cubehelix-colormaps
  41. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  42. % by Jonathan Lansey, March 2009-2013 – Lansey at gmail.com %
  43. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  44. %
  45. %% credits and where the function came from
  46. % The colors are largely taken from:
  47. % http://colorbrewer2.org and Cynthia Brewer, Mark Harrower and The Pennsylvania State University
  48. %
  49. %
  50. % She studied this from a phsychometric perspective and crafted the colors
  51. % beautifully.
  52. %
  53. % I made choices from the many there to decide the nicest once for plotting
  54. % lines in Matlab. I also made a small change to one of the colors I
  55. % thought was a bit too bright. In addition some interpolation is going on
  56. % for the sequential line styles.
  57. function lineStyles=linspecer(N,varargin)
  58. if nargin==0 % return a colormap
  59. lineStyles = linspecer(128);
  60. return;
  61. end
  62. if ischar(N)
  63. lineStyles = linspecer(128,N);
  64. return;
  65. end
  66. if N<=0 % its empty, nothing else to do here
  67. lineStyles=[];
  68. return;
  69. end
  70. % interperet varagin
  71. qualFlag = 0;
  72. colorblindFlag = 0;
  73. if ~isempty(varargin)>0 % you set a parameter?
  74. switch lower(varargin{1})
  75. case {'qualitative','qua'}
  76. if N>12 % go home, you just can't get this.
  77. warning('qualitiative is not possible for greater than 12 items, please reconsider');
  78. else
  79. if N>9
  80. warning(['Default may be nicer for ' num2str(N) ' for clearer colors use: whitebg(''black''); ']);
  81. end
  82. end
  83. qualFlag = 1;
  84. case {'sequential','seq'}
  85. lineStyles = colorm(N);
  86. return;
  87. case {'white','whitefade'}
  88. lineStyles = whiteFade(N);return;
  89. case 'red'
  90. lineStyles = whiteFade(N,'red');return;
  91. case 'blue'
  92. lineStyles = whiteFade(N,'blue');return;
  93. case 'green'
  94. lineStyles = whiteFade(N,'green');return;
  95. case {'gray','grey'}
  96. lineStyles = whiteFade(N,'gray');return;
  97. case {'colorblind'}
  98. colorblindFlag = 1;
  99. otherwise
  100. warning(['parameter ''' varargin{1} ''' not recognized']);
  101. end
  102. end
  103. % *.95
  104. % predefine some colormaps
  105. set3 = colorBrew2mat({[141, 211, 199];[ 255, 237, 111];[ 190, 186, 218];[ 251, 128, 114];[ 128, 177, 211];[ 253, 180, 98];[ 179, 222, 105];[ 188, 128, 189];[ 217, 217, 217];[ 204, 235, 197];[ 252, 205, 229];[ 255, 255, 179]}');
  106. set1JL = brighten(colorBrew2mat({[228, 26, 28];[ 55, 126, 184]; [ 77, 175, 74];[ 255, 127, 0];[ 255, 237, 111]*.85;[ 166, 86, 40];[ 247, 129, 191];[ 153, 153, 153];[ 152, 78, 163]}'));
  107. set1 = brighten(colorBrew2mat({[ 55, 126, 184]*.85;[228, 26, 28];[ 77, 175, 74];[ 255, 127, 0];[ 152, 78, 163]}),.8);
  108. % colorblindSet = {[215,25,28];[253,174,97];[171,217,233];[44,123,182]};
  109. colorblindSet = {[215,25,28];[253,174,97];[171,217,233]*.8;[44,123,182]*.8};
  110. set3 = dim(set3,.93);
  111. if colorblindFlag
  112. switch N
  113. % sorry about this line folks. kind of legacy here because I used to
  114. % use individual 1x3 cells instead of nx3 arrays
  115. case 4
  116. lineStyles = colorBrew2mat(colorblindSet);
  117. otherwise
  118. colorblindFlag = false;
  119. warning('sorry unsupported colorblind set for this number, using regular types');
  120. end
  121. end
  122. if ~colorblindFlag
  123. switch N
  124. case 1
  125. lineStyles = { [ 55, 126, 184]/255};
  126. case {2, 3, 4, 5 }
  127. lineStyles = set1(1:N);
  128. case {6 , 7, 8, 9}
  129. lineStyles = set1JL(1:N)';
  130. case {10, 11, 12}
  131. if qualFlag % force qualitative graphs
  132. lineStyles = set3(1:N)';
  133. else % 10 is a good number to start with the sequential ones.
  134. lineStyles = cmap2linspecer(colorm(N));
  135. end
  136. otherwise % any old case where I need a quick job done.
  137. lineStyles = cmap2linspecer(colorm(N));
  138. end
  139. end
  140. lineStyles = cell2mat(lineStyles);
  141. end
  142. % extra functions
  143. function varIn = colorBrew2mat(varIn)
  144. for ii=1:length(varIn) % just divide by 255
  145. varIn{ii}=varIn{ii}/255;
  146. end
  147. end
  148. function varIn = brighten(varIn,varargin) % increase the brightness
  149. if isempty(varargin),
  150. frac = .9;
  151. else
  152. frac = varargin{1};
  153. end
  154. for ii=1:length(varIn)
  155. varIn{ii}=varIn{ii}*frac+(1-frac);
  156. end
  157. end
  158. function varIn = dim(varIn,f)
  159. for ii=1:length(varIn)
  160. varIn{ii} = f*varIn{ii};
  161. end
  162. end
  163. function vOut = cmap2linspecer(vIn) % changes the format from a double array to a cell array with the right format
  164. vOut = cell(size(vIn,1),1);
  165. for ii=1:size(vIn,1)
  166. vOut{ii} = vIn(ii,:);
  167. end
  168. end
  169. %%
  170. % colorm returns a colormap which is really good for creating informative
  171. % heatmap style figures.
  172. % No particular color stands out and it doesn't do too badly for colorblind people either.
  173. % It works by interpolating the data from the
  174. % 'spectral' setting on http://colorbrewer2.org/ set to 11 colors
  175. % It is modified a little to make the brightest yellow a little less bright.
  176. function cmap = colorm(varargin)
  177. n = 100;
  178. if ~isempty(varargin)
  179. n = varargin{1};
  180. end
  181. if n==1
  182. cmap = [0.2005 0.5593 0.7380];
  183. return;
  184. end
  185. if n==2
  186. cmap = [0.2005 0.5593 0.7380;
  187. 0.9684 0.4799 0.2723];
  188. return;
  189. end
  190. frac=.95; % Slight modification from colorbrewer here to make the yellows in the center just a bit darker
  191. cmapp = [158, 1, 66; 213, 62, 79; 244, 109, 67; 253, 174, 97; 254, 224, 139; 255*frac, 255*frac, 191*frac; 230, 245, 152; 171, 221, 164; 102, 194, 165; 50, 136, 189; 94, 79, 162];
  192. x = linspace(1,n,size(cmapp,1));
  193. xi = 1:n;
  194. cmap = zeros(n,3);
  195. for ii=1:3
  196. cmap(:,ii) = pchip(x,cmapp(:,ii),xi);
  197. end
  198. cmap = flipud(cmap/255);
  199. end
  200. function cmap = whiteFade(varargin)
  201. n = 100;
  202. if nargin>0
  203. n = varargin{1};
  204. end
  205. thisColor = 'blue';
  206. if nargin>1
  207. thisColor = varargin{2};
  208. end
  209. switch thisColor
  210. case {'gray','grey'}
  211. cmapp = [255,255,255;240,240,240;217,217,217;189,189,189;150,150,150;115,115,115;82,82,82;37,37,37;0,0,0];
  212. case 'green'
  213. cmapp = [247,252,245;229,245,224;199,233,192;161,217,155;116,196,118;65,171,93;35,139,69;0,109,44;0,68,27];
  214. case 'blue'
  215. cmapp = [247,251,255;222,235,247;198,219,239;158,202,225;107,174,214;66,146,198;33,113,181;8,81,156;8,48,107];
  216. case 'red'
  217. cmapp = [255,245,240;254,224,210;252,187,161;252,146,114;251,106,74;239,59,44;203,24,29;165,15,21;103,0,13];
  218. otherwise
  219. warning(['sorry your color argument ' thisColor ' was not recognized']);
  220. end
  221. cmap = interpomap(n,cmapp);
  222. end
  223. % Eat a approximate colormap, then interpolate the rest of it up.
  224. function cmap = interpomap(n,cmapp)
  225. x = linspace(1,n,size(cmapp,1));
  226. xi = 1:n;
  227. cmap = zeros(n,3);
  228. for ii=1:3
  229. cmap(:,ii) = pchip(x,cmapp(:,ii),xi);
  230. end
  231. cmap = (cmap/255); % flipud??
  232. end
  233. %%
  234. % LICENSE
  235. %
  236. % Copyright (c) 2015, Jonathan C. Lansey
  237. % All rights reserved.
  238. %
  239. % Redistribution and use in source and binary forms, with or without
  240. % modification, are permitted provided that the following conditions are
  241. % met:
  242. % * Redistributions of source code must retain the above copyright
  243. % notice, this list of conditions and the following disclaimer.
  244. % * Redistributions in binary form must reproduce the above copyright
  245. % notice, this list of conditions and the following disclaimer in
  246. % the documentation and/or other materials provided with the distribution
  247. % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  248. % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  249. % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  250. % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  251. % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  252. % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  253. % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  254. % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  255. % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  256. % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  257. % POSSIBILITY OF SUCH DAMAGE.
  258. %
  259. %
  260. %%