Skip to content

Instantly share code, notes, and snippets.

@pistatium
Created December 10, 2019 09:40
Show Gist options
  • Save pistatium/4b997ac42bb9faa4c22366d1c5d370f1 to your computer and use it in GitHub Desktop.
Save pistatium/4b997ac42bb9faa4c22366d1c5d370f1 to your computer and use it in GitHub Desktop.
投票の様子をグラフ化するスクリプト
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
flames = []
# 投票ラベル ('A': 1, 'B': 2, 'C': 3) に対応
labels = ['A', 'B', 'C']
# ラベルの種類数
label_kinds = len(labels)
# 投票データ (1スタート)
votes = [1, 2, 3, 3, 3, 1, 3, 1, 3, 3, 1, 3, 1, 2, 3, 3, 2, 1, 3, 2, 2, 1, 2, 2, 3, 1, 2, 1, 2, 2, 2, 1, 3, 3, 2, 2, 2, 2, 1, 2, 2, 1, 2, 3, 2, 2, 1, 3, 2, 2, 2, 1, 2, 3, 2, 2, 2, 1, 2, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3]
index = np.arange(label_kinds)
for i in range(len(votes)):
# votes の先頭からi番目までを取り出す
vs = votes[:i]
# 集計
counts = []
for k in range(label_kinds):
# 1 スタートなのでここで調整
vote_index = k + 1
counts.append(sum(1 for v in vs if v == vote_index))
plt.xticks(index, labels, fontsize=15, rotation=30)
# 一番高い数なら色を変える
color = ['orange' if c >= max(counts) else 'darkcyan' for c in counts]
im = plt.bar(index, counts, color=color, width=0.5)
flames.append(im)
# アニメーションを開始
ani = animation.ArtistAnimation(fig, flames, interval=100)
plt.show()
@pistatium
Copy link
Author

animation_graph

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment