encoding and rendering a (network) graph in python encoding and rendering a (network) graph in python numpy numpy

encoding and rendering a (network) graph in python


From a look at your code, I'm not exactly sure what you had in mind so it's difficult for me to work directly with it; however, i can show you how to create graphs in python and render them in Matplotlib.

Networkx is an excellent python library for generating graphs, analyzing them, and rendering them via Matplotlib or Graphviz. For instance,

from matplotlib import pyplot as MPLimport networkx as NX                  # import networkx

You can create a graph in Networkx by importing a data file (Networkx has quite a modules to translate among formats) or by using one of Networkx' gtraph generators. To generate the graph shown below, i create a particular type of binomial random graph, erdos-renyi.

To create a graph in Networkx, i just call the graph constructor and pass in the desired number of nodes and the edge creation probability.

G = NX.erdos_renyi_graph(10, .3)

Rendering this graph in Networkx is trivial--just call draw and pass in your graph. Behind the scenes, Network passes all data required for rendering your graph in Matplotlib (e.g., node position, style attributes, labels, etc.) and calls Matplotlib's plot method for you, passing all of this data in. The only interaction with Matplotlib required by the user is to call show or savefig to render it on screen or to a file, respectively.

NX.draw(G)MPL.show()

If you want to generate the graph yourself then hand it over to Networkx to render via Matplotlib, that's also straightforward. For instance, below, i create a 5 x 5 NumPy array to represent an adjacency matrix (the most common format for representing sparse graph data):

>>> G = NP.random.randint(0, 2, 25).reshape(5, 5)>>> G  array([[0, 0, 1, 0, 1],        [1, 0, 0, 1, 1],        [0, 0, 1, 0, 1],        [0, 0, 1, 1, 1],        [0, 1, 0, 0, 1]])

Now convert the NumPy array to a Networkx graph using the standard Networkx constructor for a directed graph, DiGraph

>>> G1 = NX.DiGraph(G)>>> len(G1.nodes())      5>>> type(G1)      <class 'networkx.classes.digraph.DiGraph'>

Instead of a directed graph, you can create an undirected graph from the adjacency matrix; just use the appropriate constructor, Graph

>>> G2 = NX.Graph(G)

This graph is rendered in Matplotlib exactly like the one above--by calling Networkx' draw method then Matplotlib's show to render it on screen.

>>> NX.draw(G2)>>> MPL.show()

enter image description here


When you run this script, the H(x,y,gamma), HenonIterate(x0,y0,n,gamma) and g() get defined. Thats all that happens.

If you want to run g() at the end of the script, add a g().

It then complains that the HenonMap function is not defined. Do find a suitable implementation which takes in 6 parameters.