With our data in place and our network graph populated, we can call on methods from NetworkX to calculate measures of centrality and analyze the network. The results will be stored in a <a href="https://readmedium.com/pandas-i-read-csv-head-tail-info-and-describe-43b9b2736490">Pandas dataframe</a>.</p>
<figure id="352b">
<div>
<div>
<iframe class="gist-iframe" src="/gist/christine-egan42/c332b2fabfb7645c370ebc6621009053.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="aefc">The output should look something like this:</p>
<figure id="ca48">
<div>
<div>
<iframe class="gist-iframe" src="/gist/christine-egan42/2cc9bc5ef687508fc7fb2afdab985bc7.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="bec1">This is significantly more interesting than our results in the last episode, which looked like this:</p>
<figure id="5bd8">
<div>
<div>
<iframe class="gist-iframe" src="/gist/christine-egan42/f0ea21a72e2eb3f2491b8e673f91a091.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="4027">Before we move on to build a visualization with Matplotlib of this NetworkX graph, let’s digest our observations about the table that we generated. To do this, we will describe each centrality measure, then make an interpretation based on the results from our data.</p><h2 id="7420">1. Betweenness Centrality</h2><p id="dcf8"><i>(Quantifies how often a node acts as a bridge or intermediary in the flow of information or interactions between other nodes.)</i></p><p id="93d7">When our network contained only two bands (Smashing Pumpkins and Zwan), Billy Corgan and Jimmy Chamberlain ranked the highest in betweenness centrality. Since Tool and A Perfect Circle share a common frontman, we see that Maynard James Keenan usurps some of the betweenness, lowering the betweenness between Billy and Jimmy. However, the band member with the new highest betweenness centrality is <b>James Iha</b>. This suggests that James Iha is the node that links these <i>subgroups</i> together. Using domain knowledge, this checks out. As a member of both Smashing Pumpkins and A Perfect Circle, he is the common link between Billy Corgan’s bandmates and Maynard James Keenan’s bandmates.</p><h2 id="0e65">2. Degree Centrality</h2><p id="2d67"><i>(Measures the number of direct connections (edges) that a node (individual) has in the network.)</i></p><p id="8845"><b>James Iha</b> has the highest degree centrality score (0.782609), indicating that he has the most direct connections with other individuals in the network. This is because unlike Zwan and Tool, Smashing Pumpkins and A Perfect Circle have had a rotating cast of members that all share a connection with James Iha.</p><figure id="5dd8"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*2ReYpZlxoxasBeZJ4DGhSw.jpeg"><figcaption>James Iha by Tiffany Bauer, <a href="http://creativecommons.org/licenses/by-sa/3.0/">CC BY-SA 3.0</a>, via <a href="https://commons.wikimedia.org/wiki/File:James_Iha.jpg">Wikimedia Commons</a></figcaption></figure><h2 id="ccc0">3. Closeness Centrality</h2><p id="d1c9">Closeness centrality measures how close a node is to all other nodes in the network, taking into account the shortest paths. In this case, James Iha has the highest closeness centrality score (0.821429), indicating that he can reach other individuals in the network quickly and efficiently.</p><p id="d6ee">These centrality measures provide a way to identify key individuals who have significant connections, act as bridges, and have efficient access to other individuals in the network.</p><p id="7d98">Now that we have calculated the measures of centrality, we can create a NetworkX graph with Matplotlib. This time, we will make the graph more engaging by adding colors, labels, titles, and more.</p><h1 id="10cd">Visualize NetworkX Graphs with Matplotlib</h1><p id="e8d8">The table we created in the previous section provides important information about influential musicians in the network. To make these results more intuitive, we can create a visualization of the graph with <a href="https://matplotlib.org/">Matplotlib</a> and NetworkX. In previous installments, we have kept our graphs relatively simple. This time, we will use some advanced functionality to make our visualization especially engaging.</p><p id="94de">As a reminder, this is what our previous network graph looked like:</p><figure id="fc9d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*NGUE9roWgqVkv7N_FDBDnw.png"><figcaption>NetworkX graph for Smashing Pumpkins and Zwan from Closeness and Communities</figcaption></figure><h2 id="85c3">1. Add Color with Matplotlib</h2><p id="e35b">If you check the<a href="https://networkx.org/documentation/stable/reference/generated/networkx.drawing.nx_pylab.draw_networkx_nodes.html"> NetworkX documentation</a> for the method <code>.draw_networkx_nodes()</code>, you will notice an argument for <code>node_color</code> that accepts a <code>color</code> or <code>array of colors</code>. In our case, we want to go with the <code>array of colors</code> so that we can assign a color to each node based on which band the member is in.</p><p id="6d9f">Before we get into those details, let’s select some colors. 🎨</p><p id="ef8d">When creating plots with Matplotlib, you can use <a href="https://matplotlib.org/stable/tutorials/colors/colors.html">named colors or hex codes</a>. For more information about color choices and the many techniques available, check out the <a href="https://matplotlib.org/stable/tutorials/colors/colors.html#comparison-between-x11-css4-and-xkcd-colors">documentation</a> (they even provide code so that you can generate this chart yourself 🤩).</p><figure id="aa5a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*nv0v66apK4M_CYqXDsQ-Gg.png"><figcaption><a href="https://matplotlib.org/stable/gallery/color/named_colors.html">Named Colors to use with Matplotlib</a></figcaption></figure><p id="0dd5">In our case, the goal is to create a dictionary of band names that are mapped to the colors we selected. Then, we want to loop over the nodes and add the selected color for that node to a list. Since we want to emphasize connections between the communities, we will choose a special color for members that appear in more than one band.</p><p id="6e53">To accomplish this, we use a <code>for-loop</code> to iterate over every node in the network. Then, we use a series of <code>if-else</code> statements to assign the special color to any member that appears more than once in <code>flat_communities</code>. If the node for that member only appears once, a color is assigned based on which band the member belongs to.</p>
<figure id="5d40">
<div>
<div>
<iframe class="gist-iframe" src="/gist/christine-egan42/abd8cb99ea8f6b506ad824fc8c41cca0.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="9b60">Using the code block above, we generated a list of twenty-four colors. This directly maps to the list of twenty-four nodes in the network.</p>
<figure id="22e1">
<div>
<div>
<iframe class="gist-iframe" src="/gist/christine-egan42/c9c9c13d28ef4e81302d63399fec932d.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="7053">This output will be the input for <code>.node_colors()</code> when we call on <code>.draw_networkx_nodes()</code> to start populating our graph’s visualization.</p><h2 id="feea">2. Plotting Figures and Calculating Node Position</h2><p id="35f5">To begin drawing the visualization, we call on Matplotlib’s <code>.figure()</code> and set <code>figsize</code> to 20 x 12 to provide room for our network graph.</p><p id="30dd">Then, we use NetworkX’s <code>.spring_layout()</code> to calculate the position of each node in the network. This will allow us to plot the nodes on the visualization.</p>
<figure id="cf8e">
<div>
<div>
<iframe class="gist-iframe" src="/gist/christine-egan42/03214da2b126d56ee9bab5b7dbbcf501.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><h2 id="bf1e">3. Draw Nodes and Edges</h2><p id="7a6b">Next, we will use <code>.draw_networkx_nodes()</code> and <code>.draw_networkx_edges()</code> to graph each node and their connection. Let’s discuss the arguments for each.</p><p id="2b2a"><code><b>.draw_networkx_nodes()</b></code></p><ul><li><code>G</cod
Options
e>: the NetworkX graph object (<code>G</code>)</li><li><code>pos</code>: the position of each of the nodes we generated above (<code>pos</code>)</li><li><code>node_color</code>: the list of colors we generated (<code>node_colors</code>)</li><li><code>node_size</code>: how big should each node be?</li><li><code>alpha</code>: how transparent should each node be?</li></ul><p id="fe15"><code><b>.draw_networkx_edges()</b></code></p><ul><li><code>G</code>: the NetworkX graph object (<code>G</code>)</li><li><code>pos</code>: the position of each of the nodes we generated above (<code>pos</code>)</li><li><code>edge_color</code>: what color should the edges be?</li><li><code>alpha</code>: how transparent should each edge be?</li></ul><p id="d338">This is what it looks like in Python</p>
<figure id="14ed">
<div>
<div>
<iframe class="gist-iframe" src="/gist/christine-egan42/cadd5190fed81af695a840f0e1ed442a.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><h2 id="3bbc">4. Add Labels to NetworkX Graphs</h2><p id="f6cc">To add labels, we can use the NetworkX method <code>.draw_networkx_labels</code> .</p><p id="eb47">Let’s go through each parameter and understand their significance. We will skip the ones we have recently reviewed, like <code>G</code> and <code>pos</code>. Then, I’ll show you how to put it all together.</p><ol><li><code>font_size</code>: This parameter sets the font size for the node labels. It specifies how large or small the text representing the node label should be.</li><li><code>font_color</code>: This parameter sets the color of the node labels. You can specify the color using various formats, such as named colors (‘red’, ‘blue’, etc.) or hexadecimal values (‘#RRGGBB’) .</li><li><code>verticalalignment</code>: This parameter controls the vertical alignment of the node labels with respect to the nodes. For example, setting it to `’top’` aligns the labels above the nodes.</li><li><code>bbox</code>: This parameter is a dictionary containing properties for the bounding box (bbox) of the node labels. It is typically used to define a background box or a rectangle around the labels to improve visibility and readability.</li><li><code>labels</code>: This is an optional parameter that allows you to specify a dictionary with custom node labels. The dictionary’s keys should be node IDs (usually integers), and the values should be the corresponding labels you want to assign to each node. If this parameter is not provided, the default node labels will be used (usually the node IDs).</li></ol><p id="75d3">This is what the code will look like:</p>
<figure id="a29e">
<div>
<div>
<iframe class="gist-iframe" src="/gist/christine-egan42/cadd5190fed81af695a840f0e1ed442a.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><h2 id="6cb7">5. Add a Legend to Matplotlib Visualizations</h2><p id="b185">Finally, we want to add a legend to provide context for the viewer. To accomplish this, we need to create three lists (<code>legend labels</code>, <code>legend colors</code>, and <code>legend markers</code>), then provide those lists as arguments to <code>plt.legend()</code> . We will also use the optional args <code>loc</code>, <code>fontsize</code>, <code>frameon</code>, <code>borderpad</code>, and <code>borderaxespad</code> , to stylize the legend.</p><p id="106b">Let’s review the purpose of each optional arg we selected.</p><ol><li><code>loc</code> : Where should the legend be located on the screen?</li><li><code>fontsize</code>: Determines the text size of the legend.</li><li><code>frameon</code>: Adds a border around the legend.</li><li><code>borderpad</code>: Adds a little pad space between the border and the text.</li><li><code>borderaxespad</code>: The pad between the axes and legend border, in font-size units.</li></ol><p id="9ad1">These are just a few of the many optional args you can use to stylize your matplotlib legends. Check out the <a href="https://matplotlib.org/stable/api/legend_api.html">documentation</a> to review all of them, as well as the possible values that you can provide.</p><p id="2df5">For <code>legend labels</code> , we simply provide the name of each band. For <code>legend_colors</code> we refer to the dictionary we created, <code>color_palette</code> , and use the key values to define the color of each band. Next, <code>legend_markers</code> positions the markers in a straight vertical line, and define their size and shape.</p><p id="3147">Finally, we pull it all together with <code>plt.legend()</code> . This is what it looks like:</p>
<figure id="615e">
<div>
<div>
<iframe class="gist-iframe" src="/gist/christine-egan42/a459e949bca99aec9f489ef0179c6528.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><h2 id="4b18">6. Adding Finishing Touches</h2><p id="dc45">To put a bow on our NetworkX graph, we can add a title by calling plt.title(), then stylizing the title with <code>fontsize</code>, <code>fontweight</code>, and <code>pad.</code></p><p id="8ddf">By using the <code>off</code> parameter with plt.axis(), we can remove the border around the network graph, allowing for more even spacing and readability.</p><p id="2fb4">Calling <code>plt.tight_layout() </code>automatically adjusts the placement of graph elements, for even more improved spacing and readability.</p><p id="5d11">Finally, we simply call <code>plt.show()</code> to display our visualization!</p>
<figure id="2180">
<div>
<div>
<iframe class="gist-iframe" src="/gist/christine-egan42/b831ce7e1c651bd73c76ef47edd15391.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="5143">This is the final product:</p><figure id="3971"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*r7oJmZ7eg_AOA6k26mz4AQ.png"><figcaption>NetworkX graph created with Matplotlib “Musicians in Billy Corgan’s Network”</figcaption></figure><h1 id="c01a">Interpreting NetworkX Graphs</h1><p id="a5a7">In this tutorial, we used Billy Corgan’s network to demonstrate foundational concepts in social network analysis, by building on the network we built in Part 1, Part 2, and Part 3. This time, we made the network more complex by adding two additional bands, Tool, and A Perfect Circle. This allowed us to observe the effect of additional individuals (nodes) in the network, and how that impacts measures of centrality.</p><p id="ca50">What we discovered is that James Iha is an important intermediary between the community of musicians associated with Billy Corgan (Smashing Pumpkins and Zwan) and Maynard James Keenan (Tool and A Perfect Circle). This result is interesting because it suggests that lead singers are not necessarily the most influential players in the larger network. If we consider how this technique can be used at scale with large networks, we can appreciate the utility of NetworkX graphs and how they can help us quickly identify important nodes in the network. Then, we can use metrics, like degree centrality, betweenness centrality, and closeness centrality to highlight the nuances in our findings. Finally, we imbue our interpretation with domain knowledge to reach an explainable result.</p><p id="5d0f">So, if you are a subject matter expert when it comes to alternative music, what do you think of these results? If you noticed any interesting changes in the network after the addition of Tool and A Perfect Circle, be sure to mention it in <b>Responses </b>💬!</p><p id="858f"><b><i>If you would like the <a href="https://github.com/christine-egan42/sna-billy-corgan/blob/main/SNA4-Graphing-Billy-Corgans-Community.ipynb">annotated notebook</a> for this tutorial, visit my<a href="https://github.com/christine-egan42"> GitHub</a>! Give it a ⭐️ for easy reference. </i></b>👩🏻💻 <a href="https://christine-egan42.github.io/">Christine Egan</a> | <a href="https://christineegan42.medium.com/">medium</a> | <a href="https://github.com/christine-egan42">github</a> | <a href="https://www.linkedin.com/in/christineegan42/">linkedin</a></p><div id="eeb0" class="link-block">
<a href="https://christineegan42.medium.com/membership">
<div>
<div>
<h2>Join Medium with my referral link - Christine Egan</h2>
<div><h3>As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…</h3></div>
<div><p>christineegan42.medium.com</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*_HCtUffZMJOJ488l)"></div>
</div>
</div>
</a>
</div></article></body>
Graphing Billy Corgan’s Network: Analyzing and Mapping Social Relationships with Python’s NetworkX Library — Part 4
Continue learning how to conduct social network analysis with NetworkX and Python
At the beginning of our investigation into Billy Corgan’s sphere of influence, we introduced social network analysis and basic concepts like nodes and edges. In Part 2, we expanded our understanding of social network analysis by graphing the relationships between the members of the bands Smashing Pumpkins and Zwan. Then, we examined metrics like degree centrality and betweenness centrality to investigate the relationships between the members of the different bands. At the same time, we discussed how domain knowledge helps to inform our understanding of the results.
In Part 3, we introduced a third centrality measure, closeness centrality. We also began a discussion on the concept of communities and subgroups and demonstrated different community graphs and how we might use closeness centrality to inform our interpretation. Using the network of musicians that were members of the bands Zwan and Smashing Pumpkins, we made inferences about the relationships between the members.
This time around, we will make our results more interesting by expanding the network and adding additional bands. At the same time, we will expand our understanding of measures of centrality and the concept of community while refining our Matplotlib skills to make your NetworkX graphs even more engaging.
In previous installments, we covered three essential metrics in social network analysis: degree centrality, betweenness centrality, and closeness centrality. We also discussed the concept of communities and described how that framework can be applied to understand network dynamics among the communities/bands that comprise Billy Corgan’s network.
Remember this from last time? It’s a NetworkX graph we created with Matplotlib.
While even a small group of musicians can exhibit interesting network dynamics, the lack of complexity in the network made our results less interesting and did not provide insights beyond what we might glean from common sense. This time around, we will add more bands and musicians to the network to observe how communities emerge the connections between the musicians influence network dynamics.
Another Sphere of Influence
Another popular rock band that emerged during the 1990s is Tool — known for their unique and progressive approach to rock music. Tool often combines elements of alternative metal, progressive rock, and art rock, resulting in a distinctive and experimental sound. Their songs often incorporate metaphorical and allegorical storytelling that leaves room for interpretation.
From a data science perspective, Tool is an interesting choice, because they also incorporate mathematical themes in various ways within their music. While they don’t explicitly utilize mathematical formulas or equations (in other words — we can’t exactly call them a math rock band), they often employ complex time signatures and rhythmic patterns that can be seen as mathematically inspired. One of the better-known examples of mathematical themes in Tool’s music is the Fibonacci Sequence and the Golden Ratio in the song Lateralus.
Fans will note that Maynard James Keenan, the lead singer of Tool and A Perfect Circle, has other projects that we will save for a future installment. But what does any of this have to do with Billy Corgan? Let’s use measures of centrality to investigate.
Before you start…
Do you have basic knowledge of Python? If not, start here.
Are you familiar with basic concepts in social network analysis, like nodes and edges? If not, start here.
Are you comfortable with concepts in social network analysis, like degree centrality and betweenness centrality? If not, start here! How about communities? If not, start here.
In the code cells below we will expand our network by adding Tool and A Perfect Circle toour network of 1990s musicians. Then, we will use measures of centrality to analyze the network.
1. Constructing Communities
In the code cell below, we import NetworkX and Matplotlib. Just like we did in Part 3, we create a graph object with NetworkX and make a list of all the band members in each band. Then, we combine all of the bands and assign them to the variable communities .
Note: Current and former members are included in the lists of members.
Next, we are going to use a for-loop to iterate over the list of band members. Then, we use .add_nodes_from() to add each band member to the network graph.
To connect members of the same community (i.e., band) , we input a list comprehension that contains each member in the band and eliminates any doubles (e.g., (Billy Corgan, Billy Corgan) ) into the .add_edges_from() .
2. Calculate Measures of Centrality
With our data in place and our network graph populated, we can call on methods from NetworkX to calculate measures of centrality and analyze the network. The results will be stored in a Pandas dataframe.
The output should look something like this:
This is significantly more interesting than our results in the last episode, which looked like this:
Before we move on to build a visualization with Matplotlib of this NetworkX graph, let’s digest our observations about the table that we generated. To do this, we will describe each centrality measure, then make an interpretation based on the results from our data.
1. Betweenness Centrality
(Quantifies how often a node acts as a bridge or intermediary in the flow of information or interactions between other nodes.)
When our network contained only two bands (Smashing Pumpkins and Zwan), Billy Corgan and Jimmy Chamberlain ranked the highest in betweenness centrality. Since Tool and A Perfect Circle share a common frontman, we see that Maynard James Keenan usurps some of the betweenness, lowering the betweenness between Billy and Jimmy. However, the band member with the new highest betweenness centrality is James Iha. This suggests that James Iha is the node that links these subgroups together. Using domain knowledge, this checks out. As a member of both Smashing Pumpkins and A Perfect Circle, he is the common link between Billy Corgan’s bandmates and Maynard James Keenan’s bandmates.
2. Degree Centrality
(Measures the number of direct connections (edges) that a node (individual) has in the network.)
James Iha has the highest degree centrality score (0.782609), indicating that he has the most direct connections with other individuals in the network. This is because unlike Zwan and Tool, Smashing Pumpkins and A Perfect Circle have had a rotating cast of members that all share a connection with James Iha.
Closeness centrality measures how close a node is to all other nodes in the network, taking into account the shortest paths. In this case, James Iha has the highest closeness centrality score (0.821429), indicating that he can reach other individuals in the network quickly and efficiently.
These centrality measures provide a way to identify key individuals who have significant connections, act as bridges, and have efficient access to other individuals in the network.
Now that we have calculated the measures of centrality, we can create a NetworkX graph with Matplotlib. This time, we will make the graph more engaging by adding colors, labels, titles, and more.
Visualize NetworkX Graphs with Matplotlib
The table we created in the previous section provides important information about influential musicians in the network. To make these results more intuitive, we can create a visualization of the graph with Matplotlib and NetworkX. In previous installments, we have kept our graphs relatively simple. This time, we will use some advanced functionality to make our visualization especially engaging.
As a reminder, this is what our previous network graph looked like:
NetworkX graph for Smashing Pumpkins and Zwan from Closeness and Communities
1. Add Color with Matplotlib
If you check the NetworkX documentation for the method .draw_networkx_nodes(), you will notice an argument for node_color that accepts a color or array of colors. In our case, we want to go with the array of colors so that we can assign a color to each node based on which band the member is in.
Before we get into those details, let’s select some colors. 🎨
When creating plots with Matplotlib, you can use named colors or hex codes. For more information about color choices and the many techniques available, check out the documentation (they even provide code so that you can generate this chart yourself 🤩).
In our case, the goal is to create a dictionary of band names that are mapped to the colors we selected. Then, we want to loop over the nodes and add the selected color for that node to a list. Since we want to emphasize connections between the communities, we will choose a special color for members that appear in more than one band.
To accomplish this, we use a for-loop to iterate over every node in the network. Then, we use a series of if-else statements to assign the special color to any member that appears more than once in flat_communities. If the node for that member only appears once, a color is assigned based on which band the member belongs to.
Using the code block above, we generated a list of twenty-four colors. This directly maps to the list of twenty-four nodes in the network.
This output will be the input for .node_colors() when we call on .draw_networkx_nodes() to start populating our graph’s visualization.
2. Plotting Figures and Calculating Node Position
To begin drawing the visualization, we call on Matplotlib’s .figure() and set figsize to 20 x 12 to provide room for our network graph.
Then, we use NetworkX’s .spring_layout() to calculate the position of each node in the network. This will allow us to plot the nodes on the visualization.
3. Draw Nodes and Edges
Next, we will use .draw_networkx_nodes() and .draw_networkx_edges() to graph each node and their connection. Let’s discuss the arguments for each.
.draw_networkx_nodes()
G: the NetworkX graph object (G)
pos: the position of each of the nodes we generated above (pos)
node_color: the list of colors we generated (node_colors)
node_size: how big should each node be?
alpha: how transparent should each node be?
.draw_networkx_edges()
G: the NetworkX graph object (G)
pos: the position of each of the nodes we generated above (pos)
edge_color: what color should the edges be?
alpha: how transparent should each edge be?
This is what it looks like in Python
4. Add Labels to NetworkX Graphs
To add labels, we can use the NetworkX method .draw_networkx_labels .
Let’s go through each parameter and understand their significance. We will skip the ones we have recently reviewed, like G and pos. Then, I’ll show you how to put it all together.
font_size: This parameter sets the font size for the node labels. It specifies how large or small the text representing the node label should be.
font_color: This parameter sets the color of the node labels. You can specify the color using various formats, such as named colors (‘red’, ‘blue’, etc.) or hexadecimal values (‘#RRGGBB’) .
verticalalignment: This parameter controls the vertical alignment of the node labels with respect to the nodes. For example, setting it to `’top’` aligns the labels above the nodes.
bbox: This parameter is a dictionary containing properties for the bounding box (bbox) of the node labels. It is typically used to define a background box or a rectangle around the labels to improve visibility and readability.
labels: This is an optional parameter that allows you to specify a dictionary with custom node labels. The dictionary’s keys should be node IDs (usually integers), and the values should be the corresponding labels you want to assign to each node. If this parameter is not provided, the default node labels will be used (usually the node IDs).
This is what the code will look like:
5. Add a Legend to Matplotlib Visualizations
Finally, we want to add a legend to provide context for the viewer. To accomplish this, we need to create three lists (legend labels, legend colors, and legend markers), then provide those lists as arguments to plt.legend() . We will also use the optional args loc, fontsize, frameon, borderpad, and borderaxespad , to stylize the legend.
Let’s review the purpose of each optional arg we selected.
loc : Where should the legend be located on the screen?
fontsize: Determines the text size of the legend.
frameon: Adds a border around the legend.
borderpad: Adds a little pad space between the border and the text.
borderaxespad: The pad between the axes and legend border, in font-size units.
These are just a few of the many optional args you can use to stylize your matplotlib legends. Check out the documentation to review all of them, as well as the possible values that you can provide.
For legend labels , we simply provide the name of each band. For legend_colors we refer to the dictionary we created, color_palette , and use the key values to define the color of each band. Next, legend_markers positions the markers in a straight vertical line, and define their size and shape.
Finally, we pull it all together with plt.legend() . This is what it looks like:
6. Adding Finishing Touches
To put a bow on our NetworkX graph, we can add a title by calling plt.title(), then stylizing the title with fontsize, fontweight, and pad.
By using the off parameter with plt.axis(), we can remove the border around the network graph, allowing for more even spacing and readability.
Calling plt.tight_layout() automatically adjusts the placement of graph elements, for even more improved spacing and readability.
Finally, we simply call plt.show() to display our visualization!
This is the final product:
NetworkX graph created with Matplotlib “Musicians in Billy Corgan’s Network”
Interpreting NetworkX Graphs
In this tutorial, we used Billy Corgan’s network to demonstrate foundational concepts in social network analysis, by building on the network we built in Part 1, Part 2, and Part 3. This time, we made the network more complex by adding two additional bands, Tool, and A Perfect Circle. This allowed us to observe the effect of additional individuals (nodes) in the network, and how that impacts measures of centrality.
What we discovered is that James Iha is an important intermediary between the community of musicians associated with Billy Corgan (Smashing Pumpkins and Zwan) and Maynard James Keenan (Tool and A Perfect Circle). This result is interesting because it suggests that lead singers are not necessarily the most influential players in the larger network. If we consider how this technique can be used at scale with large networks, we can appreciate the utility of NetworkX graphs and how they can help us quickly identify important nodes in the network. Then, we can use metrics, like degree centrality, betweenness centrality, and closeness centrality to highlight the nuances in our findings. Finally, we imbue our interpretation with domain knowledge to reach an explainable result.
So, if you are a subject matter expert when it comes to alternative music, what do you think of these results? If you noticed any interesting changes in the network after the addition of Tool and A Perfect Circle, be sure to mention it in Responses 💬!