2

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

n = 10;
H = RandomReal[{0, 1}, {n, n}];;
MatrixForm@H
H1=H*H;
Histogram[H1[[1]]]

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.

NOTE

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

L.K.
  • 683
  • 1
  • 7
  • 17
  • @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. – L.K. Oct 06 '16 at 09:03
  • 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. – dionys Oct 06 '16 at 09:36
  • @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 – L.K. Oct 06 '16 at 09:41

1 Answers1

1
Module[{n = 10, data, h1}, data = RandomReal[{0, 1}, {n, n}];
 Print@data[[1]];
 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]]]

(*{0.336058,0.976536,0.507958,0.436267,0.1567,0.223463,0.760768,0.996779,0.532131,0.217988}*)

(*{0.112935,0.953623,0.258022,0.190329,0.0245549,0.0499356,0.578768,0.993568,0.283163,0.0475189}*)

enter image description here

enter image description here

dionys
  • 4,321
  • 1
  • 19
  • 46
  • 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 ? – L.K. Oct 06 '16 at 10:25
  • Just use Part to select the appropriate row from your H1 (squared matrix data) and plot it with RectangleChart. – dionys Oct 06 '16 at 10:28
  • +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? – L.K. Oct 06 '16 at 10:32
  • 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. – dionys Oct 06 '16 at 10:42
  • I am sorry to disturb you again, but why I am not getting the x coordinates – L.K. Oct 06 '16 at 11:50
  • Even not in your plot the x-cord. are displayed, another the width of the data is increasing not fixed. – L.K. Oct 06 '16 at 11:58
  • 1
    See the edited answer which shows x-axis labels and uniform widths. – dionys Oct 06 '16 at 15:50
  • A last disturbance, my boss said when I change row i.e. data[[1]] to data[[2]] then y-axis also changes means it re scales(where we have 1 it becomes 0.3 etc.). Is there a way to fix the scaling. So scale remain fixed. – L.K. Oct 12 '16 at 13:18