# When to use it: color maps

I’m researching image processing techniques for my new job.  I’m finding lots of things that I never took the time to understand, even if I had encountered them.  One of them is color maps.  Color maps are ways to convert a set of scalar values into colors.  They can be used to visualize non-visual data, or enhance visual data.

## Non-visual data

Frequently in science and engineering, an experiment or test generates data that is non-visual in nature.  For example, in my last job, a co-worker ran tests comparing the frequency of encountering a bug while varying inputs of a 2 input, 32-bit arithmetic operation.  He had a hypothesis that the frequency of encountering the bug was related to the number of on/true bits in the inputs, so he aggregated the data by the number of on/true bits in each input.  This generated a matrix of values: one axis for the number of true bits in one of the inputs, one axis for number of true bits in the other input, and the value of the matrix at X, Y was the number of times that the bug was encountered when each input had that many on/true bits.

It is immediately apparent that it is difficult to detect trends from a 32×32 matrix (1024 values) of integers.  Rather than look at numbers, it is far easier to detect a trend in this many values by visualizing it.

But how to visualize the data?  The data originated from an arithmetic operation.  There is no concept of color or shape in 32-bit inputs, even when aggregating the data.

One method of visualization is to assign a color for each frequency.  So if the minimum number of times the bug was encountered was 0, and the maximum was 100, we might choose 100 different colors and create a map:

 0 #FFFFFF 1 #EEFFFF 2 #EEEEFF … … 99 #000011 100 #000000

Table 1 – Non-linear color map from white to black

Now we can plot a 32×32 square, where each element in the square contains one of the colors.  This makes trends immediately apparent.  It will be very visually obvious when the square shows a pattern such as a diagonal/horizontal/vertical black line, one or more black regions, and so on.

Therefore, a color map can help us translate non-visual data into a visualization that can help us spot trends or patterns.

## Poorly-visualized data

The idea described above can be easily extended to a dataset that is poorly visualized, for example something with few colors or a small color range.  For example, a color map could be used to colorize old black & white photos (though if I had to guess, it would need to be done on a region-by-region basis).  In this case, each region would be assigned essentially a color, for example blue.  Pixels closer to white would be lighter blues, while pixels closer to black would be mapped to darker blues.  The person doing the colorizing could choose the color for the region, the lightest shade of the color, and the darkest shade of the color.  Then, a color map could be created for all the intensities in the region.  Each intensity would be mapped to a shade between the lightest & darkest shade.  The person performing the colorization could choose to make the colors very saturated by choosing very light & dark shades (ie, a large difference between the light & dark shade) for every region, or could choose to make the colors washed out by choosing light & dark shades that were close to each other.

## The real challenge

Even brief reading on the topic of color maps has taught me that the real challenge in creating or using color maps is creating a good color map for the application that will not introduce artifacts.  These artifacts include disparate values being mapped to similar colors/luminances which can cause patterns to appear that may not actually appear, or may cause other patterns to disappear.  It’s also taught me that the default color maps in applications such as Matlab and Octave may not be a good fit for every application.  I’m not going to cover this topic, but here’s some great resources I found on it: