Networkx DiGraph节点,设置边框和内部颜色以及格式化文本。

3
我正在使用Django,在我的视图中生成各方之间关系的图表。问题出现在以下部分:
nx.set_node_attributes(G, fill_coloring, name="background")

但是不幸的是,这并不起作用。我无法在文档中找到如何正确设置它的方法。我的目标是根据与其相关的主数据来直接对每个节点进行着色/格式化。

nx.set_node_attributes(G, border_coloring, name="color")

通过上述方法,我能够设置边框颜色,但无法设置内部颜色。我忽略了什么?

理想情况下,稍后通过添加信息到线条等来实现,但目前我想以特定颜色着色节点本身。目前,它只能着色边框。我忽略了什么/误解了什么?

def index(request):

    filename = f'{datetime.now():%Y-%m-%d_%H-%M-%S}.png'
    save_path = 'static/app/master/routing/'
    completeName = os.path.join(save_path, filename)
    print(completeName)

    G = nx.DiGraph()

    routings = Customer_Routing.objects.all()

    end_customers = []
    border_coloring = {}
    fill_coloring = {}

    for e in routings:
        G.add_edge(e.customer_start.name, e.customer_end.name)
        if e.customer_end.end_customer == True:
            try:
                print(e.customer_end.customer_class.name)
                if e.customer_end.customer_class.name == "DIY":
                    border_coloring[e.customer_end.name] = "#DAC85A"
                    fill_coloring[e.customer_end.name] = "#FEF19C"

            except AttributeError:
                print(e.customer_end.name + " has no class.")
    
    ### Define the customer coloring
    nx.set_node_attributes(G, border_coloring, name="color")
    nx.set_node_attributes(G, fill_coloring, name="background")

    A = to_agraph(G)
    A.draw(completeName, prog='dot')

    template = loader.get_template('index.html')
    context = {
        'img': completeName,
        'routing': routings,
    }
    return HttpResponse(template.render(context, request))

编辑: 我在发布这篇文章后不久找到了答案。Graphviz需要将样式设置为“filled”。

以下解决了这个问题:

nx.set_node_attributes(G, {e.customer_end.name: "filled"}, name="style")

对于遇到这个问题的人,你需要将样式设置为filled,因为grpahviz需要。我花了几个小时才找到/理解这一点,在发布之后才明白。 nx.set_node_attributes(G, {e.customer_end.name: "filled"}, name="style") - Berik
你应该将评论发布为答案并接受它。 - Paul Brodersen
1个回答

0

感谢@Paul Brodersen; 下面的回答被采纳:

发布后不久,我找到了答案。Graphviz需要将样式设置为“filled”。

以下内容解决了这个问题:

nx.set_node_attributes(G, {e.customer_end.name: "filled"}, name="style")

此外,我更改了模型以根据分割添加颜色。现在循环如下所示:
nx.set_node_attributes(G, {e.customer_end.name: "filled"}, name="style")
border_coloring[e.customer_end.name] = e.customer_end.customer_segment.border_color
fill_coloring[e.customer_end.name] = e.customer_end.customer_segment.fill_color

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接