Octave でヒストグラム

# Octave でヒストグラム

ヒストグラム（場合により棒グラフ）を描く。

    usage:
    hist (y)
    hist (y, centers)
    hist (y, nbins)
    hist (y, centers, norm)
    hist (…, prop, val, …)
    hist (hax, …)
    [nn, xx] = hist (…)

度数のカウントのみの関数（**`hist()` の結果とは異なる**）

    n = histc (x, edges)
    n = histc (x, edges, dim)
    [n, idx] = histc (…)

## 使用例


```matlab
pkg load statistics % statistics が必要
```


```matlab
format long % 表示精度を高くする（任意）
x = csvread("iris.csv")(2:151, 2:5);
```

### `hist (y)`

`y` がベクトルの場合は普通のヒストグラム


```matlab
hist(x(:, 1))
```


![output_5_0.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2611739/3bb31d32-e732-b382-99ec-fa99bbfb8760.png)

    


`y` が行列の場合はグループ別棒グラフ


```matlab
hist(x)
```


    
![output_7_0.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2611739/0d7bc422-fd2e-7a40-22f2-5600955d0ad8.png)

    


### `hist (y, centers)`

`centers` は階級の中心（ブレークポイントではないことに注意）


```matlab
hist(x(:, 1), [4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0])
```


    
![output_9_0.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2611739/a736ac17-7fa3-33fd-2e07-0be64751e17d.png)

    


### `hist (y, nbins)`

`nbins` はビンの数


```matlab
hist(x(:, 1), 30)
```


    
![output_11_0.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2611739/74e80fcd-2251-c43a-cd53-04765d31341f.png)

    


### `hist (y, x, norm)`

棒の和が `norm` になる


```matlab
hist(x(:, 1), [4,5,6,7,8], 150)
```


    
![output_13_0.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2611739/67665a9a-16fb-945f-fab2-70dbfe8b0fe9.png)

    


### `hist (…, prop, val, …)`

プロパティーと値の組の指定


```matlab
hist(x(:, 1), "facecolor", "blue", "edgecolor", "green")
```


    
![output_15_0.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2611739/2578c300-7498-7335-f372-085a5c071aa1.png)

    


カラーマップでも変わる

`autumn(), bone(), colorcube(), cool(), copper(), cubehelix(), flag(), gray(), hot(), hsv(), jet(), lines(), ocean(), pink(), prism(), rainbow(), spring(), summer(), viridis(), white(), winter()`


```matlab
hist(x(:, 1))
colormap(summer())
```


    
![output_17_0.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2611739/eb9f2b0a-61e3-b589-ffb9-f7ccc6e494b8.png)

    



```matlab
hist(x(:, 1))
colormap(rainbow(64))
```


    
![output_18_0.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2611739/f3b5a233-212c-1585-4cf5-28362bd132a9.png)

    



```matlab
## Create an image for displaying a colormap
 image (1:64, linspace (0, 1, 64), repmat ((1:64)', 1, 64));
 axis ([1, 64, 0, 1], "ticy", "xy");
 ## Show 'viridis' colormap
 colormap (viridis (64));
 title "colormap (viridis (64))"
 ## Show 'colorcube' colormap
 colormap (colorcube (64));
 title "colormap (colorcube (64))"
```


    
![output_19_0.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2611739/968e4ce0-2682-accc-82c9-fd13ab55b642.png)

    


### [nn, xx] = hist (…)

`nn` は 度数，`xx` は階級



```matlab
[nn, xx] = hist(x(:, 1), [4,5,6,7,8])
```

    nn =
    
        5   54   61   24    6
    
    xx =
    
       4   5   6   7   8
    



```matlab
sum(nn)
```

    ans = 150


### `histc()`

**`hist()` の結果とは異なる**

    n = histc (x, edges)
    n = histc (x, edges, dim)
    [n, idx] = histc (…)


```matlab
n = histc(x(:, 1), [4,5,6,7,8])
```

    n =
    
       22
       61
       54
       13
        0
    


`n = 22` は，`4.0 ≦ x(:, 1) < 5.0` の度数である。


```matlab
sum(x(:, 1) <= 4.0)
sum(x(:, 1) < 5.0)
sum(x(:, 1) <= 5.0)
sum(x(:, 1) <= 4.5)
```

    ans = 0
    ans = 22
    ans = 32
    ans = 5