在matplotlib散点图中动画化标记大小
我正在制作一个网络图动画,想通过节点的大小来展示与节点相关的数据。类似的动画可以通过节点的颜色来实现,代码如下:
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
G = nx.ladder_graph(4)
fig = plt.figure(figsize=(8,8))
pos = nx.spring_layout(G)
nc = np.random.random(len(G))
nodes = nx.draw_networkx_nodes(G,pos,node_color=nc)
edges = nx.draw_networkx_edges(G,pos)
def update(n):
nc = np.random.random(len(G))
nodes.set_array(nc)
return nodes,
anim = FuncAnimation(fig, update, interval=20, blit=True)
而且可以通过以下代码生成一个静态的画面,节点的大小是根据数据来决定的:
nodes = nx.draw_networkx_nodes(G,pos,node_size=400*nc)
我知道没有类似于.set_array的东西可以用来设置节点的大小,那么最好的办法是什么呢?
1 个回答
1
相当于这个 self._size
属性。在新的 mpl 版本(1.4.0rc1 及以上)中,PathCollection
有一个 set_sizes
方法。