Using 2 color functions in a mathematica matrix plot

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 4K views
musicgirl
Messages
12
Reaction score
0
I'm trying to use 2 color functions within one matrix plot in mathematica. Is this possible to do?

For example, using a very simple matrix:

test = Partition[Table[i, {i, 1, 9}], 3]

I would like to make the even numbers vary in color increasing from white to red; and the odd numbers vary in color from grey to black.

How could I do this? I know how to get the whole matrix to vary in color, but no more than this.

Thanks
 
Physics news on Phys.org
I think you would have much better luck making a single color function which includes each of the colors for 1...9
 
Code:
test = Partition[Table[i, {i, 1, 9}], 3];
MatrixPlot[test, ColorFunction -> ((If[EvenQ[#1 ], RGBColor[1, 0, 0, #1/Max[test]], RGBColor[0, 0, 0, 1/4 + (3/4) #1/Max[test]]] &)), ColorFunctionScaling -> False]

Something like that?
Basically I suppress the color function scaling so instead of 0..1 they go from min..max. (so in this case, 1..9).

Then I let the color function be a function that is an If EVEN statement.

If the element #1 is even, then use RGBColor[1,0,0,A] (100 = red, A = alpha transparency, let it scale by the normalized value of the index (using #1/max). Similar for grey.

Now if you want to make it "more" normalized, then you need the min and max of test, and do something like :(#1 - Min[test])/Max[test]
So if your matrix was all from 90..100, it wouldn't be black. It would rescale.