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

Question:

Abstract

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_node('A')
G.add_node('B')
G.add_node('C')
G.add_node('D')
G.add_node('E')

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)

plt.figure()

The following error occurred

Traceback (most recent call last):
  File "C:/Program Files/Python36/transparency.py", line 19, in <module>
    nx.draw(G, alpha=node_alpha, with_labels = True)
  File "C:Users████AppDataRoamingPythonPython36site-packagesnetworkxdrawingnx_pylab.py", line 123, in draw
    draw_networkx(G, pos=pos, ax=ax, **kwds)
  File "C:Users████AppDataRoamingPythonPython36site-packagesnetworkxdrawingnx_pylab.py", line 336, in draw_networkx
    draw_networkx_edges(G, pos, arrows=arrows, **edge_kwds)
  File "C:Users████AppDataRoamingPythonPython36site-packagesnetworkxdrawingnx_pylab.py", line 684, in draw_networkx_edges
    alpha=alpha,
  File "C:Users████AppDataRoamingPythonPython36site-packagesmatplotlibcollections.py", line 1391, in __init__
    **kwargs)
  File "C:Users████AppDataRoamingPythonPython36site-packagesmatplotlibcbookdeprecation.py", line 411, in wrapper
    return func(*inner_args, **inner_kwargs)
  File "C:Users████AppDataRoamingPythonPython36site-packagesmatplotlibcollections.py", line 213, in __init__
    self.update(kwargs)
  File "C:Users████AppDataRoamingPythonPython36site-packagesmatplotlibartist.py", line 998, in update
    ret.append(func(v))
  File "C:Users████AppDataRoamingPythonPython36site-packagesmatplotlibcollections.py", line 834, in set_alpha
    super().set_alpha(alpha)
  File "C:Users████AppDataRoamingPythonPython36site-packagesmatplotlibartist.py", 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

Asked By: networkxxxx

||

Answers:

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

Answered By: SultanOrazbayev

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()
   G.add_edges_from(edges)
   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)
   plt.axis('off')
   plt.show()
   
edges = [("A","B"),("A","C"),("A","D"),("B","D"),("D","E")]
draw_graph(edges)

enter image description here

Answered By: networkxxxx