Issue
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
Solution
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
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.