示例#1
0
def render_graph_color(graph_object,filename_str):
	import pydot

	node_list, adjacency_matrix = graph_object.labels, graph_object.adjacency_matrix

	if not (adjacency_matrix.shape[0] == adjacency_matrix.shape[1]):
		raise Exception("adjacency matrix should be square")
	
	if not (len(node_list) == adjacency_matrix.shape[0]):
		raise Exception("number of nodes is inconsistent with the number of available node labels")
			 
	if is_directed(adjacency_matrix):
		graph = pydot.Dot(graph_type='digraph')
	else:
		graph = pydot.Dot(graph_type='graph')
		
	nodes_dot=[]

	for n in node_list:
		tmp = pydot.Node(n)
		nodes_dot.append(tmp)
		graph.add_node(tmp)

	for (i,j) in get_indices(adjacency_matrix):
				graph.add_edge(pydot.Edge(nodes_dot[i], nodes_dot[j], label=graph_object.property_str, labelfontcolor="#009933", fontsize="10.0", color=graph_object.edge_color))
	
	name =  "%s.png" % filename_str
	graph.write_png(name)
示例#2
0
def render_multi_prop_graph(multi_prop_graph,filename_str):
		
	node_list, adjacency_matrix = multi_prop_graph.labels, multi_prop_graph.adjacency_matrix

	if not (adjacency_matrix.shape[0] == adjacency_matrix.shape[1]):
		raise Exception("adjacency matrix should be square")
	
	if not (len(node_list) == adjacency_matrix.shape[0]):
		raise Exception("number of nodes is inconsistent with the number of available node labels")
			 
	if is_directed(adjacency_matrix):
		graph = pydot.Dot(graph_type='digraph')
		direction = 'forward'
	else:
		graph = pydot.Dot(graph_type='graph',compound='true',mindist='0',ranksep='0',nodesep='0')
		direction = 'none'
	
	nodes_dot=[]

	for n in node_list:
		tmp = pydot.Node(n)
		nodes_dot.append(tmp)
		graph.add_node(tmp)

	y = deepcopy(adjacency_matrix)


	for (i,j) in get_indices(y):
		if y[i][j] == 1:
			if y[j][i] ==1:
				y[j][i] =0
				direction = 'none'
			graph.add_edge(pydot.Edge(nodes_dot[i], nodes_dot[j], label=multi_prop_graph.property_str, labelfontcolor="#009933", fontsize="10.0", dir=direction,color=multi_prop_graph.edge_colormap[node_list[i],node_list[j]]))
		if is_directed(adjacency_matrix) :
			direction = 'forward'
			
	name =  "%s.png" % filename_str
	
	#graph = add_legend(graph, property_colormap)
	graph.write_png(name)
示例#3
0
def drawGraphFromSM2(SM, names, outFile, Cut):
	graph = pydot.Dot(graph_type='graph')

	# THRESHOLD SM:
	nonZeroMean = np.mean(SM[SM.nonzero()])
	if Cut:
		T = 5.0 * nonZeroMean
	else:
		T = 0.0;

	for i in range(SM.shape[0]):
		for j in range(SM.shape[0]):
			if SM[i,j] <= T:
				SM[i,j] = 0.0
			else:
				SM[i,j] = 1.0

	numOfConnections = sum(SM, axis = 0)
	#fig = plt.figure(1)
	#plot1 = plt.imshow(SM, origin='upper', cmap=cm.gray, interpolation='nearest')
	#plt.show()

	numOfConnections = 9*numOfConnections / max(numOfConnections)

	for i,f in enumerate(names):	
		if sum(SM[i,:])>0:
			fillColorCurrent = "{0:d}".format(int(ceil(numOfConnections[i])))
			# NOTE: SEE http://www.graphviz.org/doc/info/colors.html for color schemes
			node = pydot.Node(f, style="filled", fontsize="8", shape="egg", fillcolor=fillColorCurrent, colorscheme = "reds9")
			graph.add_node(node)
			
	for i in range(len(names)):
		for j in range(len(names)):
			if i<j:
				if SM[i][j] > 0:
					#gr.add_edge((names[i], names[j]))				
					edge = pydot.Edge(names[i], names[j])	
					graph.add_edge(edge)
	graph.write_png(outFile)
gr.add_edge(("Austria","Italy"))
gr.add_edge(("Austria","Czech Republic"))
gr.add_edge(("Austria","Slovakia"))
gr.add_edge(("Austria","Hungary"))
gr.add_edge(("Denmark","Germany"))
gr.add_edge(("Poland","Czech Republic"))
gr.add_edge(("Poland","Slovakia"))
gr.add_edge(("Poland","Germany"))
gr.add_edge(("Czech Republic","Slovakia"))
gr.add_edge(("Czech Republic","Germany"))
gr.add_edge(("Slovakia","Hungary"))

# Draw as PNG
dot = write(gr)
graph = pydot.graph_from_dot_data(dot)
graph.write_png('europe.png')
#gvv = gv.readstring(dot)
#gv.layout(gvv,'dot')
#gv.render(gvv,'png','europe.png')

# Then, draw the breadth first search spanning tree rooted in Switzerland
st, order = breadth_first_search(gr, root="Switzerland")
gst = digraph()
gst.add_spanning_tree(st)

dot = write(gst)
graph = pydot.graph_from_dot_data(dot)
graph.write_png('europe-st.png')

#gvv = gv.readstring(dot)
#gv.layout(gvv,'dot')