-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathyearinsearchtrends.py
145 lines (125 loc) · 4.21 KB
/
yearinsearchtrends.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import datetime
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from pytrends.request import TrendReq
def get_interest_over_time(
keywords: list,
timeframe: str,
chunksize: int = 1,
cat: int = 0,
geo: str = "",
normalize: bool = True,
) -> pd.DataFrame:
"""Requests weekly search trend data for a list of keywords and writes results into a dataframe.
Args:
keywords (list): List of search keywords.
timeframe (str): Requested timeframe in the format 'yyyy-mm-dd yyyy-mm-dd'.
chunksize (int, optional): Group request into junks to gracefully call the API.
Must be <= 5. Defaults to 1.
cat (int, optional): Search category according to
https://github.com/pat310/google-trends-api/wiki/Google-Trends-Categories. Defaults to 0.
geo (str, optional): Geocode to localize search trends.
See https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes for codes. Defaults to "".
normalize (bool, optional): Whether to normalize individual keywords in the same chunk.
If False, the trends for keywords in the same chunk are relative to each other. Defaults to True.
Returns:
pd.DataFrame: Dataframe holding the weekly search trends over time for the provided keywords.
"""
def chunk_generator(lst, n):
for i in range(0, len(lst), n):
yield lst[i : i + n]
chunks = chunk_generator(keywords, chunksize)
df = pd.DataFrame(columns=["date"])
pytrends = TrendReq(tz=360)
for chunk in chunks:
pytrends.build_payload(chunk, cat=cat, geo=geo, timeframe=timeframe)
df_chunk = pytrends.interest_over_time()
if "isPartial" in df_chunk.columns:
df_chunk = df_chunk.drop("isPartial", axis=1)
df = pd.merge(df, df_chunk, how="right", on="date")
df = df.set_index("date")
if chunksize > 1 and normalize:
df = df * (100 / df.max())
df_longform = df.reset_index().melt(
id_vars=["date"], var_name="Keyword", value_name="Interest"
)
return df_longform
def draw_ridgeplot(
df_longform,
aspect=15,
height=0.55,
palette="viridis",
hspace=-0.35,
reflinewidth=2,
plotlinewidth=2.5,
font="DejaVu Sans",
indent=70,
):
"""
Plots overlapping ridgeplot heavily inspired by
https://seaborn.pydata.org/examples/kde_ridgeplot.html
"""
sns.set_theme(style="white", rc={"axes.facecolor": (0, 0, 0, 0)})
plt.rcParams["xtick.labelsize"] = 14
plt.rcParams["font.family"] = [font]
idx = df_longform.groupby("Keyword")["Interest"].idxmax()
row_order = df_longform.loc[idx].sort_values("date")["Keyword"].tolist()
g = sns.FacetGrid(
df_longform,
row="Keyword",
hue="Keyword",
aspect=aspect,
height=height,
palette=palette,
row_order=row_order,
hue_order=row_order,
)
def label(x, color, label):
ax = plt.gca()
ax.text(
0,
0.2,
label,
fontsize=14,
fontweight="bold",
color=color,
ha="left",
va="center",
transform=ax.transAxes,
)
g.map(label, "date")
g.map(plt.plot, "date", "Interest", color="white", linewidth=plotlinewidth)
g.map(plt.fill_between, "date", "Interest", alpha=1)
g.refline(y=0, linewidth=reflinewidth, linestyle="-", color=None, clip_on=False)
g.figure.subplots_adjust(hspace=hspace)
g.set_titles("")
g.set(
yticks=[],
ylabel="",
xlabel="",
xlim=[
df_longform["date"].min() - datetime.timedelta(days=indent),
df_longform["date"].max(),
],
xticks=[
datetime.datetime(df_longform.date.min().year, i, 12) for i in range(1, 13)
],
xticklabels=[
"JAN",
"FEB",
"MAR",
"APR",
"MAY",
"JUN",
"JUL",
"AUG",
"SEP",
"OCT",
"NOV",
"DEC",
],
)
g.despine(bottom=True, left=True)
plt.suptitle(f"{df_longform.date.min().year} in Search Trends", fontsize=20)
plt.show()