6
$\begingroup$

ConfusionMatrix

How do I show the values in each box? I know how to add the labels.

$\endgroup$
9
$\begingroup$
MatrixPlot[{{14789, 482}, {446, 202}},
 Epilog -> {
   Text[14789, {1/2, 3/2}],
   Text[  482, {3/2, 3/2}],
   Text[  446, {1/2, 1/2}],
   Text[  202, {3/2, 1/2}]}]

enter image description here

This generalizes (for quadratic matrices) to

m = {{14789, 482, 100}, {446, 202, 200}, {1, 2, 3}};

t = 
 Transpose@Map[Flatten, {#, Reverse@Transpose@#} &
  [Table[Range[1, 2 # - 1, 2], {#}]] &[Length@m]]/2

enter image description here

MatrixPlot[m, Epilog -> Text @@@ Transpose[{Catenate@m, t}]]

enter image description here

Adding labels

m = {{14789, 482}, {446, 202}};

t = 
 Transpose@Map[Flatten, {#, Reverse@Transpose@#} &
  [Table[Range[1, 2 # - 1, 2], {#}]] &[Length@m]]/2;

p =
  MatrixPlot[m,
   Epilog -> Text @@@ Transpose[{Catenate@m, t}],
   FrameTicks -> {
     {{1, "Left1"}, {2, "Left2"}},
     {{1, "Bottom1"}, {2, "Bottom2"}},
     {{1, "Right1"}, {2, "Right2"}},
     {{1, "Top1"}, {2, "Top2"}}},
   ImageSize -> Medium];

Column[{
  Row[{Rotate["actual class", 90 Degree], Spacer@15, p}],
  Spacer@5, "predicted class"}, Alignment -> Center]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Good start, but what about the frame labels? $\endgroup$ – m_goldberg Jan 10 '16 at 5:36
  • $\begingroup$ "I know how to add the labels" - from the question $\endgroup$ – eldo Jan 10 '16 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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