数学建模——Python的有向图
Python Network(二)绘图draw系列draw(),draw_networkx(),draw_networkx_nodes(),draw_networkx_edges()
两个网站需要搭配着看
我依据网站写的代码
## 标签 Labels
# libraries
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
# import xlrd
# worksheet = xlrd.open_workbook("influence_data.xlsx")
# sheet_names= worksheet.sheet_names()
# print(sheet_names)
# for sheet_name in sheet_names:
# sheet = worksheet.sheet_by_name(sheet_name)
# rows = sheet.nrows # 获取行数
# cols = sheet.ncols # 获取列数,尽管没用到
# all_content = []
# cols = sheet.col_values(3) # 获取第二列内容, 数据格式为此数据的原有格式(原:字符串,读取:字符串; 原:浮点数, 读取:浮点数)
# print(cols)
# print(cols[3])
# print(type(cols[3])) #查看数据类型
# Build a dataframe with your connections
# str_1 = ['The Beatles'] * 533
# str_2 = ['The Rolling Stones'] * 295
# str_3 = ['Deep Purple'] * 94
# str_4 = ['Alice in Chains'] * 44
# str_5 = ['Barenaked Ladies'] * 15
str_1 = ['The Beatles'] * 7
str_2 = ['The Rolling Stones'] * 5
str_3 = ['Deep Purple'] * 3
str_4 = ['Alice in Chains'] *2
str_5 = ['Barenaked Ladies'] * 1
str = str_1 + str_2 +str_3 + str_4 + str_5
print(len(str))
List_1 = ['Carpenters','Chicago','Joe Cocker','Deep Purple','Neil Young','Donovan','Devo']
List_2 = ['Neil Young','Terry Reid','Joe Cocker','Blondie','Kaleo']
List_3 = ['Budgie','Neil Young','Krokus']
List_4 = ['Devo','Kyuss']
List_5 = ['Terry Reid']
llt_a = List_1 + List_2 + List_3 + List_4 + List_5
print(len(llt_a))
df = pd.DataFrame({ 'from':llt_a, 'to':str})
df
# Build your graph
G=nx.from_pandas_edgelist(df, 'from', 'to',create_using=nx.DiGraph())
# Custom the edges:
# font_size标签字体大小,font_color标签字体颜色,font_weight字体形式
nx.draw(G, with_labels=True, node_size=300, font_size=10, font_color="black", font_weight="bold",pos=nx.circular_layout(G))
plt.show()