Can the transparency of the link be specified in the list?

Can the transparency of the link be specified in the list?

Problem Description:


I am trying to find a way to make the nodes transparent. In the simple case, the transparency of a node is simply specified by the "alpha" option of "nx.draw".
However, I thought I could specify the transparency with a list, just as I specified the color with a list, but failed. Do you know the reason for this?

import networkx as nx

G = nx.Graph()


G.add_edge('A', 'B')
G.add_edge('A', 'C')
G.add_edge('A', 'D')
G.add_edge('B', 'D')
G.add_edge('D', 'E')

node_alpha = [1, 0.8, 0.6, 0.4, 0.2]

nx.draw(G, alpha=node_alpha, with_labels = True)


The following error occurred

Traceback (most recent call last):
  File "C:/Program Files/Python36/", line 19, in <module>
    nx.draw(G, alpha=node_alpha, with_labels = True)
  File "C:Users████", line 123, in draw
    draw_networkx(G, pos=pos, ax=ax, **kwds)
  File "C:Users████", line 336, in draw_networkx
    draw_networkx_edges(G, pos, arrows=arrows, **edge_kwds)
  File "C:Users████", line 684, in draw_networkx_edges
  File "C:Users████", line 1391, in __init__
  File "C:Users████", line 411, in wrapper
    return func(*inner_args, **inner_kwargs)
  File "C:Users████", line 213, in __init__
  File "C:Users████", line 998, in update
  File "C:Users████", line 834, in set_alpha
  File "C:Users████", line 930, in set_alpha
    raise TypeError('alpha must be a float or None')
TypeError: alpha must be a float or None

We believe that the image will be as follows

enter image description here

Solution – 1

Edit: as answered by @Andrew (with a further reference to @AveragePythonEnjoyer) in this answer, it is possible to add a list of alpha values. This is not consistent with the documentation as of networkx version 2.8, however it should be fixed in future releases (see this PR).

Passing an array or a list of alpha values is supported by both nx.draw_networkx_nodes and nx.draw_networkx_edges. However, right now the doc string for draw_networkx_edges is not consistent with the allowed alpha types, so one can refer to the documentation for nx.draw_networkx_nodes:

alpha : float or array of floats (default=None)
The node transparency. This can be a single alpha value,
in which case it will be applied to all the nodes of color. Otherwise,
if it is an array, the elements of alpha will be applied to the colors
in order (cycling through alpha multiple times if necessary).

Solution – 2

As advised, I was able to solve the problem using nx.draw_networkx_edges. Thanks.

import networkx as nx
import sys
import matplotlib
import matplotlib.pyplot as plt

def draw_graph(edges):
   G = nx.DiGraph()
   pos = nx.spring_layout(G)
   alphas = [1, 0.8, 0.6, 0.4, 0.2]

   nx.draw_networkx_nodes(G,pos,node_color = "b",node_size = 300,alpha = alphas)
   nx.draw_networkx_edges(G,pos,arrows = False)
   nx.draw_networkx_labels(G,pos,font_size = 12)
edges = [("A","B"),("A","C"),("A","D"),("B","D"),("D","E")]

enter image description here

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.