# Matrix Plot: How to Apply color function to only a region of the plot

I want to make a matrix plot where a color function is applied to a region of a matrix. Those matrix elements which were not in the region specified should not have any color associated with them, and they should display the numerical value of that matrix element.

Specifically, I have a matrix that is 8 columns big. I want to make a matrix plot where the first seven columns to have a color function applied to them. The 8th column should simply be uncolored and should display the numerical value associated with that matrix element.

To contextualize the data, each row of the matrix represents a molecule. Each column represents the charge on a particular carbon atom. The final column is the equilibrium constant for a chemical reaction associated with each atom. The goal is to correlate the charge on each atom with the equilibrium constant.

data = {{0.462, -0.195, -0.364, -0.331, -0.248, -0.183,
0.329, -1}, {0.462, -0.195, -0.364, -0.331, -0.248, -0.183,
0.329, -1}, {0.307, -0.12, -0.332, -0.328, -0.332, -0.12, 0.307,
0.0672}, {0.22, -0.147, -0.328, -0.391, -0.328, -0.147, 0.22,
0.011}, {0.275, -0.108, -0.327, -0.344, -0.327, -0.108, 0.275,
0.0363}, {0.291, -0.164, -0.258, -0.343, -0.268, -0.164,
0.291, -1}, {0.391, -0.2, -0.242, -0.349, -0.242, -0.2,
0.391, -1}, {0.448, -0.237, -0.232, -0.333, -0.232, -0.237, 0.448,
1}, {0.472, -0.208, -0.226, -0.379, -0.226, -0.2, 0.472,
0.261}, {0.347, -0.246, -0.161, -0.359, -0.161, -0.246, 0.347, 20}}


I want the first 7 columns to have the color map. And the 8th column to display the number associated with that matrix element.

• @MarcoB How did you get the image to display correctly? I couldn't figure it out – olliepower Jun 17 '15 at 20:33
• You had the link indented by 4 spaces, so the system interpreted it as code instead. I just had to remove the extra leading spaces. As an aside, are you sure of the values in your last column? Equilibrium constants should be positive numbers, but you have some negative values there. – MarcoB Jun 17 '15 at 20:37

Updated code

Based on some wonderful comments below, here is some simplified, more robust code.

MatrixPlot[
, ColorRules -> {0 -> White}
, Epilog -> MapIndexed[
Text[#1, Flatten[{Last[Dimensions[data]] - 0.5, #2 - 0.5}]] &
, Reverse@data[[All, -1]]
]
]


Original code

Does this do the trick? It's sort of messy, but it does what I think what you want.

MatrixPlot[
Transpose@Insert[
Transpose[data][[;; -2]]
, ConstantArray[0, Length@data]
, -1
]
, ColorRules -> {0 -> White}
, Epilog -> MapIndexed[
Text[#1, Flatten[{Length@Transpose@data - 0.5, #2 - 0.5}]] &
, Reverse@data[[All, -1]]
]
]


Updates: Based on the good comment below, I've added ColorRules -> {0 -> White} in order to assure that the last column will always be white no matter what ColorFunction is used. In addition, I have made this more general by using the Length of Transpose@data in order to place the text, rather than putting in 8 by hand.

The result looks like this:

I've replaced the final column in data with a column of zeros, and I've fed the last column of data to Text's which are placed using Epilog. The use of MapIndexed might be overkill, but it works.

• You could use ColorRules->{0->White} to make sure that any applied ColorFunction makes that last column white. – xsk8rat Jun 17 '15 at 20:54
• Yes, that is a good idea. I will add it to the code. – march Jun 17 '15 at 20:57
• Consider PadRight[Drop[data, None, -1], Dimensions[data]] – J. M. is in limbo Jun 17 '15 at 21:21
• @J. M. Much more beautiful than mine. Thank you. I will update the answer. – march Jun 17 '15 at 21:23
• Also, prolly better to do Last[Dimensions[data]] than Length[Transpose[data]]. – J. M. is in limbo Jun 17 '15 at 21:26

Another way using Graphics primitives Raster and Text:

dimdata = Reverse@Dimensions@data;

map = data[[All, ;; -2]] // Raster[#, {{0, 0}, Reverse@Dimensions@#}, MinMax@#,
ColorFunction -> "Rainbow"] &;

text = Text @@@ Thread[{data[[All, -1]],

Graphics[{map, text}, AspectRatio -> Automatic, Frame -> True,