示例#1
0
from vedo.pyplot import DirectedGraph

# Create some graph with nodes and edges
# layouts: [2d, fast2d, clustering2d, circular, circular3d, cone, force, tree]
g = DirectedGraph(layout='fast2d')
for i in range(6):
    g.addChild(i)  # add one child node to node i
for i in range(3):
    g.addChild(i)
for i in range(3):
    g.addChild(i)
for i in range(7, 9):
    g.addChild(i)
for i in range(3):
    g.addChild(12)  # add 3 children to node 12
g.build()

dgraph = g.unpack(0).lineWidth(4)  # get the graph lines
nodes = dgraph.points()  # get the 3d points of the nodes

pts = Points(nodes, r=12).c('red').alpha(0.5)

v1 = ['node' + str(n) for n in range(len(nodes))]
v2 = [sin(x) for x in range(len(nodes))]
labs1 = pts.labels(v1, scale=.04, italic=True).addPos(.05, 0.04, 0).c('green')
labs2 = pts.labels(v2, scale=.04, precision=3).addPos(.05, -.04, 0).c('red')

# Interpolate the node value to color the edges:
dgraph.clean().pointColors(v2, cmap='viridis').addScalarBar()

# This would colorize the edges directly with solid color based on the v3 array:
示例#2
0
##################### Use networkx to create random nodes and edges
# import networkx
# G = networkx.gnm_random_graph(n=20, m=35)
# for i, j in G.edges(): g.addEdge(j,i)

##################### Manually create nodes and edges
for i in range(6): g.addChild(i)  # add one child node to node i
for i in range(3): g.addChild(i)
for i in range(3): g.addChild(i)
for i in range(7,9): g.addChild(i)
for i in range(3): g.addChild(12) # add 3 children to node 12
g.addEdge(1,16)

##################### build and draw
graph = g.build().unpack(0).lineWidth(4) # get the vedo 3d graph lines
nodes = graph.points()                   # get the 3d points of the nodes

pts = Points(nodes, r=10).lighting('off')

v1 = ['node'+str(n) for n in range(len(nodes))]
v2 = [sin(x) for x in range(len(nodes))]
labs1 = pts.labels(v1, scale=.04, italic=True).addPos(.05,0.04,0).c('green')
labs2 = pts.labels(v2, scale=.04, precision=3).addPos(.05,-.04,0).c('red')

# Interpolate the node value to color the edges:
graph.cmap('viridis', v2).addScalarBar3D(c='k').addPos(.3,0,0)
pts.cmap('viridis', v2)

# This would colorize the edges directly with solid color based on a v3 array:
# v3 = [sin(x) for x in range(graph.NCells())]
示例#3
0
#g = Graph(layout='2d', zrange=7)
g = DirectedGraph(layout='cone')
#g = DirectedGraph(layout='circular3d', height=1, radius=1.5)
#g = DirectedGraph(layout='force')

# Vertex generation is automatic,
#  add a child to vertex0, so that now vertex1 exists
g.addChild(0, edgeLabel="Mother giving birth\nto her little baby cell")
g.addChild(1)
g.addChild(1)
g.addChild(2)
g.addChild(2)
g.addChild(2)
g.addChild(3)
g.addChild(3, edgeLabel="It's a male!")
g.addChild(4)
g.addChild(4)
for i in range(7):
    g.addChild(5, vertexLabel="cell5_" + str(i))
g.addChild(7)
g.addChild(7)
g.addChild(7)

g.build()  # optimize layout

g.unpack(0).color('dg').lineWidth(
    3)  #0=graph, 1=vertexLabels, 2=edgeLabels, 3=arrows
g.unpack(2).color('dr')

show(g, __doc__, axes=9, elevation=-40)