我正在尝试可视化贝叶斯网络,并希望叠加一个显示边际概率分布而不是标记/节点的简单条形图。
例如
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import networkx as nx
G = nx.DiGraph([
('Rain', 'Grass_Wet'),
('Sprinkler', 'Grass_Wet'),
('Rain', 'Sprinkler')])
app = dash.Dash(__name__)
def generate_network_graph(G):
pos = nx.drawing.layout.spectral_layout(G)
# Add additional attribute specifying node position
label_col = 'label'
for node in G.nodes:
G.nodes[node]['pos'] = list(pos[node])
G.nodes[node][label_col] = node
# Define Figure
traceRecode = [] # contains edge_trace, node_trace, middle_node_trace
index = 0
for edge in G.edges:
x0, y0 = G.nodes[edge[0]]['pos']
x1, y1 = G.nodes[edge[1]]['pos']
#weight = float(G.edges[edge]['TransactionAmt']) / max(edge1['TransactionAmt']) * 10
trace = go.Scatter(x=tuple([x0, x1, None]), y=tuple([y0, y1, None]),
mode='lines')
traceRecode.append(trace)
index = index + 1
node_trace = go.Scatter(x=[], y=[], hovertext=[], text=[], mode='markers+text', textposition="bottom center", marker={'size': 20})
index = 0
for node in G.nodes():
x, y = G.nodes[node]['pos']
node_trace['x'] += tuple([x])
node_trace['y'] += tuple([y])
index = index + 1
traceRecode.append(node_trace)
figure = {
"data": traceRecode,
"layout": go.Layout(title='Bayesian Network', showlegend=False, hovermode='closest',
margin={'b': 40, 'l': 40, 'r': 40, 't': 40},
xaxis={'showgrid': False, 'zeroline': False, 'showticklabels': False},
yaxis={'showgrid': False, 'zeroline': False, 'showticklabels': False},
height=600
)}
return figure
def create_bar_plot():
figure = go.Figure(go.Bar(
x=[0.6, 0.4],
y=['Yes', 'No'],
orientation='h'))
return figure
app.layout = html.Div(
[
html.Div(
# className="six columns",
children=[dcc.Graph(id="my-graph", figure=generate_network_graph(G))],
),
html.Div(children=[dcc.Graph(id="my-graph2", figure=create_bar_plot())]),
]
)
if __name__ == '__main__':
app.run_server()
将生成第一个输出。
但是我需要在每个节点上嵌入或重叠条形图,以显示该节点的边际概率分布。 我希望能够从networkx获得面向网络的信息,因为这对我的应用程序非常重要。该网络非常庞大,无法直接指定先验位置。
答案 0 :(得分:0)
没有功能可以在python中创建节点嵌入式条形图(有关替代方法-manual.cytoscape.org/en/stable/Styles.html,请参阅教程6:节点图)-但是可以使用破折号创建饼图-cytoscape,如下图所示
在Cytoscape默认破折号样式表中添加以下块将创建具有以下样式的节点
default_stylesheet = [{
"selector": "node",
"style": {
"background-color": "green",
"pie-size": "80%",
"pie-1-background-color": "#E8747C",
"pie-1-background-size": f"mapData(Yes, 0, 1, 0, 100)",
"pie-2-background-color": "#74CBE8",
"pie-2-background-size": f"mapData(No, 0, 1, 0, 100)",
}]
Dash-cytoscape允许链接中提供的大多数cytoscape js样式设置选项,这些选项可能足以满足您的特定用例
有用的链接:
https://js.cytoscape.org/demos/pie-style/ https://dash.plot.ly/cytoscape