我正在使用Django,在我看来,我生成了一个各方之间的关系图.问题出在以下方面:

nx.set_node_attributes(G, fill_coloring, name="background")

但不幸的是,这并不管用.我在文档中找不到如何正确设置它.我想要实现的是基于与其相关的主数据来指导每个 node 的着色/格式化.

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

有了上面的设置,我可以设置边框 colored颜色 ,但不能设置内部 colored颜色 .我忽略了什么?

理想的情况是稍后通过向线条添加信息等方式,但现在我想用特定的 colored颜色 为 node 本身上色.目前,它只为边框上色.我忽略/误解了什么?

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需要将样式设置为"已填充".

下面解决了这个问题:

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

推荐答案

感谢@Paul Brodersen;以下是被接受的答案:

在发布了这篇文章后不久,我找到了答案.Graphviz需要将样式设置为"已填充".

下面解决了这个问题:

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

此外,我更改了模型,根据分割添加 colored颜色 .现在,for看起来如下所示:

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

Python相关问答推荐

如何在Power Query中按名称和时间总和进行分组

"如果发生特定错误,返回值

如何在矩阵上并行化简单循环?

使用多个性能指标执行循环特征消除

比较两个二元组列表,NP.isin

使用mySQL的SQlalchemy过滤重叠时间段

根据不同列的值在收件箱中移动数据

如何在虚拟Python环境中运行Python程序?

用NumPy优化a[i] = a[i-1]*b[i] + c[i]的迭代计算

driver. find_element无法通过class_name找到元素'""

为什么NumPy的向量化计算在将向量存储为类属性时较慢?'

什么是最好的方法来切割一个相框到一个面具的第一个实例?

用砂箱开发Web统计分析

在Python中使用if else或使用regex将二进制数据如111转换为001""

基于形状而非距离的两个numpy数组相似性

python panda ExcelWriter切换动态公式到数组公式

手动设置seborn/matplotlib散点图连续变量图例中显示的值

将标签移动到matplotlib饼图中楔形块的开始处

在代码执行后关闭ChromeDriver窗口

如何在Python 3.9.6和MacOS Sonoma 14.3.1下安装Pyregion