Choosing colors is one of the hardest parts of designing a data visualization! We talk about the three main types of color scales, and related functions that are built into d3.
One of the hardest parts of creating charts is choosing colors. The wrong colors can make a chart unappealing, ineffective, and, worst of all, impossible to read. This section will give you a good framework for choosing colors, important facts about human perception, and simple tips.
When choosing a color scale, you first want to identify its purpose. Going back to what we know about data types, there are three basic use cases:
Representing a category
Representing a continuous metric
Representing a diverging metric
Let's dive deeper into each of these.
1. Representing a category#
The first two data types we talked about (binary and nominal) will be best represented with a categorical color scheme.
Since our metric values don't have a natural order, we don't want to use a color scheme that has a natural order (like white to black).
d3 has built-in color schemes in its d3-scale-chromatic library.
These categorical color schemes have been carefully designed to have enough contrast between colors. Each of these schemes is an array of colors — for example, to use the first scale in this list, we would access each color at its index in
For categorical color schemes, it's helpful for each color to have a different descriptive name. For example, there might be two colors that could be described as "blue", which can be confusing to talk about.
2. Representing a continuous metric#
For metrics that are continuous, we'll want a way to interpolate in between color values. For example, we could represent humidity values with a color scale ranging from white to dark blue.