2013-07-19 38 views
11

Tôi đang cố gắng hình dung một vài biểu đồ có nút đại diện cho các đối tượng khác nhau. Tôi muốn tạo ra một hình ảnh mà trông giống như ai ở đây:Vẽ đồ thị đa cấp với networkx?

enter image description here

Về cơ bản, tôi cần một cốt truyện 3D và khả năng để vẽ các cạnh giữa các nút trên cùng cấp hoặc nút trên mức độ khác nhau.

+1

Tôi nhận thấy thẻ nói python, nhưng bạn có bất kỳ gói hoặc tiện ích bổ sung nào khác không? Sẽ dễ dàng hơn nhiều nếu bạn nói với chúng tôi những công cụ bạn phải làm việc. – HardcoreBro

+1

Tôi có pydot, numpy và matplotlib. Tôi cũng đang chạy Python 2.7 nếu nó giúp. – Danny

Trả lời

6

Câu trả lời dưới đây có thể không phải là giải pháp hoàn chỉnh, nhưng là bản trình diễn làm việc để hiển thị biểu đồ 3D bằng networkx. networkx như vậy không thể hiển thị đồ thị 3D. Chúng tôi sẽ phải cài đặt mayavi để điều đó xảy ra.

import networkx as nx 
import matplotlib.pyplot as plt 
import numpy as np 
from mayavi import mlab 

import random 

def draw_graph3d(graph, graph_colormap='winter', bgcolor = (1, 1, 1), 
       node_size=0.03, 
       edge_color=(0.8, 0.8, 0.8), edge_size=0.002, 
       text_size=0.008, text_color=(0, 0, 0)): 

    H=nx.Graph() 

    # add edges 
    for node, edges in graph.items(): 
     for edge, val in edges.items(): 
      if val == 1: 
       H.add_edge(node, edge) 

    G=nx.convert_node_labels_to_integers(H) 

    graph_pos=nx.spring_layout(G, dim=3) 

    # numpy array of x,y,z positions in sorted node order 
    xyz=np.array([graph_pos[v] for v in sorted(G)]) 

    # scalar colors 
    scalars=np.array(G.nodes())+5 
    mlab.figure(1, bgcolor=bgcolor) 
    mlab.clf() 

    #---------------------------------------------------------------------------- 
    # the x,y, and z co-ordinates are here 
    # manipulate them to obtain the desired projection perspective 
    pts = mlab.points3d(xyz[:,0], xyz[:,1], xyz[:,2], 
         scalars, 
         scale_factor=node_size, 
         scale_mode='none', 
         colormap=graph_colormap, 
         resolution=20) 
    #---------------------------------------------------------------------------- 

    for i, (x, y, z) in enumerate(xyz): 
     label = mlab.text(x, y, str(i), z=z, 
          width=text_size, name=str(i), color=text_color) 
     label.property.shadow = True 

    pts.mlab_source.dataset.lines = np.array(G.edges()) 
    tube = mlab.pipeline.tube(pts, tube_radius=edge_size) 
    mlab.pipeline.surface(tube, color=edge_color) 

    mlab.show() # interactive window 

# create tangled hypercube 
def make_graph(nodes): 

    def make_link(graph, i1, i2): 
     graph[i1][i2] = 1 
     graph[i2][i1] = 1 

    n = len(nodes) 

    if n == 1: return {nodes[0]:{}} 

    nodes1 = nodes[0:n/2] 
    nodes2 = nodes[n/2:] 
    G1 = make_graph(nodes1) 
    G2 = make_graph(nodes2) 

    # merge G1 and G2 into a single graph 
    G = dict(G1.items() + G2.items()) 

    # link G1 and G2 
    random.shuffle(nodes1) 
    random.shuffle(nodes2) 
    for i in range(len(nodes1)): 
     make_link(G, nodes1[i], nodes2[i]) 

    return G 

# graph example 
nodes = range(10) 
graph = make_graph(nodes) 
draw_graph3d(graph) 

Mã này được sửa đổi từ một trong các ví dụ here. Vui lòng đăng mã trong trường hợp này, khi bạn thành công trong việc đạt được mục tiêu.

+0

Xin vui lòng xem trang 19 của tài liệu này - http://cs.brown.edu/~rt/gdhandbook/chapters/force-directed.pdf. Hình minh họa rõ ràng có cấu trúc giống như hình ảnh khách quan của bạn, và tôi đoán điều này được kết xuất với networkx và mayavi. – Vikram

Các vấn đề liên quan