I have a matrix and I would like to plot the square of its element.

n = 10;
H = RandomReal[{0, 1}, {n, n}];;

Histogram Plot

First row is H1[[1]]

{0.977106, 0.720555, 0.230498, 0.432439, 0.180738, 0.0134593, \
0.592225, 0.0321264, 0.587188, 0.333721}

I got this, I wanted these values to be along $y$ and there position in columns to be along $x$. I saw this How can I transpose $x$ and $y$ axis on a Plot?. But this unnecessary complicated to just switch them and also it is for a function and I have list data. Is there any simple way to do that.


Values along $X $ $->$ Like probabilities and $Y $ $->$ Locations (like 1st site, 2nd site, and so on). Want to see the plot otherway.

  • $\begingroup$ @Kuba I would like to maintain the plot on top of X-axes only values to be switched. Even values also get switched, so no change as such. $\endgroup$
    – L.K.
    Oct 6, 2016 at 9:03
  • $\begingroup$ Your question still isn't clear. It sounds like maybe you just want a default-style histogram of your data sorted the other way? In that case use Transpose. $\endgroup$
    – dionys
    Oct 6, 2016 at 9:36
  • $\begingroup$ @dionys Please tell me if anything is unclear, I will try to clear it(but editing my question). Transpose will not work.I have tried. See the edited, if it works $\endgroup$
    – L.K.
    Oct 6, 2016 at 9:41

1 Answer 1

Module[{n = 10, data, h1}, data = RandomReal[{0, 1}, {n, n}];
 h1 = Part[data^2, 1]; Print@h1;
 Print@Histogram[h1, PlotLabel -> "default histogram view"];
 RectangleChart[Transpose[{Array[1 &, n], h1}], 
  PlotLabel -> "rectangle chart view", 
  ChartLabels -> ToString /@ Range[10]]]



enter image description here

enter image description here

  • $\begingroup$ Good One, this solves my problem. But I would like to do this for each row(one histogram plot for each row, as above), to convey my problem I showed you one row. Do I need to change h1 everytime? Or I can make h1 a matrix and change the row ? $\endgroup$
    – L.K.
    Oct 6, 2016 at 10:25
  • $\begingroup$ Just use Part to select the appropriate row from your H1 (squared matrix data) and plot it with RectangleChart. $\endgroup$
    – dionys
    Oct 6, 2016 at 10:28
  • $\begingroup$ +1 .But for large $n$ isn't it a problem. Writing everytime new row or can we make a loop, which will select the row automatically and histogram plot for that? $\endgroup$
    – L.K.
    Oct 6, 2016 at 10:32
  • $\begingroup$ Read the help docs for Map and Part carefully. You can apply Part and generate the plots for all rows by mapping a function over your squared data. You could do it in a loop as well, but it's messier and less efficient. Also, read the help for Table before you resort to procedural for-loops. $\endgroup$
    – dionys
    Oct 6, 2016 at 10:42
  • 1
    $\begingroup$ See the edited answer which shows x-axis labels and uniform widths. $\endgroup$
    – dionys
    Oct 6, 2016 at 15:50

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.