# How to force MatrixPlot to differentiate distinct expr?

I thought MatrixPlot can differentiate different expressions in color until I found this:

SparseArray[Band[{1, 1}] -> {E^-I, E^I, E^I, E^-I, E^-I, E^I}] // MatrixPlot MatrixPlot can't differentiate E^-I from E^I!!!!

But I use MatrixPlot to get a first impression of how many different expressions are there and how they are located in a newly generated matrix?

So here is the question:

How to force MatrixPlot to differentiate every distinct expression?

• SparseArray[Band[{1, 1}] -> {E^-I, E^I, E^I, E^-I, E^-I, E^I}] // Im // MatrixPlot – eldo Nov 7 '15 at 11:06
• "MatrixPlot can't differentiate E^-I from E^I!!!!". It can, but only if it gets input it can work with, i.e. real numbers. – Graumagier Nov 7 '15 at 11:06
• @Graumagier well, so what I am asking is to find a way to let MatrixPlot differentiate every distinct expr without supplying it with numbers – matheorem Nov 7 '15 at 11:09
• @matheorem – eldo's comment should help you with that. – Graumagier Nov 7 '15 at 11:10
• @eldo This is to specific, what if there are other kind of expr that MatrixPlot can't differentiate, we don't know – matheorem Nov 7 '15 at 11:10

got an idea, use Hash function. Hash function will generate very different hash code for different expr

mat=SparseArray[Band[{1, 1}] -> {E^-I, E^I, E^I, E^-I, E^-I, E^I}]

MatrixPlot[Map[Hash,Normal@mat,{2}],ColorFunction->Hue] I wrote a function colormat to color matrix with any expressions and to make it as colorful as possible

    Clear[colormat];
colormat[mat_] := Module[{},
tmpmat = Map[Hash[#] &, Normal@mat, {2}] /. Hash -> 0;
tallyelement = Tally@Flatten@tmpmat;
fcol = Hue;(*ColorData["CMYKColors"]*)
customhuelist[n_] :=
fcol[#[], #[], 1] & /@
Transpose@{Subdivide[0, 0.9, n - 1],
Take[Flatten@Table[{0.35, 0.55, 1}, 30], n]};
If[MemberQ[tallyelement, 0, -1],
customcolorrule =
Prepend[customhuelist[Length[tallyelement] - 1], White]],
customcolorrule =
customhuelist[Length[tallyelement]]]];
MatrixPlot[#[], ColorRules -> customcolorrule,
PlotLabel -> #[]] & /@ {{tmpmat, "mat"}, {Re@mat,
"Re@mat"}, {Im@mat, "Im@mat"}}]


Now for matrix like following

colormat[{{a, b, d}, {e, f, g}, {h, i, j}}]


will give I keep the last two plot as default MatrixPlot of Re and Im for possible reference.

If we directly use Hash or bill s ToCharacterCode will give So you can see how colorful colormat can give :)

Another way to approach this is to turn each item in the list into a string, then transform the string to charactercodes, which are numbers that can be plotted by MatrixPlot.

mat = SparseArray[Band[{1, 1}] -> {E^-I, E^I, E^I, E^-I, E^-I, E^I}];
MatrixPlot[Map[Total, ToCharacterCode /@ Map[ToString, Normal[mat], {2}], {2}],
ColorFunction -> Hue] • The interesting thing is why we got the same color? – matheorem Nov 8 '15 at 1:46
• I think it's coincidence because the numbers in the matrices are very different. – bill s Nov 8 '15 at 4:00
• Modified to match purpose of question...Im seems the most natural choice – ubpdqn Nov 8 '15 at 8:31

Example:

mat = SparseArray[Band[{1, 1}] -> {E^-I, E^I, E^I, E^-I, E^-I, E^I}];


Auxiliary. Nonzero values:

nzv[mat_] := Sort@DeleteDuplicates@SparseArray[mat]["NonzeroValues"];


Index function. Assign a positive integer index to each nonzero expression in the matrix mat.

Clear[idxFN];
idxFN[mat_] := With[{a = AssociationThread[# -> Range@Length@# &@nzv[mat]]},
Lookup[a, #, 0] &];

MatrixPlot[Map[idxFN[mat], mat, {2}]] A ColorFunction that assigns a different color to each expression in mat. Adding the default color function made it look at little complicated, but it's basically the same idea as the indexing function.

Clear[colorFN];
colorFN[mat_,
cf_:  (* default color function with default rescaling *)
Composition[
"DefaultColorFunction" /.
(Method /. ChartingResolvePlotTheme["Default", MatrixPlot]),
0.5 + 0.5 # &],
default_: White] :=
With[{a = AssociationThread[# -> cf /@ (N@Range@Length@#/Length[#]) &@ nzv[mat]],
d = default /. Automatic -> cf}},
Lookup[a, #, d] &];


The usual use should use ColorFunctionScaling -> False:

MatrixPlot[mat, ColorFunction -> colorFN[mat],
ColorFunctionScaling -> False] Other styling options:

MatrixPlot[mat,
ColorFunction -> colorFN[mat, ColorData@"Rainbow", Blue],
ColorFunctionScaling -> False] MatrixPlot[mat,
ColorFunction -> colorFN[mat, ColorData@"LakeColors", Automatic],
ColorFunctionScaling -> False] • Upvoted, what I want to do in colormat has the same effect as indexing different expr. But I done it stupidly. Very clever method! – matheorem Nov 8 '15 at 15:20
• BTW, what does Composition[ "DefaultColorFunction" /. (Method /. ChartingResolvePlotTheme["Default", MatrixPlot]), 0.5 + 0.5 # &] mean? ChartingResolvePlotTheme ? – matheorem Nov 8 '15 at 15:24
• @matheorem Thanks. ResolvePlotTheme is a way to look up some default settings for various plotting functions. Search for it on this site, and you'll find some other uses. The default behavior of MatrixPlot seems to scale the input to the default color function so that the matrix value 0 corresponds to the color function value 0.5. Therefore I composed with 0.5 + 0.5 # &. – Michael E2 Nov 8 '15 at 15:34

You can simply colorize by the complex phase

mat = SparseArray[Band[{1, 1}] -> {E^-I, E^I, E^I, E^-I, E^-I, E^I}];
MatrixPlot[mat, ColorFunction -> (Hue[Arg@#/2/π, Abs@#] &),
ColorFunctionScaling -> False]
` 