首页 > 解决方案 > 使用网络图绘制时间选择器

问题描述

我正在使用具有公司电子邮件流量的数据集(安然数据集的修改版本)进行网络图可视化。在此图中,我将公司中的所有人员作为节点,将这些人之间的所有电子邮件作为边。因为有很多边,我想制作一个时间范围滑块,只显示在这个时间段内发送的邮件。我将日期作为边缘的属性,但我不知道如何为此制作范围滑块以及如何制作它以使某些边缘可见而其他边缘不可见。我发现的唯一滑块在 x 轴上工作,但因为时间在两个轴上都不是变量,所以我不能使用它。我还尝试制作一个下拉菜单,您只能在其中选择年份,但我也无法让它与边缘的属性一起使用。所以我的问题是:如何制作滑块或下拉菜单来选择某个时间段并仅显示该时间段内的边缘?我必须制作网络图的代码

df_enron = pd.read_csv('enron-v1.csv', parse_dates=['date']) #reading the enron csv and storing it as dataframe

#get a list of all the years in the dataset
df_enron['year'] = pd.DatetimeIndex(df_enron['date']).year
years = list(df_enron['year'].unique())

mailsenders = list(df_enron["fromId"].unique()) #define the senders and receivers as nodes
mailreceivers = list(df_enron["toId"].unique())
jobtitles = list(df_enron['fromJobtitle'].unique()) #list of all the job titles
jobtitlesindex = jobtitles.copy()
node_list = list(set(mailsenders+mailreceivers)) #list of everything that needs to be a node
G = nx.Graph() #defining the graph

jobtitlescount = len(jobtitles) #amount of functions in the company
    

for i in node_list:
    G.add_node(i) #adding every node to the graph 

for i,j in df_enron.iterrows(): #adding the edges
    G.add_edges_from([(j["fromId"],j["toId"], {"date": df_enron['date'].loc[i]})])
#actually making the graph with plotly

edge_trace = go.Scatter(
    x=[],
    y=[],
    line=dict(width=0.5, color = '#888'),
    hoverinfo='none',
    mode='lines')
for edge in G.edges():
    x0, y0 = G.nodes[edge[0]]['pos']
    x1, y1 = G.nodes[edge[1]]['pos']
    edge_trace['x'] += tuple([x0, x1, None])
    edge_trace['y'] += tuple([y0, y1, None])
node_trace = go.Scatter(
    x=[],
    y=[],
    text=[],
    mode='markers',
    hoverinfo='text',
    marker=dict(
        showscale=True,
        colorscale='RdBu',
        reversescale=True,
        color=[],
        size=15,
        colorbar=dict(
            thickness=10,
            title='Node Connections',
            xanchor='left',
            titleside='right'
        ),
        line=dict(width=0)))
for node in G.nodes():
    x, y = G.nodes[node]['pos']
    node_trace['x'] += tuple([x])
    node_trace['y'] += tuple([y])
    
#adding annotations to the nodes and edges

for node, adjacencies in enumerate(G.adjacency()):
    node_trace['marker']['color']+=tuple([len(adjacencies[1])])
    node_info = G.nodes[node+1]['Title']+', ID: '+adjacencies[0].astype(str) +', # of connections: '+str(len(adjacencies[1])) + ', sentiment: ' + str(G.nodes[node+1]['sentiment'])  #DE .astype(str) IS TOEGEVOEGD
    node_trace['text']+=tuple([node_info])
    
    
#actually plotting the graph

fig = go.Figure(data=[edge_trace, node_trace],
             layout=go.Layout(
                title='<br>Enron email traffic',
                titlefont=dict(size=16),
                showlegend=False,
                hovermode='closest',
                margin=dict(b=20,l=5,r=5,t=40),
                annotations=[ dict(
                text="No. of connections",
                showarrow=False,
                xref="paper", yref="paper") ],
                xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
                yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)))

iplot(fig)

结果图如下: 按职位分组绘制 如果有人有任何想法/提示,他们真的会帮助我!

标签: pythonpandasplotlyvisualizationnetworkx

解决方案


推荐阅读