Python 来自数据框的 networkx 图,线宽取决于列值
Python networkx plot from dataframe, with linewidth depending on columns values
我正在研究不同机场之间航线的表示,并希望使用 networkx 图来表示它们。
输入数据为dataframe,示例:
from, to, airline, trip_number
Paris, New York, Air France, AF001
Paris, Munich, Air France, AF002
Paris, New York, Air France, AF003
Toronto, Paris, Air Canada, AC001
Toronto, Munich, Air Canada, AC002
Munich, New York, Lufthansa, LF001
Franfort, Los Angeles, Lufthansa, LF002
Francfort, Paris, Lufthansa, LF003
Paris, Francfort, Lufthansa, LF004
Paris, Francfort, Air France, AF004
Paris, Francfort, Air Berlin, AB001
我设法获得了网络表示,但我缺少两项:
- 每个节点的标签
- 当行程数增加时,增加连接线的大小(例如:多伦多巴黎小,因为有 1 趟,巴黎法兰克福有 3 趟,宽
当前最小代码,df 是数据帧:
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
from nxviz.plots import CircosPlot
G = nx.from_pandas_edgelist(df, 'from', 'to')
nx.draw(G, node_size=5, node_color='red')
plt.show()
感谢您的帮助
下面应该做的工作:
import pandas as pd
from io import StringIO
import networkx as nx
import matplotlib.pyplot as plt
data = ('from, to, airline, trip_number\n'
'Paris, New York, Air France, AF001\n'
'Paris, Munich, Air France, AF002\n'
'Paris, New York, Air France, AF003\n'
'Toronto, Paris, Air Canada, AC001\n'
'Toronto, Munich, Air Canada, AC002\n'
'Munich, New York, Lufthansa, LF001\n'
'Franfort, Los Angeles, Lufthansa, LF002\n'
'Francfort, Paris, Lufthansa, LF003\n'
'Paris, Francfort, Lufthansa, LF004\n'
'Paris, Francfort, Air France, AF004\n'
'Paris, Francfort, Air Berlin, AB001')
df = pd.read_csv(StringIO(data), sep=", ")
# see
short_df = pd.DataFrame({'count': df.groupby(["from", "to"]).size()}).reset_index()
G = nx.from_pandas_edgelist(short_df, source='from', target='to', edge_attr="count")
# edge size, see
weights = [G[u][v]['count'] for u,v in G.edges()]
nx.draw(G, node_size=5, node_color='red', with_labels=True, width=weights)
plt.show()
说明
您首先需要检索航班数量,这 pandas
可以轻松完成。用here, I create a new data frame with only three columns ("from", "to", "count"). Afterwards, you need to include the edge attribute while creating the graph, i.e., add edge_attr="count"
. Then, I followed this answer的答案来控制边宽。
最后,向图中添加标签是 draw
中的 with_labels=True
参数。您可以使用 draw_networkx
.
的所有参数
我正在研究不同机场之间航线的表示,并希望使用 networkx 图来表示它们。
输入数据为dataframe,示例:
from, to, airline, trip_number
Paris, New York, Air France, AF001
Paris, Munich, Air France, AF002
Paris, New York, Air France, AF003
Toronto, Paris, Air Canada, AC001
Toronto, Munich, Air Canada, AC002
Munich, New York, Lufthansa, LF001
Franfort, Los Angeles, Lufthansa, LF002
Francfort, Paris, Lufthansa, LF003
Paris, Francfort, Lufthansa, LF004
Paris, Francfort, Air France, AF004
Paris, Francfort, Air Berlin, AB001
我设法获得了网络表示,但我缺少两项:
- 每个节点的标签
- 当行程数增加时,增加连接线的大小(例如:多伦多巴黎小,因为有 1 趟,巴黎法兰克福有 3 趟,宽
当前最小代码,df 是数据帧:
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
from nxviz.plots import CircosPlot
G = nx.from_pandas_edgelist(df, 'from', 'to')
nx.draw(G, node_size=5, node_color='red')
plt.show()
感谢您的帮助
下面应该做的工作:
import pandas as pd
from io import StringIO
import networkx as nx
import matplotlib.pyplot as plt
data = ('from, to, airline, trip_number\n'
'Paris, New York, Air France, AF001\n'
'Paris, Munich, Air France, AF002\n'
'Paris, New York, Air France, AF003\n'
'Toronto, Paris, Air Canada, AC001\n'
'Toronto, Munich, Air Canada, AC002\n'
'Munich, New York, Lufthansa, LF001\n'
'Franfort, Los Angeles, Lufthansa, LF002\n'
'Francfort, Paris, Lufthansa, LF003\n'
'Paris, Francfort, Lufthansa, LF004\n'
'Paris, Francfort, Air France, AF004\n'
'Paris, Francfort, Air Berlin, AB001')
df = pd.read_csv(StringIO(data), sep=", ")
# see
short_df = pd.DataFrame({'count': df.groupby(["from", "to"]).size()}).reset_index()
G = nx.from_pandas_edgelist(short_df, source='from', target='to', edge_attr="count")
# edge size, see
weights = [G[u][v]['count'] for u,v in G.edges()]
nx.draw(G, node_size=5, node_color='red', with_labels=True, width=weights)
plt.show()
说明
您首先需要检索航班数量,这 pandas
可以轻松完成。用here, I create a new data frame with only three columns ("from", "to", "count"). Afterwards, you need to include the edge attribute while creating the graph, i.e., add edge_attr="count"
. Then, I followed this answer的答案来控制边宽。
最后,向图中添加标签是 draw
中的 with_labels=True
参数。您可以使用 draw_networkx
.