LoginSignup
4
5

More than 5 years have passed since last update.

ipytracerでヒストグラム作成をアニメーション表示

Last updated at Posted at 2017-04-26

pythonのアルゴリズム可視化ツールipytracerで,ヒストグラム作成をアニメーション化してみた.

インストール

$ pip install ipytracer
$ jupyter nbextension enable --py --sys-prefix ipytracer

使い方

jupyter notebookでやりましょう


import ipytracer
from IPython.display import display
import numpy as np
from time import sleep


def hist(img):
    h, w = img.shape

    im = ipytracer.List2DTracer(img.tolist())
    display(im)

    hist = ipytracer.ChartTracer(list(np.zeros(img.max()+1)))
    display(hist)
    for i in range(10):
        hist[i] = 0 # 一回目のアクセスがなぜか表示されないので,とりあえずここでアクセスしておく


    for y in range(h):
        for x in range(w):

            value = im[y][x]
            hist[value] += 1
#             sleep(1) # 表示が速すぎるときにはスリープ


im = np.random.randint(1, 30, size=(5, 10)) # 5x10ランダム画像生成
hist(im) # 画像のヒストグラム作成

結果

histogram.gif

4
5
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
4
5