Encoding and rendering (network) graphics in python

the code below does not display 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() 
+4
source share
2 answers

From looking at your code, I'm not quite sure what you had in mind, so it’s hard for me to work directly with it; however, I can show you how to create graphs in python and display them in Matplotlib .

Networkx is a great python library for generating graphs, analyzing them and rendering them through 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 enough modules to translate between formats) or using one of the Networkx gtx generators. To create the graph shown below, I am creating a specific type of binomial random graph erdos-renyi .

To create a graph in Networkx, I simply call the graph constructor and pass in the required number of nodes and the probability of creating an edge.

 G = NX.erdos_renyi_graph(10, .3) 

Displaying this graph in Networkx is trivial - just call draw and go to your graph. Behind the scenes, the network transfers all the data necessary for rendering your chart in Matplotlib (for example, node position, style attributes, labels, etc.) And it calls the Matplotlib plot method for you, passing all this data in. The only interaction with Matplotlib required by the user is to call show or savefig to display it on the screen or in the file, respectively.

 NX.draw(G) MPL.show() 

If you want to generate a graph yourself, pass it to Networkx for rendering through Matplotlib, which is also easy. For example, 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 an adjacency matrix; just use the appropriate constructor, Graph

 >>> G2 = NX.Graph(G) 

This graph is displayed in Matplotlib exactly the same as above, by calling the Networkx draw method, and then showing Matplotlib to display it on the screen.

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

enter image description here

+5
source

When you run this script, define H(x,y,gamma) , HenonIterate(x0,y0,n,gamma) and g() . It all happens.

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

He then complains that the HenonMap function HenonMap not defined. Find a suitable implementation that takes 6 parameters.

0
source

Source: https://habr.com/ru/post/1411223/


All Articles