24.07.2016 Views

www.allitebooks.com

Learning%20Data%20Mining%20with%20Python

Learning%20Data%20Mining%20with%20Python

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Discovering Accounts to Follow Using Graph Mining<br />

Next, iterate over each connected <strong>com</strong>ponent and add a subplot for each. The<br />

parameters to add_subplot are the number of rows of subplots, the number of<br />

columns, and the index of the subplot we are interested in. My visualization uses<br />

three columns, but you can try other values instead of three (just remember to<br />

change both values):<br />

for i, sub_graph in enumerate(sub_graphs):<br />

ax = fig.add_subplot(int(n_subgraphs / 3), 3, i)<br />

By default, pyplot shows plots with axis labels, which are meaningless in this<br />

context. For that reason, we turn labels off:<br />

ax.get_xaxis().set_visible(False)<br />

ax.get_yaxis().set_visible(False)<br />

Then we plot the nodes and edges (using the ax parameter to plot to the correct<br />

subplot). To do this, we also need to set up a layout first:<br />

pos = nx.spring_layout(G)<br />

nx.draw_networkx_nodes(G, pos, sub_graph.nodes(), ax=ax,<br />

node_size=500)<br />

nx.draw_networkx_edges(G, pos, sub_graph.edges(), ax=ax)<br />

The results visualize each connected <strong>com</strong>ponent, giving us a sense of the number<br />

of nodes in each and also how connected they are.<br />

[ 154 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!