nano_exit

基礎的なことこそ、簡単な例が必要だと思うのです。

二次元ヒストグラムの計算およびプロット(python)

いつも忘れるので個人用のメモ

参考ページ
matplotlib - hist2d で2次元ヒストグラムを作成する方法 - pystyle
[Python]Matplotlibで2次元ヒストグラムを作成する方法 - Qiita
NumPyでヒストグラムを作るnp.histogram関数の使い方 - DeepAge

ヒストグラムの棒の数がbinsで、bin"s"なのはbinの複数系なのではなく、binsでビンと読むらしい。
ヒストグラム - Wikipedia
コードを書くときにいつもsを付け忘れてエラーになり、その度に引数をググっていた。

適当に二次元配列の乱数から二次元ヒストグラムを作る。

import numpy as np
from matplotlib import pyplot as plt

# setting for distribution
n = 100
r = np.random.rand(n,2).T

# setting for histogram
bins_ = 5  # if you want to specify different bins between x and y, you can use tuple or list of (bins_x, bins_y).  
range_ = ([0,1],[0,1])
d_TF = True  # True: histogram will be normalized

# more fine tuning for bins
# min_ = 0
# max_ = 1
# interval_ = 0.2
# bins_ = int((max_-min_)/interval_)

# plot histogram
plt.hist2d(r[0],r[1],bins=bins_,range=range_,density=d_TF)
plt.show()

# get histogram data
frequency, x_axis, y_axis = np.histogram2d(r[0],r[1],bins=bins_,range=range_,density=d_TF)

numpyとmatplotlib.pyplotで呼び出す関数名が異なることに注意。