4

the code below does not render my graph.

import numpy
import matplotlib.pyplot as plt
import matplotlib as mpl
import pylab
import random
import scipy
from matplotlib.mlab import griddata
from pylab import *
from scipy import ndimage
def H(x,y,gamma):
    val = HenonMap(x,y,1,1,0.2,gamma)
    return val
def HenonIterate(x0,y0,n,gamma):
    (x,y) = H(x0,y0,gamma)
    for i in xrange (0,n):
        (x,y)=H(x,y,gamma)
        if (pow(x,2)) + (pow(y,2)) > 100:
            return i
    return n

def g():
    x2=1000
    y2=1000
    max=100
    u = zeros([x2,y2])
    for x in range(x2):
        for y in range(y2):
            y0= .01*y-5.0
            x0= -.01*x+5.0
            u[x][y] = HenonIterate(x0,y0,max,1.03)
        imshow(u)
        show()
6
  • 2
    How do you run the code? Nothing should show because you don't run any main method. Commented May 8, 2012 at 4:29
  • I run it using python shell. It's suppose to show a graph. Commented May 8, 2012 at 4:31
  • 1
    It doesn't because you don't run anything. You define a few functions but you don't actually run them. Commented May 8, 2012 at 4:31
  • also, where is HenonMap defined? Commented May 8, 2012 at 4:41
  • 1
    Have posted full code? And you may add "g()" at end of file. Commented May 8, 2012 at 4:47

2 Answers 2

6

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 MPL
import 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

Sign up to request clarification or add additional context in comments.

4 Comments

While a nice introduction to networkx, I think that the OP misused the word graph. There is code to imshow(u) where u is a square array of floats which, to me, implies a heatmap of some kind. A second hint is that a Hénon map en.wikipedia.org/wiki/H%C3%A9non_map is a toy dynamical system that exhibits chaotic behaviors. The pictures on the wiki page suggest a 2D image map as well.
@Hooked just checked the OP; you're right. the Henon reference went right over my head though--thanks for the explanation.
@doug What if I wanted to use your adjacency matrix approach to build a grid network with something like nx.grid_2d_graph(N,N)? In this case I need the positions to respect this line pos = dict( (n, n) for n in G.nodes() ), with edges coming from the adjacency matrix? The only constraint is that thre must be no disconnected nodes, and when the matrix is created, all (i,j) positions must have the same probability of landing a 1. Feasible? Should I turn this into a proper question? Thanks!
How did you get it to show the node numbers in the plot?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.