Reputation: 23887
I would like to plot a completely invisible node in networkx. Basically it should be as if it weren't plotted. However, because of the structure of my code, I can't easily not plot it. It would be simpler if I could set the node_color
to be something like "Invisible"
.
Since networkx uses matplotlib.pyplot.scatter
to plot the nodes, I thought I would approach it like matplotlib. To do this there, just set the c
argument for a particular marker to be the string "None"
(see Plotting with a transparent marker but non-transparent edge).
Here's an example where 2 of the 5 markers are invisible:
plt.scatter(range(5), range(5), c = ["None", "black", 'red', "None", 'blue'])
Let's try that with networkx:
G = nx.erdos_renyi_graph(100,0.03)
nodelist = list(G.nodes())
colorlist = ["None" if node%2==0 else "red" for node in nodelist] #even nodes should be invisible
nx.draw_networkx(G, nodelist=nodelist, node_color=colorlist)
Notice that the even nodes are not invisible, they are black.
I don't understand, because when I look at the source code from networkx, the relevant line appears to be
node_collection = ax.scatter(xy[:, 0], xy[:, 1],
s=node_size,
c=node_color,
marker=node_shape,
cmap=cmap,
vmin=vmin,
vmax=vmax,
alpha=alpha,
linewidths=linewidths,
edgecolors=edgecolors,
label=label)
so colorlist
should be sent directly to scatter
without any alteration.
Can anyone explain why this is happening? Why are these nodes turning up black?
Upvotes: 1
Views: 1004
Reputation: 4892
It took some time to find the issue, but I was able to reproduce networkx
behaviour with matplotlib
:
pl.scatter(range(5), range(5), c = ["None", "black", 'red', "None", 'blue'], alpha=1.0)
Networkx
default parameter alpha=1.0
(which is different from alpha=None
of matplotlib
).
The following worked for me:
nx.draw_networkx(G, nodelist=nodelist, node_color=colorlist, alpha=None, with_labels=False)
Thanks to the comment of @Paul Brodersen: It looks like the different default values were resolved: Compare code of 2.3 vs. latest (2.4.xy).
Upvotes: 2