1

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\████\AppData\Roaming\Python\Python36\site-packages\networkx\drawing\nx_pylab.py", line 123, in draw
    draw_networkx(G, pos=pos, ax=ax, **kwds)
  File "C:\Users\████\AppData\Roaming\Python\Python36\site-packages\networkx\drawing\nx_pylab.py", line 336, in draw_networkx
    draw_networkx_edges(G, pos, arrows=arrows, **edge_kwds)
  File "C:\Users\████\AppData\Roaming\Python\Python36\site-packages\networkx\drawing\nx_pylab.py", line 684, in draw_networkx_edges
    alpha=alpha,
  File "C:\Users\████\AppData\Roaming\Python\Python36\site-packages\matplotlib\collections.py", line 1391, in __init__
    **kwargs)
  File "C:\Users\████\AppData\Roaming\Python\Python36\site-packages\matplotlib\cbook\deprecation.py", line 411, in wrapper
    return func(*inner_args, **inner_kwargs)
  File "C:\Users\████\AppData\Roaming\Python\Python36\site-packages\matplotlib\collections.py", line 213, in __init__
    self.update(kwargs)
  File "C:\Users\████\AppData\Roaming\Python\Python36\site-packages\matplotlib\artist.py", line 998, in update
    ret.append(func(v))
  File "C:\Users\████\AppData\Roaming\Python\Python36\site-packages\matplotlib\collections.py", line 834, in set_alpha
    super().set_alpha(alpha)
  File "C:\Users\████\AppData\Roaming\Python\Python36\site-packages\matplotlib\artist.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

2 Answers 2

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

3
  • 1
    nx.draw_networkx_edges actually does support an array of floats! It's not listed in the documentation, but it was pointed out to me in this answer. You can't use an array in nx.draw since NetworkX won't know whether you're referring to the nodes or the edges (and since the number of nodes can be different than the number of edges), but if you draw the nodes and edges separately, you can pass arrays to the alpha value for each.
    – Andrew
    Dec 5, 2022 at 5:27
  • 1
    Interesting, thank you for pointing this out, seems docs need updating. Dec 5, 2022 at 6:41
  • 1
    I updated the answer and submitted a PR to fix the docs. Dec 5, 2022 at 12:28
1

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

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.