交互式可点击的Networkx树,使用python可视化时间序列数据

时间:2018-10-18 20:54:32

标签: python-3.x networkx

我想知道, 是否可以在Networkx中创建一个交互式图形,您可以在其中单击一个节点,然后在图形旁边显示该节点的时间序列分析? 我正在尝试在python 3中做到这一点。 你知道有什么阴谋可以做到吗?

我正在执行以下操作:

import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode
init_notebook_mode()
from plotly.offline import iplot
import networkx as nx

G=nx.random_geometric_graph(3,0.99)
pos=nx.get_node_attributes(G,'pos')

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.node[edge[0]]['pos']
x1, y1 = G.node[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='YlGnBu',
reversescale=True,
color=[],
size=10,
colorbar=dict(
thickness=15,
title='Node Connections',
xanchor='left',
titleside='right'
),
line=dict(width=2)))

for node in G.nodes():
x, y = G.node[node]['pos']
node_trace['x'] += tuple([x])
node_trace['y'] += tuple([y])
for node, adjacencies in enumerate(G.adjacency()):
node_trace['marker']['color']+=tuple([len(adjacencies[1])])
node_info = '# of connections: '+str(len(adjacencies[1]))
node_trace['text']+=tuple([node_info])
fw_graph = go.FigureWidget(data=[edge_trace, node_trace],
layout=go.Layout(
title='
Network graph made with Python',
titlefont=dict(size=16),
showlegend=False,
hovermode='closest',
margin=dict(b=20,l=5,r=5,t=40),
annotations=[ dict(
text="Python code: https://plot.ly/ipython-notebooks/network-graphs/",
showarrow=False,
xref="paper", yref="paper",
x=0.005, y=-0.002 ) ],
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)))

iplot(fw_graph, show_link=False)

trace1 = go.Bar(
x=['giraffes', 'orangutans', 'monkeys'],
y=[20, 14, 23],
name='SF Zoo'
)
trace2 = go.Bar(
x=['giraffes', 'orangutans', 'monkeys'],
y=[12, 18, 29],
name='LA Zoo'
)

data = [trace1, trace2]
layout = go.Layout(
barmode='group'
)

fw_analytics = go.FigureWidget(data=data, layout=layout)
iplot(fw_analytics, show_link=False)

import ipywidgets as ipw
def callback(trace, points, state):
if not points.point_inds:
return
ind = points.point_inds[0] # get the index of the hovered node
# use this index to extract data for the analytics of this node index
# and then use these data to update the previous fw_analytics
with fw_analytics.batch_update(): # update the fw_analytics with info extracted from the last
# hovered node
fw_analytics.data[0].x = [2,3,4]
fw_analytics.data[0].y = ['cat', 'mouse', 'dog']
fw_analytics.data[0].marker.color = 'yellow'

trace = fw_graph.data[1]
trace.on_hover(callback)
ipw.HBox([fw_graph, fw_analytics])

fw_graph和fw_analytics会自行绘制,但是当我将其放在HBox中时,它不会产生任何输出吗? 我在这里做错什么了吗?

0 个答案:

没有答案