
我试图逐步执行一系列字母(节点),然后绘制从原点“O”到当前步骤的路径.看下面的图表会更有意义.我不想让它们在Python 3.6之外创建.mp4.我认为这将是人们理解这些更新功能如何运作以及如何将其应用于可视化网络的良好资源.
如何使用更新功能为下面的networkx图表制作动画?
显然,动画不会出现在具有不同ax对象的matplotlib图形上,但这只是为了说明框架的布局方式.
import networkx as nximport numpy as npimport matplotlib.pyplot as pltimport seaborn as snsimport matplotlib.animation as animationdef update_func(num,data,line): # https://matplotlib.org/2.0.0/examples/animation/basic_example.HTML line.set_data(data[...,:num]) return line# Create Graphnp.random.seed(2)G = nx.cubical_graph()G = nx.relabel_nodes(G,{0:"O",1:"X",2:"XZ",3:"Z",4:"Y",5:"YZ",6: "XYZ",7:"XY"})pos = nx.spring_layout(G)# Sequence of letterssequence_of_letters = "".join(['X','Y','Z','Z']) #np.random.RandomState(0).choice(List("XYZ"),size=6,replace=True)IDx_colors = sns.cubehelix_palette(5,start=.5,rot=-.75)[::-1]IDx_weights = [3,2,1]# General graph structurewith plt.style.context("seaborn-white"): fig,ax = plt.subplots() nx.draw(G,pos=pos,with_labels=True,ax=ax) print(ax.get_xlim(),ax.get_ylim())# (-0.10500000000000001,1.105) (-0.088398066788676247,0.93028441715702148)# Build plotwith plt.style.context("seaborn-white"): fig,axes = plt.subplots(ncols=3,nrows=2,figsize=(10,5)) for i in range(0,len(sequence_of_letters),3): triad = sequence_of_letters[i:i+3] for j in range(1,4): # Axes index for rows and cols IDx = i + j - 1 row_IDx,col_IDx = {True: (0,IDx),False: (1,IDx - 3)}[IDx < 3] ax = axes[row_IDx][col_IDx] # Path in Graph path = ["O"] + ["".join(sorted(set(triad[:k + 1]))) for k in range(j)] # Background nodes nx.draw_networkx_edges(G,ax=ax,edge_color="gray") null_nodes = nx.draw_networkx_nodes(G,nodeList=set(G.nodes()) - set(path),node_color="white",ax=ax) null_nodes.set_edgecolor("black") # query nodes query_nodes = nx.draw_networkx_nodes(G,nodeList=path,node_color=IDx_colors[:len(path)],ax=ax) query_nodes.set_edgecolor("white") nx.draw_networkx_labels(G,labels=dict(zip(path,path)),Font_color="white",ax=ax) edgeList = [path[k:k+2] for k in range(len(path) - 1)] nx.draw_networkx_edges(G,edgeList=edgeList,wIDth=IDx_weights[:len(path)],ax=ax) # Scale plot ax ax.set_Title("Frame %d: "%(IDx+1) + " - ".join(path),Fontweight="bold") ax.set_xlim((-0.10500000000000001,1.105)) ax.set_ylim((-0.088398066788676247,0.93028441715702148)) ax.set_xticks([]) ax.set_yticks([]) 解决方法 这两个链接问题的答案提供了关于如何为networkx图表设置动画的非常好的示例.它们比这个问题中的示例代码允许的任何答案都更加规范. 因此,我将重点讨论如何使用更新函数从问题中为networkx图表制作动画.
解决方案是将两个for循环中的所有内容放入一个函数中,该函数至少需要一个索引作为参数.然后可以使用该索引生成图像.
import networkx as nximport numpy as npimport matplotlib.pyplot as pltimport seaborn.APIonly as snsimport matplotlib.animation# Create Graphnp.random.seed(2)G = nx.cubical_graph()G = nx.relabel_nodes(G,'Z'])IDx_colors = sns.cubehelix_palette(5,1]# Build plotfig,ax = plt.subplots(figsize=(6,4))def update(num): ax.clear() i = num // 3 j = num % 3 + 1 triad = sequence_of_letters[i:i+3] path = ["O"] + ["".join(sorted(set(triad[:k + 1]))) for k in range(j)] # Background nodes nx.draw_networkx_edges(G,edge_color="gray") null_nodes = nx.draw_networkx_nodes(G,ax=ax) null_nodes.set_edgecolor("black") # query nodes query_nodes = nx.draw_networkx_nodes(G,ax=ax) query_nodes.set_edgecolor("white") nx.draw_networkx_labels(G,ax=ax) edgeList = [path[k:k+2] for k in range(len(path) - 1)] nx.draw_networkx_edges(G,ax=ax) # Scale plot ax ax.set_Title("Frame %d: "%(num+1) + " - ".join(path),Fontweight="bold") ax.set_xticks([]) ax.set_yticks([])ani = matplotlib.animation.FuncAnimation(fig,update,frames=6,interval=1000,repeat=True)plt.show() 总结 以上是内存溢出为你收集整理的如何使用更新函数在Matplotlib 2.0.0中为NetworkX图形设置动画?全部内容,希望文章能够帮你解决如何使用更新函数在Matplotlib 2.0.0中为NetworkX图形设置动画?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)