I am quite new to Mathematica and I am searching for a way to create a smooth colormap based on the samples given by ColorBrewer. Is it possible to create a interpolated function from those sample points?

  • $\begingroup$ By samples, do you mean color samples, i.e, color schemes? $\endgroup$ – VLC Nov 16 '12 at 13:47
  • $\begingroup$ yes, as you can see on the website there are only color samples (e.g. 6 rgb values) $\endgroup$ – MrMuh Nov 16 '12 at 13:48
  • 1
    $\begingroup$ Look up Blend[]... $\endgroup$ – J. M. will be back soon Nov 16 '12 at 13:54
  • $\begingroup$ How do you want to use your color map in Mathematica? How you incorporate your six RGB values into Blend will depend on you intended use. $\endgroup$ – m_goldberg Nov 16 '12 at 15:30

I enter RGB values in a matrix using Ctrl+, and Ctrl+Enter (tutorial):

Mathematica graphics

{{237, 248, 177}, {127, 205, 187}, {44, 127, 184}}

Then create a color function cf:

With[{rgb = RGBColor @@@ (dat/255)},
 cf = Blend[rgb, #] &;

And use it:

ArrayPlot[{Range@10}, ColorFunction -> cf]

Mathematica graphics

 Table[Sin[j^2 + i], {i, 0, Pi, Pi/5}, {j, 0, Pi, Pi/5}],
 Mesh -> None,
 InterpolationOrder -> 0,
 ColorFunction -> cf

Mathematica graphics

  • $\begingroup$ Any reason for the With[] filigree (e.g. pedagogical reasons)? cf = Blend[RGBColor @@@ (dat/255), #] &; seems to work just as well... $\endgroup$ – J. M. will be back soon Nov 16 '12 at 14:29
  • $\begingroup$ great postings here! one last question about colorfunctions: when data values ranging from 0 ... 30 and I want to set a maximum for the colorfunction e.g. 0...20 -> all above 20 is colored with the same color (e.g.color3). is this somehow possible to achive ?! $\endgroup$ – MrMuh Nov 16 '12 at 14:37
  • $\begingroup$ @J.M. that requires dat to remain defined, does it not? My intention was to create an independent function cf without any traps for beginners. Also, there should be less re-evaluation this way. $\endgroup$ – Mr.Wizard Nov 16 '12 at 14:37
  • $\begingroup$ @MrMuh the function cf is automatically defined over a range of zero to one. Arguments higher than one produce the same output value, e.g.: cf /@ Range[0, 2, 1/3]. You probably want to establish your color range outside of the cf function itself, that is with the plotting function's options, etc. Perhaps you would post an additional question about this so it can be addressed more completely? $\endgroup$ – Mr.Wizard Nov 16 '12 at 14:41
  • $\begingroup$ @MrMuh: you'll probably want to use Rescale[] in conjunction with Blend[], then... $\endgroup$ – J. M. will be back soon Nov 16 '12 at 14:50

If these are the RGB values of the color scheme:

color1 = {229, 245, 249};
color2 = {153, 216, 201};
color3 = {44, 162, 95};

you can blend them in this way:

Graphics[Table[{Blend[{{0, RGBColor[color1/255]}, {.5, 
 RGBColor[color2/255]}, {1, RGBColor[color3/255]}}, x], 
 Disk[{8 x, 0}]}, {x, 0, 1, 1/8}]]

enter image description here

  • 1
    $\begingroup$ Since the colors are equispaced, one could use the more compact Blend[{RGBColor[color1/255], RGBColor[color2/255], RGBColor[color3/255]}, x] instead. $\endgroup$ – J. M. will be back soon Nov 16 '12 at 14:02
  • $\begingroup$ awesome work! hm ok maybe you know how to use get the colorfunction into a ListPlot3D? $\endgroup$ – MrMuh Nov 16 '12 at 14:22

Here's how to make all the color schemes in ColorBrewer available in Mathematica:

ColorBrewer =
    Association /@ Association[Import["http://colorbrewer2.org/export/colorbrewer.json"] /.
    {s_String /; StringMatchQ[s, NumberString] :> FromDigits[s], 
     s_String /; StringMatchQ[s, "rgb(*)"] :> Interpreter["Color"][s]} /. 
    v_ /; VectorQ[v, ColorQ] :> With[{cols = v}, Blend[cols, #] &]];

Thus, one can now retrieve the color schemes in Wizard's and VLC's answers like so:

{ColorBrewer["YlGnBu", 3], ColorBrewer["BuGn", 3]}


  • 1
    $\begingroup$ I love the streak you are on, getting information from web pages! For this one, how could I modify it so that I always get the list with the most colors? For example, compare {BarLegend[{ColorBrewer["Spectral", 10], {0, 1}}], BarLegend[{ColorBrewer["Spectral", 3], {0, 1}}]} - I would always like the first one. The maximum number of colors for any given map is different, so I don't want to have to experiment to see that "PuRd" can do 9 colors, but "Spectral" can give 11. $\endgroup$ – Jason B. May 23 '16 at 15:36
  • $\begingroup$ Hmm, I'll think about it; a hackish method would be to apply KeySort[] to all the records of ColorBrewer and take the penultimate entry (since the last entry gives the class). $\endgroup$ – J. M. will be back soon May 23 '16 at 15:41
  • 1
    $\begingroup$ One could also start with Import["http://colorbrewer2.org/export/colorbrewer.json", "RawJSON"], thus the Association muscles get some more training. $\endgroup$ – Karsten 7. May 23 '16 at 15:53
  • $\begingroup$ @Karsten, I actually tried that at first, but then it seems the associations become opaque from replacement rules (as I used here); that's why I fell back to normal JSON importing. $\endgroup$ – J. M. will be back soon May 23 '16 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.