亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我無法根據三個標準用 networkx 形成圖表

我無法根據三個標準用 networkx 形成圖表

叮當貓咪 2023-10-18 16:30:54
我是Python新手。請幫我解決圖形構建的問題。我有一個屬性為“來源”、“對話者”和“頻率”的數據庫。三行的示例:我需要基于源對話者構建一個圖表,但也考慮了頻率。像這樣:我的代碼:dic_values={Source:[24120.0,24120.0,24120.0], Interlocutor:[34,34,34],Frequency:[446625000, 442475000, 445300000]session_graph=pd.DataFrame(dic_values)friquency=session_graph['Frequency'].unique()plt.figure(figsize=(10,10))for i in range(len(friquency)):   df_friq=session_subset[session_subset['Frequency']==friquency[i]]   G_frique=nx.from_pandas_edgelist(df_friq,source='Source',target='Interlocutor')   pos = nx.spring_layout(G_frique)   nx.draw_networkx_nodes(G_frique, pos, cmap=plt.get_cmap('jet'), node_size = 20)   nx.draw_networkx_edges(G_frique, pos,  arrows=True)   nx.draw_networkx_labels(G_frique, pos)plt.show()  我有這樣的:
查看完整描述

1 回答

?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

你的問題需要一個MultiGraph


import networkx as nx

import matplotlib.pyplot as plt

import pandas as pd

import pydot

from IPython.display import Image



dic_values = {"Source":[24120.0,24120.0,24120.0], "Interlocutor":[34,34,34],

? ? ? ? ? ? ? "Frequency":[446625000, 442475000, 445300000]}


session_graph = pd.DataFrame(dic_values)

sources = session_graph['Source'].unique()

targets = session_graph['Interlocutor'].unique()



#create a Multigraph and add the unique nodes

G = nx.MultiDiGraph()

for n in [sources, targets]:

? ? G.add_node(n[0])

? ??

#Add edges, multiple connections between the same set of nodes okay.?

# Handled by enum in Multigraph? ??


#Itertuples() is a faster way to iterate through a Pandas dataframe. Adding one edge per row

for row in session_graph.itertuples():

? ? #print(row[1], row[2], row[3])

? ? G.add_edge(row[1], row[2], label=row[3])? ? ? ??

? ? ? ??


#Now, render it to a file...

p=nx.drawing.nx_pydot.to_pydot(G)

p.write_png('multi.png')

Image(filename='multi.png') #optional?

這將產生以下結果:

https://img1.sycdn.imooc.com/652f98180001036204470236.jpg

請注意,當您使用 Graphviz/Pydot 時,節點布局會更加棘手。



查看完整回答
反對 回復 2023-10-18
  • 1 回答
  • 0 關注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號