tab20.m 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. function map = tab20(N)
  2. % Qualitative colormap from MatPlotLib, for plots using the line ColorOrder.
  3. % In MatPlotLib 2 it is named VEGA20, for MatPlotLib 3 was renamed TAB20.
  4. %
  5. % Copyright (c) 2017-2019 Stephen Cobeldick
  6. %
  7. %%% Syntax:
  8. % map = tab20
  9. % map = tab20(N)
  10. %
  11. % For MatPlotLib 2.0 improved colormaps were created for plot lines of
  12. % categorical data. The new colormaps are introduced here:
  13. % <http://matplotlib.org/2.0.0rc2/users/dflt_style_changes.html>
  14. % VEGA10/TAB10 is the default Line Color Order for MatPlotLib 2 and 3.
  15. %
  16. % MATLAB axes ColorOrder (note that this is NOT the axes COLORMAP):
  17. % <https://www.mathworks.com/help/matlab/creating_plots/defining-the-color-of-lines-for-plotting.html>
  18. % <https://www.mathworks.com/help/matlab/graphics_transition/why-are-plot-lines-different-colors.html>
  19. %
  20. %% Examples %%
  21. %
  22. %%% PLOT using matrices:
  23. % N = 20;
  24. % axes('ColorOrder',tab20(N),'NextPlot','replacechildren')
  25. % X = linspace(0,pi*3,1000);
  26. % Y = bsxfun(@(x,n)sqrt(n)*sin(x+2*n*pi/N), X(:), 1:N);
  27. % plot(X,Y, 'linewidth',4)
  28. %
  29. %%% PLOT in a loop:
  30. % N = 20;
  31. % set(0,'DefaultAxesColorOrder',tab20(N))
  32. % X = linspace(0,pi*3,1000);
  33. % Y = bsxfun(@(x,n)sqrt(n)*sin(x+2*n*pi/N), X(:), 1:N);
  34. % for n = 1:N
  35. % plot(X(:),Y(:,n), 'linewidth',4);
  36. % hold all
  37. % end
  38. %
  39. %%% LINE using matrices:
  40. % N = 20;
  41. % set(0,'DefaultAxesColorOrder',tab20(N))
  42. % X = linspace(0,pi*3,1000);
  43. % Y = bsxfun(@(x,n)sqrt(n)*cos(x+2*n*pi/N), X(:), 1:N);
  44. % line(X(:),Y)
  45. %
  46. %% Input and Output Arguments %%
  47. %
  48. %%% Inputs (*=default):
  49. % N = NumericScalar, N>=0, an integer to define the colormap length.
  50. % = *[], use the length of the current figure's colormap (see COLORMAP).
  51. %
  52. %%% Outputs:
  53. % map = NumericMatrix, size Nx3, a colormap of RGB values between 0 and 1.
  54. %
  55. % See also TAB10 TAB20B TAB20C SET TWILIGHT VIRIDIS LINES COLORMAP PARULA
  56. if nargin<1
  57. N = size(get(gcf,'colormap'),1);
  58. else
  59. assert(isscalar(N)&&isreal(N),'First argument must be a real numeric scalar.')
  60. assert(fix(N)==N&&N>=0,'First argument must be a positive integer.')
  61. end
  62. %
  63. hex = ['#1f77b4';'#aec7e8';'#ff7f0e';'#ffbb78';'#2ca02c';'#98df8a';'#d62728';'#ff9896';'#9467bd';'#c5b0d5';'#8c564b';'#c49c94';'#e377c2';'#f7b6d2';'#7f7f7f';'#c7c7c7';'#bcbd22';'#dbdb8d';'#17becf';'#9edae5'];
  64. raw = sscanf(hex.','#%2x%2x%2x',[3,Inf]).';
  65. %
  66. map = raw(1+mod(0:N-1,size(raw,1)),:) / 255;
  67. %
  68. end
  69. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%tab20