1

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'])

enter image description here

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) 

enter image description here

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?

Joel
  • 22,598
  • 6
  • 69
  • 93
  • Just to be clear to potential answerers - my question is "why are the nodes turning up black?" I know the standard networkx ways to only plot a subset of the nodes. – Joel Aug 09 '19 at 06:45

1 Answers1

2

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

Sparky05
  • 4,692
  • 1
  • 10
  • 27
  • As of May 19, the default value for the alpha parameter in networkx is None, but then @Joel may not be running the latest version of networkx. Upvoted regardless. – Paul Brodersen Aug 09 '19 at 09:36
  • The version of my test environment was 2.2. Just checked the code in 2.3 its still 1.0 and in the latest it's now None. Added this. – Sparky05 Aug 09 '19 at 10:06
  • Great - thanks to all. I was running 2.1. I may upgrade then. – Joel Aug 09 '19 at 13:50