Assign color to NetworkX node based on column name

前端 未结 2 1604
Happy的楠姐
Happy的楠姐 2021-01-07 10:56

I am attempting to build a network graph using NetworkX and Bokeh. I am using the NetworkX from_pandas_edgelist function to add data for the graph. I would like

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-07 11:13

    After the old Edit:

    Can't give too much context as I am not super familiar with bokeh, but looks like you can use a similar approach to what I did initially just instead of passing the "color_map" do your draw function you have to stick your data in here graph_renderer.node_renderer.data_source.data['colors'] Anyway this seems to do the job, Good luck dude.

    relation = pd.DataFrame({
                    "company":["Google", "Google", "Amazon", "Amazon", "Amazon",
                                "Apple", "Apple", "Apple"],
                    "client":["AT&T", "Cisco", "Facebook", "Snap", "Microsoft",
                              "Intel", "IBM", "Visa"]})
    
    G=nx.from_pandas_edgelist(relation, 'company', 'client')
    colors = []
    
    for node in G:
        if node in relation["client"].values:
            colors.append("blue")
        else: colors.append("green")
    
    plot = Plot(plot_width=1000, plot_height=800,
                x_range=Range1d(-1.1, 1.1), y_range=Range1d(-1.1, 1.1))
    plot.title.text = "Company - Client Network"
    
    node_hover_tool = HoverTool(tooltips=[("Company Name", "@index")])
    plot.add_tools(node_hover_tool, BoxZoomTool(), ResetTool())
    
    graph_renderer = from_networkx(G, nx.spring_layout, scale=1, center=(0, 0))
    
    graph_renderer.node_renderer.data_source.data['colors'] = colors
    graph_renderer.node_renderer.glyph = Circle(size=20, fill_color='colors')
    
    graph_renderer.edge_renderer.glyph = MultiLine(line_color="red", line_alpha=0.8, line_width=1)
    plot.renderers.append(graph_renderer)
    
    output_file("boo.html")
    show(plot)
    
    
    

提交回复
热议问题