2017-11-17 1 views
1

私は、Pythonで円グラフアニメーションを作成したいところです(データはループによって連続的に変更されています)。問題は、すべての円グラフを1つずつ印刷していて、多くの円グラフを持つことになります。 1つの円グラフを変更してアニメーションのように見せたい。どのようにこれを行うにはどのようなアイデア?Pythonの円グラフアニメーション

私はあなたがFuncAnimationを使用したいと思う次のコード

colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue', 'black', 'red', 'navy', 'blue', 'magenta', 'crimson'] 
explode = (0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, .01) 
labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] 
nums = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 

for num in range(1000): 
    str_num = str(num) 
    for x in range(10): 
     nums[x] += str_num.count(str(x)) 
    plt.pie(nums, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=140) 
    plt.axis('equal') 
    plt.show() 
+0

Guysがダウンし、あなたがより精緻化が必要な場合は、その後のコメントでお願いし、質問を投票しません。 – GadaaDhaariGeek

+0

Matplotlibの[アニメーションモジュール](https://matplotlib.org/api/animation_api.html)を使ってみましたか?アニメーション関数の各ループの新しいデータで軸プロットを更新する[この例](https://matplotlib.org/gallery/animation/double_pendulum_animated_sgskip.html)は、探しているものに近いかもしれません。 –

+0

ありがとう@StevenWalton。これは私が探していたものです。 – GadaaDhaariGeek

答えて

1

を使用しています。残念ながら、円グラフには更新機能はありません。ウェッジを新しいデータで更新することは可能ですが、これはむしろ面倒です。したがって、各ステップで軸をクリアして新しい円グラフを描く方が簡単かもしれません。

import matplotlib.pyplot as plt 
from matplotlib.animation import FuncAnimation 

colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue', 'limegreen', 
      'red', 'navy', 'blue', 'magenta', 'crimson'] 
explode = (0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, .01) 
labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] 
nums = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 

fig, ax = plt.subplots() 

def update(num): 
    ax.clear() 
    ax.axis('equal') 
    str_num = str(num) 
    for x in range(10): 
     nums[x] += str_num.count(str(x)) 
    ax.pie(nums, explode=explode, labels=labels, colors=colors, 
      autopct='%1.1f%%', shadow=True, startangle=140) 
    ax.set_title(str_num) 

ani = FuncAnimation(fig, update, frames=range(100), repeat=False) 
plt.show() 

enter image description here

関連する問題