Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python绘制组织树图?_Python_Plot_Charts_Tree_Visualization - Fatal编程技术网

如何使用Python绘制组织树图?

如何使用Python绘制组织树图?,python,plot,charts,tree,visualization,Python,Plot,Charts,Tree,Visualization,我想建立一个组织结构图。如下图所示: A - B - C B - C - D A - E 这意味着A管理B,B管理C,C管理D,A管理E。我想知道是否可以将每个员工作为节点来构建树状图 A - B - C - D | E 有些代码如下,我不知道如何将其应用于我当前的人力资源数据 pip install python-igraph import igraph from igraph import Graph, EdgeSeq nr_vertices = 25 v_label = list(m

我想建立一个组织结构图。如下图所示:

A - B - C
B - C - D
A - E
这意味着A管理B,B管理C,C管理D,A管理E。我想知道是否可以将每个员工作为节点来构建树状图

A - B - C - D 
|
E
有些代码如下,我不知道如何将其应用于我当前的人力资源数据

pip install python-igraph
import igraph
from igraph import Graph, EdgeSeq
nr_vertices = 25
v_label = list(map(str, range(nr_vertices)))
G = Graph.Tree(nr_vertices, 2) # 2 stands for children number
lay = G.layout('rt')

position = {k: lay[k] for k in range(nr_vertices)}
Y = [lay[k][1] for k in range(nr_vertices)]
M = max(Y)

es = EdgeSeq(G) # sequence of edges
E = [e.tuple for e in G.es] # list of edges

L = len(position)
Xn = [position[k][0] for k in range(L)]
Yn = [2*M-position[k][1] for k in range(L)]
Xe = []
Ye = []
for edge in E:
    Xe+=[position[edge[0]][0],position[edge[1]][0], None]
    Ye+=[2*M-position[edge[0]][1],2*M-position[edge[1]][1], None]

labels = v_label

import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(x=Xe,
               y=Ye,
               mode='lines',
               line=dict(color='rgb(210,210,210)', width=4),
               hoverinfo='none'
               ))
fig.add_trace(go.Scatter(x=Xn,
              y=Yn,
              mode='markers',
              name='bla',
              marker=dict(symbol='circle-dot',
                            size=28,
                            color='#6175c1',    #'#DB4551',
                            line=dict(color='rgb(50,50,50)', width=1)
                            ),
              text=labels,
              hoverinfo='text',
              opacity=0.8
              ))

def make_annotations(pos, text, font_size=15, 
font_color='rgb(250,250,250)'):
    L=len(pos)
    if len(text)!=L:
        raise ValueError('The lists pos and text must have the same len')
    annotations = []
    for k in range(L):
        annotations.append(
            dict(
                text=labels[k], # or replace labels with a different list for the text within the circle
            x=pos[k][0], y=2*M-position[k][1],
            xref='x1', yref='y1',
            font=dict(color=font_color, size=font_size),
            showarrow=False)
    )
return annotations

axis = dict(showline=False, # hide axis line, grid, ticklabels and  title
        zeroline=False,
        showgrid=False,
        showticklabels=False,
        )

fig.update_layout(title= 'Tree with Reingold-Tilford Layout',
          annotations=make_annotations(position, v_label),
          font_size=12,
          showlegend=False,
          xaxis=axis,
          yaxis=axis,
          margin=dict(l=40, r=40, b=85, t=100),
          hovermode='closest',
          plot_bgcolor='rgb(248,248,248)'
          )
fig.show()
我不知道圆圈里的数字是怎么显示的。相反,我希望员工的姓名显示在圆圈中。 有人能帮忙吗