The representation of data that is collected over time helps to identify trends, patterns, and anomalies within the data. By plotting data points along a timeline, you can visually analyze the movement of values over time.
Exhibit 25.22 provides the Python code for the visualization of time series data. The example uses data stored in the file fb_comments_metrics.csv
, which contains information on the likes and shares of comments on Facebook posts throughout a day.
The Seaborn library is used to create the bar plots in this example. Built on top of Matplotlib, Seaborn provides a high-level interface for creating attractive and informative statistical graphics. For more information on Seaborn and to explore additional examples of its use in statistical analysis and visualization, refer to the section Seaborn Visualization in Python.
The code in Exhibit 25.22 is a continuation of our analysis of Facebook textual data, demonstrating how representing data collected over time can highlight trends, patterns, and anomalies.
The code analyzes data about likes and shares received by Facebook posts throughout a day. Here’s a breakdown of the steps in the analysis:
fb_comments_metrics.csv
.
In short, this code helps you see how the number of likes and shares for Facebook posts change throughout the day and across different days.
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
df = pd.read_csv(r"data/fb_comments_metrics.csv", encoding ="latin-1")
'''
`r` prefix preceding a string literal denotes a raw string literal.
Specifically, backslashes (\) are treated as literal characters, and are not
used for escaping special characters like they are in regular string literals.
'''
# set figure size and font size
sns.set(rc={'figure.figsize':(40,20)})
sns.set(font_scale=3)
# Separating time by Hour, Day, Month and Year for further analysis using datetime package
import datetime as dt
'''
A lambda function is a small anonymous function that can take any number of arguments, but can only have one expression.
e.g.:
lambda x: x.hour
paramter: x
expression: x.hour ... converts time in datetime format to hour and returns hour
'''
df['time'] = pd.to_datetime(df['created_time'])
df['hour'] = df['time'].apply(lambda x: x.hour)
df['month'] = df['time'].apply(lambda x: x.month)
df['day'] = df['time'].apply(lambda x: x.day)
df['year'] = df['time'].apply(lambda x: x.year)
df.head()
# set x labels
x_labels = df.hour
#create bar plot
sns.barplot(x=x_labels, y=df.likes, color="blue")
# display the plot
plt.show()
# only show x-axis labels for Jan 1 of every other year
# tick_positions = np.arange(10, len(x_labels), step=24)
#create bar plot
sns.barplot(x=x_labels, y=df.shares, color="green")
# display the plot
plt.show()
# Set the background color to white
sns.set_style(rc = {'axes.facecolor': 'white'})
#create bar plot
sns.barplot(x=df.day, y=df.shares, color="black")
# display the plot
plt.show()
Use the Search Bar to find content on MarketingMind.
Contact | Privacy Statement | Disclaimer: Opinions and views expressed on www.ashokcharan.com are the author’s personal views, and do not represent the official views of the National University of Singapore (NUS) or the NUS Business School | © Copyright 2013-2025 www.ashokcharan.com. All Rights Reserved.