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
Exercise instructions
- Define a timedelta of 2 days using the
timedelta()
function and specifying an argument for thedays
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 isG.edges(data=True)
, and there are two conditions: ifd['date']
is>=
curr_day
and<
thancurr_day
+td
. - Append the number of edges (use the
len()
function to help you calculate this) ton_posts
. - Increment
curr_day
by the time deltatd
.
- Filter edges such that they are within the sliding time window. Use a list comprehension to do this, where the output expression is
- Make a plot of
n_posts
usingplt.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()