Search code examples
python-3.xnetworkx

Change networkx graph transparency of each edge and node


When drawing graphs in Networkx, I am wondering if there is a way to specify alpha for each edge and node - similar to how the node_color or edge_color can be a list of colors. The goal is to be able to selectively highlight a subgraph of a larger graph.


Solution

  • If you use draw_networkx_nodes, you can pass in an array of floats to the alpha argument like you would do for node_color.

    Edit based on comment from @AveragePythonEnjoyer

    draw_networkx_edges does offer the same functionality, so you can pass a list of floats to the alpha argument the same length as the number of edges for that one as well!

    If the lists that you pass into the alpha argument are shorter than the number of nodes/edges, the values will cycle through the list then restart at the beginning until the number of nodes/edges is covered. If the list is longer than the number of nodes/edges, only the number of values corresponding to the number of nodes/edges will be used.

    Both draw_networkx_nodes and draw_networkx_edges require you to pass in a pos argument for node positions, so those must be defined in advance.