如何解决Plotly Dash:在 Python 中绘制 networkx
我正在尝试在 Python dash 中绘制一个 Networkx 图形以动态更改它。生成networkx图的代码如下:
def networkGraph(EGDE_VAR):
edges = [[EGDE_VAR,'B'],['B','C'],'D']]
G = nx.Graph()
G.add_edges_from(edges)
pos = nx.spring_layout(G)
plt.figure()
fig = nx.draw(G,pos,edge_color='black',width=1,linewidths=1,\
node_size=500,node_color='pink',alpha=0.9,\
labels={node:node for node in G.nodes()})
return(fig)
EGDE_VAR = 'K'
networkGraph(EGDE_VAR)
如果我尝试运行上面的函数它工作正常,所以我得到:
现在我想创建一个 Python 破折号,通过将其设置到输入框中来动态更改 EDGE_VAR。所以我尝试了:
#-*- coding: utf-8 -*-
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input,Output
# import the css template,and pass the css template into dash
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__,external_stylesheets=external_stylesheets)
app.title = "Dash Networkx"
EGDE_VAR = 'R'
# Input box para procurar o sinistro
app.layout = html.Div([
html.I("Write your EDGE_VAR"),html.Br(),dcc.Input(id="EGDE_VAR",type="text",placeholder=""),dcc.Graph(id='my-graph'),]
)
@app.callback(
Output("my-graph","figure"),[Input("EGDE_VAR","value")],)
def update_output(EGDE_VAR):
return networkGraph(EGDE_VAR)
if __name__ == '__main__':
app.run_server(debug=True,use_reloader=False)
但它不起作用。有什么想法吗?
解决方法
您的代码的问题在于 networkGraph()
函数没有返回 Plotly 图形对象,请参阅 Plotly documentation on network graphs。我根据您的代码在下面添加了一个示例。
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input,Output
import plotly.graph_objects as go
import networkx as nx
# Plotly figure
def networkGraph(EGDE_VAR):
edges = [[EGDE_VAR,'B'],['B','C'],'D']]
G = nx.Graph()
G.add_edges_from(edges)
pos = nx.spring_layout(G)
# edges trace
edge_x = []
edge_y = []
for edge in G.edges():
x0,y0 = pos[edge[0]]
x1,y1 = pos[edge[1]]
edge_x.append(x0)
edge_x.append(x1)
edge_x.append(None)
edge_y.append(y0)
edge_y.append(y1)
edge_y.append(None)
edge_trace = go.Scatter(
x=edge_x,y=edge_y,line=dict(color='black',width=1),hoverinfo='none',showlegend=False,mode='lines')
# nodes trace
node_x = []
node_y = []
text = []
for node in G.nodes():
x,y = pos[node]
node_x.append(x)
node_y.append(y)
text.append(node)
node_trace = go.Scatter(
x=node_x,y=node_y,text=text,mode='markers+text',marker=dict(
color='pink',size=50,width=1)))
# layout
layout = dict(plot_bgcolor='white',paper_bgcolor='white',margin=dict(t=10,b=10,l=10,r=10,pad=0),xaxis=dict(linecolor='black',showgrid=False,showticklabels=False,mirror=True),yaxis=dict(linecolor='black',mirror=True))
# figure
fig = go.Figure(data=[edge_trace,node_trace],layout=layout)
return fig
# Dash app
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__,external_stylesheets=external_stylesheets)
app.title = 'Dash Networkx'
app.layout = html.Div([
html.I('Write your EDGE_VAR'),html.Br(),dcc.Input(id='EGDE_VAR',type='text',value='K',debounce=True),dcc.Graph(id='my-graph'),]
)
@app.callback(
Output('my-graph','figure'),[Input('EGDE_VAR','value')],)
def update_output(EGDE_VAR):
return networkGraph(EGDE_VAR)
if __name__ == '__main__':
app.run_server(debug=True,host='0.0.0.0',port=1234)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。