3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ヒストグラムの返り値について。

Posted at

こんにちは。
今回はmatplotlibモジュールの中にあるhistメソッドを見ていきたい。
僕にとって意外だったののはこの関数は3つの返り値を返す事。
それではまずサンプルコードから見ていきましょう!

サンプルコード

hist_sample.py
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(123)
mu = 10  #  平均値
sigma = 15 # 標準偏差
# 平均値10、標準偏差15の正規分布に従う乱数を1000個作成。
x = np.random.normal(mu, sigma, 1000) 

# 描画オブジェクト(fig)とサブプロット(ax)を作成。
fig, ax = plt.subplots()

# ここで、ヒストグラムを描写!そして3つの返り値が存在。
n, bins, patches = ax.hist(x)

plt.show()

結果:
ダウンロード.png

なんと美しい。

返り値の説明

返り値は : n, bins, patchesの三つ。
一つずつ見ていこう。

  • n (配列)

    • 縦軸の個数を表す。
    • 全ての要素を足すと、上記のコードでは1000になる。
  • bins (配列)

    • 横軸の句切となる座標
    • デフォルトの大きさは10。
    • ex: [51.3, 61.3, 71.3, ・・・]
  • patches

    • 描画したヒストグラムのビン毎のobjectの集合
    • つまり、それぞれのグラフの情報が入っている(色など)

まとめ

ヒストグラムは小学校?で習うし、グラフとしても有能だ。
マスターして、素早く可視化できるようになっておいた方がいいと思う。
また、ヒストグラムをアレンジしたければ、
Qiita: Matplotlibでヒストグラムを描画する方法がとても参考になりました。

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?