Assignment Objective:

“Build and visualize a simple un-directed social network graph using Python. You’ll use nodes and edges to represent people and their connections.”

Python Code:

Walkthrough of Code:

For this assignment, I used Python along with the libraries NetworkX, pandas, and plotnine to create a social network visualization.

First, I imported the necessary libraries. NetworkX was used to create and manage the network graph, pandas helped organize the data into data frames, and plotnine was used to visualize the graph in a ggplot-style format.

Next, I created a random undirected graph using nx.gnp_random_graph(10, 0.5). This generated 10 nodes with a 50% probability of connections between them. I then assigned labels A–J to each node so the graph would be easier to read.

After that, I used a spring layout (nx.spring_layout) to position the nodes. This layout spreads the nodes out in a visually balanced way based on their connections.

I then converted the graph into two data frames:

  • nodes_df, which stores the x and y positions of each node along with its label
  • edges_df, which stores the starting and ending coordinates for each connection (edge)

These data frames were necessary because plotnine requires structured data to create visualizations.

Finally, I built the visualization using plotnine:

  • geom_segment() was used to draw the edges (lines between nodes)
  • geom_point() was used to plot the nodes
  • geom_text() added labels (A–J) above each node
  • theme_void() removed unnecessary background elements
  • ggtitle() added a title to the graph

Issue Encountered (and how I fixed it)

One issue I ran into was that the graph would not display when running the code in Python IDLE. Instead of showing the visualization, the output only displayed:

<ggplot: (640 x 480)>

This means the plot was successfully created, but Python IDLE did not automatically render or display it.

To resolve this, I modified the code to save the graph as a PNG file instead of trying to display it directly. I used:

network_plot.save("network_graph.png", width=6, height=4, dpi=300)

This allowed the visualization to be saved as an image file in my Downloads folder. I was then able to open the image and use it for submission.

Network Graph:

Reflection:

For this assignment, I created a social network graph in Python using NetworkX, pandas, and plotnine. What worked well was generating the random network and converting the node and edge data into a visualization with labeled connections. One issue I encountered was getting the graph to display in Python IDLE, but I resolved it by saving the graph as a PNG file instead. This method worked well overall because it allowed me to clearly visualize relationships between nodes in a network. While I initially had trouble displaying the plot, saving it as an image provided a simple and effective workaround. I would use this method again for basic network visualization tasks because it is flexible and produces clean, readable outputs. Overall, I would use this method again because it is a simple and effective way to visualize relationships between nodes in a network.

Posted in

Leave a Reply

Create your own website, free on WordPress.com.Create your own website.
Start free