利用networkx和matplotlib给我最喜爱的55部美剧来个天体排名
【最终呈现效果】
【代码实现及注释】
import networkx as nx标签:60,部美剧,Python,---,00FF00,70,90,80,MyUSTVNode From: https://blog.51cto.com/dcboy/5846531
import matplotlib.pyplot as plt
USTVNode=['Everybody Loves Raymond','Friends','Sex and the City','Desperate Housewives','Two and a Half Men',
'30 Rock','Ugly Betty','How I Met Your Mother','Modern Family','The Big Bang Theory','Veep','Silicon Valley',
'The Marvelous Mrs. Maisel','Two Broken Girls','Breaking Bad','Billions','Seinfeld','Prison Break',
'House of Cards','Monk','The Mentalist','Good Luck Charlie','Hanna Montana','Gossiple Girl','Heroes',
'CSI','Greys Anatony','24 hours','Criminal Minds','Bones','Westworld','Lost','Supernatural','Growing Pains',
'Better Call Saul','White Collar','Law & Order','The Good Wife','Wizards of Waverly Place','Mom','True Blood',
'The Last Man on Earth','Community','The 100','Without a Trace','The Shield','Southland','The Rookie',
'Masters of Sex','Orange Is the New Black','The Chair','Fresh Off the Boat','Mistresses','Significant Mother','Smallville']
#输入我最喜欢的美剧名称,形成列表USTVNode
USTVRank=['80','99','70','90','92','62','60','80','85','91','80',
'75','70','80','95','94','90','96','80','90','85','70',
'73','79','70','76','65','60','70','74','75','60','88',
'89','92','87','90','86','69','75','61','77','70','66',
'62','76','71','60','61','68','75','63','65','76','80']
#给对每部美剧的喜爱程度打分,分数越高越好
i=0
ranking={}
while i<len(USTVNode):
ranking.update({USTVNode[i]:USTVRank[i]})
i+=1
MyRanking = sorted(ranking.items(), key=lambda x: x[1], reverse=True)
#给美剧从高到低排序
MyUSTVNode=[]
MyUSTVRank=[]
for x in MyRanking:
MyUSTVNode.append(x[0])
MyUSTVRank.append(x[1])
mapping={}
for x in MyUSTVNode:
mapping.update({MyUSTVNode.index(x): x})
US=[int(x)*30 for x in MyUSTVRank]
#节点标签映射
USTVRank=[270, 265, 260, 255, 250, 245, 240, 235, 230, 225, 220,
215, 210, 205, 200, 195, 190, 185, 180, 175, 170, 165,
160, 155, 150, 145, 140, 135, 130, 125, 120, 115, 110,
105, 100, 95, 90, 85, 80, 75, 70, 65, 60,55,
50, 45, 40, 35, 30, 25, 20, 15, 10, 5, 0]
US=[int(x)*15 for x in USTVRank]
#绘制球体的体积大小数列
NodeColor=['#FFC0CB', '#DC143C', '#FFF0F5', '#DB7093', '#FF69B4', '#FF1493',
'#C71585', '#DA70D6', '#D8BFD8', '#DDA0DD', '#EE82EE', '#FF00FF',
'#FF00FF', '#8B008B', '#800080', '#BA55D3', '#9400D3', '#9932CC',
'#00FF00', '#8A2BE2', '#9370DB', '#7B68EE', '#6A5ACD', '#483D8B',
'#E6E6FA', '#F8F8FF', '#00FF00', '#00FF00', '#00FF00', '#00FF00',
'#90EE90', '#98FB98', '#8FBC8F', '#32CD32', '#00FF00', '#D8BFD8',
'#B0C4DE', '#DDA0DD', '#7FFF00', '#7CFC00', '#ADFF2F', '#FFF0F5',
'#F5F5DC', '#FAFAD2', '#FFFFF0', '#FFFFE0', '#FFFF00', '#E6E6FA',
'#BDB76B', '#FFFACD', '#000080', '#4169E1', '#6495ED', '#B0C4DE', '#FAEBD7']
#球体颜色列表
G=nx.path_graph(55)
G.add_nodes_from(MyUSTVNode)
nx.draw(nx.relabel_nodes(G,mapping),pos=nx.spiral_layout(G),with_labels=True,
font_size='14',font_color='black',font_weight='bold',edge_color='g',
node_shape='o',node_color=NodeColor, node_size=US)
plt.show()