Last active
March 23, 2021 16:43
-
-
Save mu-777/abae4330872a9325b19ebb19fd980d07 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
import matplotlib.ticker as ticker | |
import colorsys | |
import matplotlib.animation as animation | |
from matplotlib.offsetbox import OffsetImage, AnnotationBbox | |
def binlen(i): | |
return len(bin(i)) - 2 | |
def kamogawa_seq(idx): | |
if(binlen(idx)==1): | |
return idx | |
divnum = pow(2, binlen(idx-1)) | |
startnum = pow(2, binlen(idx-1)-1) | |
return ((idx - startnum)*2-1)*(1.0/divnum) | |
# https://matplotlib.org/2.0.2/examples/ticks_and_spines/tick-locators.html | |
fig = plt.figure(figsize=(10, 3)) | |
ax = plt.subplot() | |
ax.set_xlim(-0.02, 1.02) | |
ax.set_ylim(-0.1, 0.1) | |
# グラフの左/右/上の線を消す | |
ax.spines['right'].set_color('none') | |
ax.spines['left'].set_color('none') | |
ax.spines['top'].set_color('none') | |
# X軸をY=0の位置に移動 | |
ax.spines['bottom'].set_position(('data', 0)) | |
# Y軸のメモリをなくし,X軸のメモリを入れる | |
ax.yaxis.set_major_locator(ticker.NullLocator()) | |
ax.xaxis.set_major_locator(ticker.MultipleLocator(0.5)) | |
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.1)) | |
# 主メモリ・サブメモリのサイズを設定する | |
ax.tick_params(which='major', width=1.00) | |
ax.tick_params(which='major', length=5) | |
ax.tick_params(which='minor', width=0.75) | |
ax.tick_params(which='minor', length=2.5) | |
# https://qiita.com/sabopy/items/b4ed95f713e6d98ab52c | |
img_path = 'bird_magamo.png' | |
img = plt.imread(img_path) | |
imgbox = OffsetImage(img, zoom=0.06) | |
# https://qiita.com/SwitchBlade/items/47ffac180919969ab32e | |
frames = [[]] | |
points = [] | |
for i in range(17): | |
title = ax.text(0.0, 0.9, "Index={0}".format(i), | |
fontsize=12, transform=ax.transAxes) | |
x = kamogawa_seq(i) | |
# points.append(ax.scatter([x], [0], c=[colorsys.hsv_to_rgb(x, 0.5, 0.8)], | |
# s=80,marker='o')) | |
artist = AnnotationBbox(imgbox, (x, 0), xycoords='data', | |
boxcoords=("axes fraction", "data"), | |
box_alignment=(0.5, 0.0), | |
frameon=False) | |
points.append(ax.add_artist(artist)) | |
frames.append([title]+points) | |
ani = animation.ArtistAnimation(fig, frames, interval=500) | |
ani.save('kamogawa_algo.gif') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment