Get startedGet started for free

Plot number of posts being made over time

Let's recap how you can plot evolving graph statistics from the graph data. First off, you will use the graph data to quantify the number of edges that show up within a chunking time window of td days, which is 2 days in the exercise below.

The datetime variables dayone and lastday have been provided for you.

This exercise is part of the course

Intermediate Network Analysis in Python

View Course

Exercise instructions

  • Define a timedelta of 2 days using the timedelta() function and specifying an argument for the days parameter.
  • Inside the while loop:
    • Filter edges such that they are within the sliding time window. Use a list comprehension to do this, where the output expression is (u, v, d), the iterable is G.edges(data=True), and there are two conditions: if d['date'] is >= curr_day and < than curr_day + td.
    • Append the number of edges (use the len() function to help you calculate this) to n_posts.
    • Increment curr_day by the time delta td.
  • Make a plot of n_posts using plt.plot().

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Import necessary modules
from datetime import timedelta  
import matplotlib.pyplot as plt

# Define current day and timedelta of 2 days
curr_day = dayone
td = ____

# Initialize an empty list of posts by day
n_posts = []
while curr_day < lastday:
    if curr_day.day == 1:
        print(curr_day) 
    # Filter edges such that they are within the sliding time window: edges
    edges = [(____, ____, ____) for u, v, d in ____ if d['date'] >= ____ and d['date'] < ____ + ____]
    
    # Append number of edges to the n_posts list
    ____
    
    # Increment the curr_day by the time delta
    ____ += ____
    
# Create the plot
plt.plot(____)  
plt.xlabel('Days elapsed')
plt.ylabel('Number of posts')
plt.show()  
Edit and Run Code