0
0

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 3 years have passed since last update.

Matlabのhistcounts/histcounts2をPythonで

Last updated at Posted at 2021-01-28

Matlabで個人的によく使うhistcoutsとその2変数版のhistcounts2のやりかたメモ。

#ヒストグラムをプロットするやり方
”python ヒストグラム” とかでググると
matplotlibのhistやhist2dの情報が見つかって、その返り値からcountをとってくる方法が色々と出てくる。

たとえばこんな感じ。

import numpy as np
import matplotlib.pyplot as plt

x = np.random.normal(loc=0, scale=1, size=1000)
y = np.random.normal(loc=0, scale=2, size=1000)
 
xEdge = np.arange(-3, 3, 0.1)
yEdge = np.arange(-6, 6, 0.2)

fig = plt.figure()
ax = fig.add_subplot(111)
(cnt, xBin, yBin, img) = ax.hist2d(x, y, bins=[xEdge, yEdge])

これはこれで良いのだけれど(ちなみにax.hist2dの4つめの返り値が何なのかは知らない)
いちいちプロットするのがまどろっこしい…と思っていたのだが、普通にnumpyにあった。

#1変数の場合

import numpy as np

x=np.random.normal(loc=0, scale=1, size=1000)
xEdge=np.arange(-3, 3, 0.1)
(cnt, xBin) = np.histogram(x, bins=xEdge)

#2変数の場合

import numpy as np

x = np.random.normal(loc=0, scale=1, size=1000)
y = np.random.normal(loc=0, scale=2, size=1000)
xEdge = np.arange(-3,3,0.1)
yEdge = np.arange(-6,6,0.2)
(cnt, xBin, yBin) = np.histogram2d(x, y, bins=[xEdge,yEdge])

これで簡単にcntをz-scoreにしてプロットとかできて捗る。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?