# Custom color function in ArrayPlot

I wish print the ArrayPlot of a the two-dimensional array A, whose entries are distributed as in:

Histogram[Flatten[A], PlotRange -> All, "PDF"] A standard Arrayplot[A, ColorFunction -> ColorData["SunsetColors"]] doesn't give good results: it is mostly of the same color.

I guess this is due to the fact that the colormap is uniformly spaced, while I need to resolve for the many values in the interval $[0,2]$.

How can I print a significant 'ArrayPlot' from such data?

• you can use ColorFunction -> (Blend["SunsetColors", whateverscaling function]&). Don't forget ColorFunctionScaling -> False – Kuba Mar 23 '17 at 19:23
• @Kuba Sounds good, unfortunately I didn't manage to work it out... What function do you suggest to use? – altroware Mar 24 '17 at 16:59

## 2 Answers

One trick to deal with such arrays is to use HistogramTransform on them. This applies a transformation to each number so that their histogram on the interval $[0,1]$ will be flat.

This transformation is in fact the CDF of their distribution. If you were to compute this manually, you could find it by plotting the values against their index after being sorted, then rescaling the indices to $[0,1]$.

Of course, this sort of plotting is not always appropriate because it uses a rather arbitrary transformation on the values. It is up to you to decide if it is acceptable to use this method for your application.

• Thanks! In fact, such a trasformation seems to arbitary for my case, although HistogramTransform seems a rather useful function! – altroware Mar 24 '17 at 17:11

Here is a temporary solution I found myself, that consists of defining a customised ColorFunction, tabulated with respect to a "Log-spaced" interval:

nbins = 20;
minValue = 0.01; (*this ideally is Min[A]*)
maxValue = 25; (*this is Max[A]*)
F = ColorData["SunsetColors"];
colors = Table[F[i/nbins], {i, 2, nbins + 1}]
fSpace[min_, max_, steps_, f_: Log] := InverseFunction[f] /@ Range[f@min, f@max, (f@max - f@min)/(steps - 1)]
bins = fSpace[minValue, maxValue, nbins + 1]
intervals = Table[Interval[{bins[[i]], bins[[i + 1]] }], {i, 1, nbins}];
cFunction = First@Pick[colors, IntervalMemberQ[intervals, #]] &;

ArrayPlot[
A,
ColorFunctionScaling -> False,
ColorFunction -> (cFunction[#] &),
PlotLegends -> Automatic
]


I'm still open to alterative solutions, such as anything that uses a continous function, instead of a tabulated function, but at the moment I chose this as correct answer.