0
$\begingroup$

I am trying to find a suitable way to visualize the upper half of a matrix. Can anyone be of help ? I have a matrix of the form M[i,j], i=1,....,n and j=i,...,n Using ListPlot3D is not helping as it is also plotting all the M[i,j]=0 for j < i

$\endgroup$
  • 1
    $\begingroup$ You may want MatrixPlot or ArrayPlot. $\endgroup$ – evanb Dec 1 '17 at 10:20
  • $\begingroup$ stackoverflow.com/questions/2318529/…. Something like this will be helpful $\endgroup$ – Sayak Dec 1 '17 at 10:38
  • $\begingroup$ Hi, Array plot will work, but I was looking for something like $\endgroup$ – Sayak Dec 1 '17 at 10:39
3
$\begingroup$
ArrayPlot[
 Table[If[i > j, 0, RandomReal[]], {i, 10}, {j, 10}],
 ColorFunction -> Function[z, If[z == 0, White, Hue[z]]],
 Frame -> False] 

enter image description here

$\endgroup$
1
$\begingroup$

Primarily to illustrate BarLegend. You can omit the text placement. It is just to compare with bar legend.

sa = SparseArray[{i_, j_} /; i < j :> RandomReal[10], {10, 10}];
Legended[MatrixPlot[sa, Mesh -> All, ColorRules -> {0 -> White}, 
  ColorFunction -> (ColorData["Rainbow"][Rescale[#, {0, 10}]] &), 
  ColorFunctionScaling -> False, MeshStyle -> White, Frame -> True, 
  Epilog -> {Text[
       NumberForm[Extract[sa, ##], 
        2], {#[[2]], 10 - #[[1]]} + {-0.5, 0.5}] & /@ 
     sa["NonzeroPositions"]}], BarLegend[{"Rainbow", {0, 10}}]]

enter image description here

Note

  • you can also use UpperTriangularize to make upper triangular matrix
  • you have to deal with different coordinate systems for Plot and MatrixPlot
  • ColorFunctionScaling->False was done to get the BarLegend and MatrixPlot to display same scale
$\endgroup$
  • $\begingroup$ Thank you so much guys. This is very helpful $\endgroup$ – Sayak Dec 4 '17 at 3:57

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.