Mathematica Using 2 color functions in a mathematica matrix plot

Click For Summary
Using two color functions within a single matrix plot in Mathematica is achievable by creating a custom color function that differentiates between even and odd numbers. The example provided demonstrates how to construct a matrix from a simple table of numbers and apply distinct color gradients based on parity. Even numbers can be colored with a gradient from white to red, while odd numbers can transition from grey to black. The approach involves suppressing color function scaling to allow the color mapping to reflect the actual values in the matrix instead of a normalized range. The color function utilizes an If statement to determine whether a number is even or odd, applying the appropriate RGBColor settings based on the number's value relative to the maximum in the matrix. For enhanced normalization, adjustments can be made to the color function to account for the minimum and maximum values in the matrix, ensuring that the colors reflect the intended range accurately.
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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K