Search code examples
pythonbokehnetworkx

Assign color to NetworkX node based on column name


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 to color the node of the graph based on the column in the initial data input.

The relation DataFrame is as follows:

company   client

Google    AT&T
Google    Cisco       
Amazon    Facebook
Amazon    Snap
Amazon    Microsoft
Apple     Intel
Apple     IBM
Apple     Visa

The above snippet is only a portion of the DataFrame.

I would like all of the nodes from company to return in a different color to client.

The code below produces a network graph where all nodes are the same color.

G=nx.from_pandas_edgelist(relation, 'company', 'client')

# Show with Bokeh
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.glyph = Circle(size=20)

graph_renderer.edge_renderer.glyph = MultiLine(line_color="red", line_alpha=0.8, line_width=1)
plot.renderers.append(graph_renderer)

output_file("interactive_graphs.html")
show(plot)

Any assistance anyone could provide would be greatly appreciated.


Solution

  • 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)