ネットワークルーティングシミュレーションの視覚化を作成しています。ここでは、ネットワークはmatplotlibの2Dサークルパッチで表されます。
Matplotlibのアニメーションを使用して、シミュレートされたルーティングを示しています。
Matplotlib.collectionsを調べると、円オブジェクトの色をすばやく変更してコレクションを再描画するために、円オブジェクトにランダムアクセスする良い方法がないようです。
続行する方法についての提案は大歓迎です!
現在、私のアニメーションは次のとおりです。
def init():
pass
def animate(i):
global network_nodes, active_stack, nums
import matplotlib.artist as mplart
#hard coded routes
n = routes(i)
network_nodes = {}
# draw colorless network
network_gen(levels,0.0,radius,0.0,0.0)
# simplified alterations
network_nodes[n].set_facecolor('blue')
# add the patch
fig.gca().add_patch(network_nodes[c][0])
パッチコレクション内のオブジェクトの色を変更するには、コレクションのカラーマップを設定してから、アニメーションの各ステップでset_arrayを使用して画像配列を変更します。以下の例では、この例に触発されたように、画像配列はランダムです。
import numpy as np
from matplotlib.patches import Circle
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib import animation
fig, ax = plt.subplots()
patches = []
# create circles with random sizes and locations
N = 10 # number of circles
x = np.random.rand(N)
y = np.random.rand(N)
radii = 0.1*np.random.rand(N)
for x1,y1,r in zip(x, y, radii):
circle = Circle((x1,y1), r)
patches.append(circle)
# add these circles to a collection
p = PatchCollection(patches, cmap=cm.prism, alpha=0.4)
ax.add_collection(p)
def animate(i):
colors = 100*np.random.rand(len(patches)) # random index to color map
p.set_array(np.array(colors)) # set new color colors
return p,
ani = animation.FuncAnimation(fig, animate, frames=50, interval=50)
plt.show()
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加