I am trying to restructure a correlation matrix such that the variables with the strongest correlations are grouped together


These are the column headers of the data'

t = {"Age", "Blood Pressure", "Specific Gravity", "Albumin", "Sugar","Red Blood Cells"}

This is the data


These are the ticks for the matrix plot

ticks = {{Table[{i, t[[i]]}, {i, 1, Length@t}], None}, {None, 
Table[{i, Rotate[columnHeaders[[i]], 90 Degree]}, {i, 1, 

and here is the correlation matrix

matplot =MatrixPlot[m, Mesh -> True,ColorFunction -> ColorData[{"TemperatureMap", "Reverse"}],FrameTicks -> ticks]

I am attempting to use MinimumBandwidthOrdering[] to essentially restructure the matrix so that the strongest correlated varibales are grouped together.

{r, c} = MinimumBandwidthOrdering[m]

Plotting the matrices side by side

{MatrixPlot[m], MatrixPlot[m[[r, c]]]}

My problem is this: How can i plot the the matrices such that when the matrix is transformed, the column headers that are associated with that variable are transformed with it.

from the plots, it is just labelled as: 1,2,3,4,5,6 Whereas i was hoping it to be in the format of the plot that was given from the matplot function.

Thank you for reading.

  • $\begingroup$ With t[[c]]...? $\endgroup$ Commented Mar 5, 2020 at 13:46
  • $\begingroup$ If you mean t[[c]] as in the collum headers appended to the collums yes. But with t appended to the columns and rows as shown in the plot from the matplot[] correlation matrix. $\endgroup$
    – isaac5122
    Commented Mar 5, 2020 at 13:50
  • 1
    $\begingroup$ use ticks2 = {{Table[{i, t[[r]][[i]]}, {i, 1, Length@t}], None}, {None, Table[{i, Rotate[t[[c]][[i]], 90 Degree]}, {i, 1, Length@t}]}}; for FrameTicks in MatrixPlot of m[[r,c]]? $\endgroup$
    – kglr
    Commented Mar 5, 2020 at 14:39

1 Answer 1


Define ticks2 using t[[r]] and t[[c]] in place of t in your code for ticks.

ticks2 = {{Table[{i, t[[r]][[i]]}, {i, 1, Length@t}], None}, {None, 
    Table[{i, Rotate[t[[c]][[i]], 90 Degree]}, {i, 1, Length@t}]}};

Row[MapThread[Panel @
   MatrixPlot[#, Mesh -> True, ImageSize -> 1 -> 40, 
     ColorFunction -> ColorData[{"TemperatureMap", "Reverse"}], 
     FrameTicks -> #2, 
     Epilog -> {Text[Style[#3, 16], Scaled@{-.15, 1.15}]}] &, 
  {{m, m[[r, c]]}, {ticks, ticks2}, {"m", "m[[r, c]]"}}], Spacer[20]]

enter image description here


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.