Networkx: How to visually group a set of nodes - python

I would like to visually group a set of nodes in networkx. Of course one could do so by changing node color, size etc. My preferred solution however should look something like the plot created with igraph below. Is this possible in networkx?

Related

How to find communities using graph tool "minimize_blockmodel_d"?

I would like to use graph tool subpackage : minimize_blockmodel_dl
https://graph-tool.skewed.de/static/doc/inference.html#graph_tool.inference.minimize_blockmodel_dl to find communities in a network.
I write code like this:
state = gt.minimize_blockmodel_dl(G). G is the graph containing nodes and edges. I wonder how can I do next to get the specific communities?

networkX.draw() not producing edges

I'm not sure why my network graph doesn't include edges.
I'm creating a network from a pandas dataframe that looks like the following:
I created the network as follows:
G = nx.from_pandas_edgelist(network_df,
edge_attr='weight',
source='Source',
target='Target',
create_using=nx.Graph())
but nx.draw(G) produces a graph without edges.
I tried using nx.DigGraph() but the result is the same.
Any help is greatly appreciated.
That central "blob" in your plot is a lot of nodes connected together which probably do have edges, but they are obscured by the dense mass of nodes. On the periphery there are a few nodes joined together by edges, but due to the plotting algorithm they pairs (or somewhat larger cluster) are again so close together that the nodes are obscured. The isolated nodes are isolated.
It's probably best to try another layout. The default is spring_layout. Here's another that will probably show it better:
pos = nx.circular_layout(G)
nx.draw(G, pos)
As a general rule, networkx was not designed for the purpose of graph visualization. So you may need to look at other tools like graphviz.

Visualising directed hyperedges in network graph Python

I'm trying to visualise a multi-edge directed graph, with hyper-edges. I've looked into python NetworkX, I'm starting with the directed graph, which can basically give me this:
But I would like to visualise my edges combined (since they are a kind of "bundle", with multiple source vertices, and one destination vertex).
Like this:
Any help would be appreciated.

networkx: giving a wrong direction for an edge

I am trying to read some dataframe to a networkx graph like this:
bipartGraph = nx.Graph()
bipartGraph.add_edge(618254814, 14337833)
bipartGraph.add_edge(618254882, 12087274)
When I display the edges using the bipartGraph.edges() function, I get the below:
[(14337833, 618254814), (618254882, 12087274)]
So, the direction of the first edge is reversed. I am trying to build a bipartite graph from a dataframe which I need to reuse to build an another graph. Is there any specific property of networkx I am missing?
At least in networkx 1.11 if your graph is undirected, an edge that is added as (u,v) may be returned as either (v,u) or (u,v). [I think this may have been changed in version 2.0]. This is because the underlying data structure is a dict, and there is no guarantee that a dict returns values in any particular order: Why items order in a dictionary changed in Python?.
If you really want a direction on your edge, you should make your graph a DiGraph.

How to draw same nodes with different edge colours correspond to two different graphs?

Hope my question has not asked before. I have two graphs, which nodes are the same in both of them but edges are different. I want to draw both of graphs in one plot. Which means I have the same nodes, but with two different edge colours. But it gives me two different graphs. How could I have them in one graph but with different edge colours?
If you are using Python, NetworkX and Matplotlib then you can do something like this, where you have two graphs with the same set of nodes and so you draw first the nodes and then the two set of edges in different colors.
import networkx as nx
G=nx.gnm_random_graph(10,20)
G2=nx.gnm_random_graph(10,20)
pos=nx.spring_layout(G)
nx.draw_networkx_nodes(G,pos,node_size=80)
nx.draw_networkx_edges(G,pos,edge_color='r')
nx.draw_networkx_edges(G2,pos,edge_color='b')
Take care with edges of different colors between the same endpoints, they will be indistinguishable.

Categories

Resources