5
$\begingroup$

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}}

enter image description here

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

$\endgroup$
2
  • $\begingroup$ @MarcoB How did you get the image to display correctly? I couldn't figure it out $\endgroup$
    – olliepower
    Jun 17, 2015 at 20:33
  • 1
    $\begingroup$ 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. $\endgroup$
    – MarcoB
    Jun 17, 2015 at 20:37

2 Answers 2

8
$\begingroup$

Updated code

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

MatrixPlot[
 PadRight[Drop[data, None, -1], Dimensions[data]]
 , 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:

enter image description here

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.

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

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]], 
Thread@{First@dimdata, Range[Last@dimdata]} - 0.5}];

then

Graphics[{map, text}, AspectRatio -> Automatic, Frame -> True, 
 PlotRange -> Thread[{0, dimdata}], PlotRangePadding -> 0.3, 
 FrameTicks -> ((Range[#] // Thread@{# - 0.5, #} &) & /@ dimdata)]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.