添加彩色面以绘制网络图

时间:2016-09-11 17:11:21

标签: plotly

我想使用plotly和python绘制一些拓扑复合体。挑战在于复合物在网络的某些部分具有彩色三角形面板或面。我在下面列出了一张图片来展示。

有没有办法使用plotly绘制这些彩色面孔?我可以使网络图工作,但不知道如何为面部着色。

enter image description here

1 个答案:

答案 0 :(得分:1)

假设您使用散点图绘制节点和边缘,可以通过shapestriangles添加为layout

只需在path

中添加shapes layout密钥即可
layout = plotly.graph_objs.Layout(
    shapes=[{'type': 'graph',
             'path': 'M 0 0 L 1 2 L 3 4 Z'
           }]
)

从x / y坐标0; 01; 23; 4绘制一个三角形。

下面的代码是从here修改的,它会创建一个随机图并为所有具有正好2条边的节点绘制一个三角形。

enter image description here

import plotly
import networkx as nx

def scatter_nodes(pos, labels=None, color=None, size=20, opacity=1):
    L = len(pos)
    trace = plotly.graph_objs.Scatter(x=list(), y=list(), mode='markers', marker=plotly.graph_objs.Marker(size=list()))
    for k in range(L):
        trace['x'].append(pos[k][0])
        trace['y'].append(pos[k][1])
    attrib = dict(name='', text=labels , hoverinfo='text', opacity=opacity)
    trace = dict(trace, **attrib)
    trace['marker']['size']=size
    return trace

def scatter_edges(G, pos, line_color=None, line_width=1):
    trace = plotly.graph_objs.Scatter(x=list(), y=list(), mode='lines')
    for edge in G.edges():
        trace['x'] += [pos[edge[0]][0],pos[edge[1]][0], None]
        trace['y'] += [pos[edge[0]][1],pos[edge[1]][1], None]  
        trace['hoverinfo'] = 'none'
        trace['line']['width'] = line_width
        if line_color:
            trace['line']['color'] = line_color
    return trace            

G = nx.Graph()
n = 13
Nodes = range(n)
G.add_nodes_from(Nodes)
Edges=[(0,7), (0,2), (1,3), (1,4), (1,7), (2,5), (2,8), (3, 4), (3,5), (4,6), (4,8), (5,7), (7,8), (9, 10), (9, 11), (6,10), (10, 11)]
G.add_edges_from(Edges)

axis = dict(showline=False,
          zeroline=False,
          showgrid=False,
          showticklabels=False,
          title='' 
          )
layout = plotly.graph_objs.Layout(
    showlegend=False,
    xaxis=plotly.graph_objs.XAxis(axis),
    yaxis=plotly.graph_objs.YAxis(axis),
    )

pos = nx.fruchterman_reingold_layout(G)   

labels = [str(k) for k in range(len(pos))]
trace1 = scatter_edges(G, pos)
trace2 = scatter_nodes(pos, labels=labels)

data = plotly.graph_objs.Data([trace1, trace2])

#let's get the triangles
triangles = list()
for i in range(n):
    if len(G.neighbors(i)) == 2:
        neighbors = G.neighbors(i)
        triangles.append(dict(type='path',
                              path='M {0} {1} L {2} {3} L {4} {5} Z'.format(
                                  pos[i][0],
                                  pos[i][1],
                                  pos[neighbors[0]][0],
                                  pos[neighbors[0]][1],
                                  pos[neighbors[1]][0],
                                  pos[neighbors[1]][1]),
                              fillcolor='rgba(44, 160, 101, 0.5)',
                              line=dict(color='rgb(44, 160, 101)')
                        ))
layout.update(shapes=triangles)
fig = plotly.graph_objs.Figure(data=data, layout=layout)
plotly.offline.plot(fig)