2

I am trying to animate the graph, but jupyter is giving error:

MovieWriter imagemagick unavailable. And just animating the 1st image(which is obvious as MovieWriter is not working). How to fix it?

Python version: 3

Here is the code

import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
from matplotlib.animation import FuncAnimation

# number of nodes
size = 10

# generate graph
G=nx.complete_graph(size)

frame = np.random.randint(0, 5, (size, size)) # random ndarray between 0 and 5, length and number of frames = number of nodes in the graph

pos = nx.spring_layout(G)
nodes = nx.draw_networkx_nodes(G,pos)
edges = nx.draw_networkx_edges(G,pos)
plt.axis('off')

def update(i):
    nc = frame[i] # np.random.randint(2, size=200)
    nodes.set_array(nc)
    return nodes,

# output animation; its important I save it
fig = plt.gcf()
ani = FuncAnimation(fig, update, interval=50, frames=range(size), blit=True)
ani.save('crap.gif', writer='imagemagick',  savefig_kwargs={'facecolor':'white'}, fps=1)

Expectation: Animation should be working and will be able to show the updated color

Md Sajid
  • 131
  • 1
  • 1
  • 13
  • 1
    matplotlib requires some tool to display/write images. In this case it is looking for ImageMagick. If you do not have it installed, then you will need to install it. Alternately, I believe that matplotlib can use other display tools. But you will need to look at its list of display tools from its documentation. Your`ani.save` is specifically asking for ImageMagick as its image writer. – fmw42 Apr 27 '19 at 19:11

1 Answers1

2

It is working after installing the networkx package and adding pillowwritter as shown below.

import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
from matplotlib.animation import FuncAnimation, PillowWriter 

networkx

Axe319
  • 4,255
  • 3
  • 15
  • 31
yudhi irw
  • 21
  • 2