Is there a limit for schemeColors[k] on k when using scaleOrdinal?

Hi all, another newbie question
I’ve tried playing with color schemes and it works well. However from the documentation to create ordinal scales from continuous schemes with the k parameter, it seems the highest value I can do is 9 (or 10 in one case).
i.e.
const blues = d3.scaleOrdinal(d3.schemeBlues[9]); as in the documentations and many examples is ok.
but const blues = d3.scaleOrdinal(d3.schemeBlues[k > 9]); Why?

I found a notebook getting more with scaleSequential and quantize but I thought that feels a bit too much when you have a built-in parameter.

d3.schemeBlues is simply an array. You could always augment it with interpolation between values, if you like.

thanks a lot! I am missing something here probably, why is the maximum size 9 ?

The limit of 9 entries here is likely arbitrary, but there has to be some limit (you can’t have an infinitely sized array). None of us are the original author of that code, so we can’t really say why it is specifically 9.

1 Like

There are quite a few d3.scheme* functions in the d3-scale-chromatic module and associated with each sequential scale is a corresponding interpolated version. Thus, you’ve got

  • d3.schemeBlues and d3.interpolateBlues
  • d3.schemeReds and d3.interpolateReds
  • and several others pairs.

Note that the interpolated version is literally an interpolation of the sequential version. Here’s a look at how the sequential hues fit together with the interpolation:


While the list of colors is useful to have around, another point is to get ultimately to the interpolated version. Once you have that, of course, it’s easy to generate finer discrete versions, if you like. Here are 11 blues, for example:

These color schemes were all based on ColorBrewer, which was an NSF funded project that put a lot of thought into how to represent color on maps back in 2001/2002.

These images above were all generated by Mike Bostock’s Color Scheme notebook.

2 Likes

Yes, I was figuring I am missing something in the documentation because that’s kinda what its for. Thanks.

Thanks a lot! My question (why 9?) came from going over this notebook and two or three others as well as the documentation. I guess I resign myself to just use sequential scales with interpolation or quantization :sweat_smile: . thanks for the patience!