我想让它这样我可以把一个$的重量数字,并设置为边缘文本。有人能告诉我怎么做吗?例如,如果边的权重是20,我希望边文本为“$20”
这是我的密码。在
import json
import networkx as nx
import matplotlib.pyplot as plt
import os
import random
from networkx import graphviz_layout
G=nx.Graph()
for fn in os.listdir(os.getcwd()):
with open(fn) as data_file:
data = json.load(data_file)
name=data["name"]
name=name.split(',')
name = name[1] + " " + name[0]
cycle=data["cycle"]
contributions=data["contributions"]
contributionListforIndustry=[]
colorList=[]
colorList.append((random.uniform(0,1),random.uniform(0,1),random.uniform(0,1)))
for contibution in contributions:
amount=contibution["amount"]
industryName=contibution["name"]
metric=contibution["metric"]
colorList.append((random.uniform(0,1),random.uniform(0,1),random.uniform(0,1)))
contributionListforIndustry.append((industryName,amount))
G.add_edge(name,industryName,weight=amount, metricval=metric)
position=nx.graphviz_layout(G,prog='twopi',args='')
nx.draw(G,position,with_labels=False,node_color=colorList )
for p in position: # raise text positions
t= list(position[p])
t[1]=t[1]+10
position[p]=tuple(t)
nx.draw_networkx_edge_labels(G,position)
nx.draw_networkx_labels(G, position)
plt.title("Break down for donations to " + name + " from agriculture industry for " + str(cycle) )
plt.show()
另外,如果有人可以告诉我如何使文本看起来在绘图的前面,即文本没有被边缘可视化地切片,如果边缘文本应该通过边缘,则边缘文本位于边缘的顶部。最后,由于某种原因,我的情节没有出现。如果有人知道解决这个问题的方法,那就太棒了。谢谢各位。总是帮了大忙。在
The documentation概述了必须使用
edge_labels
参数来指定自定义标签。默认情况下,使用边缘数据的字符串表示。在下面的示例中,创建了这样一个字典:它将边缘元组作为键,格式化字符串作为值。在要使节点标签更加突出,可以向相应的文本元素添加边界框。您可以在
draw_networkx_labels
创建它们之后执行此操作:编辑:
你不能真正地移除轴,因为它们是整个图形的容器。所以人们通常做的就是让脊椎隐形:
^{pr2}$设置标题应该很简单:
结果:
相关问题 更多 >
编程相关推荐