Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Using 2 color functions in a mathematica matrix plot

  1. Aug 19, 2013 #1
    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.

  2. jcsd
  3. Aug 19, 2013 #2
    I think you would have much better luck making a single color function which includes each of the colors for 1...9
  4. Aug 20, 2013 #3


    User Avatar
    Gold Member

    Code (Text):

    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 wouldnt be black. It would rescale.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook