Using 2 color functions in a mathematica matrix plot

Click For Summary
SUMMARY

This discussion focuses on implementing two distinct color functions within a single matrix plot in Mathematica. The user seeks to color even numbers from white to red and odd numbers from grey to black using the MatrixPlot function. A solution is provided that utilizes the ColorFunction option with an If statement to differentiate between even and odd values, while suppressing color function scaling to maintain the original data range. The proposed method effectively normalizes the colors based on the maximum value of the matrix.

PREREQUISITES
  • Familiarity with Mathematica syntax and functions
  • Understanding of matrix manipulation using Partition and Table functions
  • Knowledge of color functions in Mathematica, specifically RGBColor
  • Basic concepts of conditional statements in programming (If statements)
NEXT STEPS
  • Explore advanced color functions in Mathematica for data visualization
  • Learn about the use of ColorFunctionScaling in Mathematica
  • Investigate the Partition function for creating complex matrices
  • Study normalization techniques for data visualization in Mathematica
USEFUL FOR

Mathematica users, data scientists, and educators looking to enhance their matrix visualizations with custom color schemes.

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
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K