Recognizing relationships between one item to another is difficult if you stare at a spreadsheet. How many nodes connect to another node and how frequent are their connections? Visualize the network and draw insights quickly with NetworkX.
In this example I am going to visually depict the 2019 Twins Schedule as a Network Diagram. Depicting the schedule visually provides a high level indication of what matchups occurred most frequently. Figure 1 below shows the output of the analysis with NetworkX. The size and the distance from the central node (marked as MIN in blue) indicate the number of times an opposing team played the Twins in 2019.

Based on the diagram it is clear the number games played reach a maximum for opposing teams in the American League Central (Detroit Tigers - DET, Chicago White Sox - CHW, Cleveland Indians - CLE, and Kansas City Royals - KCR) followed by teams in the broader American League and, in the outermost rung, matchups with teams in the National League. Logically, there is little surprise in how those matchups are coordinated. However, if you extend the use of network diagrams beyond Major League Baseball schedules to networks of unknown relation, I am sure you can quickly see the value in what the diagrams provide.
To help you get started with using the diagrams, I've included the code to generate figure 1 below. The code is commented heavily to help you understand how the components work. I've also posted the code and the supporting data to my git repository in the sub folder named 'data_networkx.'
Hopefully jumpstart your efforts to start visualizing network relationships. Please leave a comment or share if you find this article helpful!
#import load data functions stored in utility_functions.py (see git repository)
import utility_functions as util
import pandas as pd
#import networkx and matpolotlib
import networkx as nx
import matplotlib
#Load the data courtesy of https://www.baseball-reference.com/teams/MIN/2019-schedule-scores.shtml
data=util.loadCSVfile('Twins2019Schedule.csv')
#Instantiate the Network X graph object.
G=nx.Graph()
#Add a node for the Twins. I am going to make it blue and a size of 500 so it is distinguished. Color is added as a property of the node. You will see why this is important and in later steps to provide color to the image.
G.add_node('MIN',color='#0000FF',size=500)
#Create a weights lookup table to distinguish teams played more in the schedule. This will naturally depict in the graph by the distance from the central node. The more times the opposing team plays the Twins, the closer they will reside to MIN on the network diagram. To make even easier to see, the size of each entry will also be proportional to the number of games played.
opponent_dic = data['Opp'].value_counts().to_dict()
#Add nodes for the opposing teams. To make it easier to see I've added opposing teams as red and made the size of the nodes proportional to the number of games played against the Twins using the opponent_dic lookup table. In other words, a larger dot equates more games played.
for i in range(len(data)):
G.add_node(data.iloc[i,5], color='red',size=opponent_dic[data.iloc[i,5]]*100)
#Add the connections or 'edges' to the network diagram. Similar to the size of the node, the distance of the node from the center is proportional to the number of games played against the Twins. This is done by assigning a property 'weight' to each edge. The closer the node to the center, the more games played.
for i in range(len(data)):
G.add_edges_from([(data.iloc[i,3],data.iloc[i,5],{'weight': opponent_dic[data.iloc[i,5]]})])
#Create list of assigned node sizes and colors. This step iterates through each node object and creates a list showing the node colors and sizes. Given the index of the list matches the index of the nodes in the G object, when we render the image in the next step the color and sizes are assigned to the correct node.
node_color=[]
node_size=[]
for node in G.nodes():
color = G.nodes[node]['color']
node_color.append(color)
size = G.nodes[node]['size']
node_size.append(size)
#Draw the network digram assigning node_color and node_size using the lists established in the previous step and lavish in your new found network diagram greatness.
nx.draw(G, with_labels=True,node_color= node_color,node_size=node_size)