-
Notifications
You must be signed in to change notification settings - Fork 0
/
charts.py
104 lines (86 loc) · 2.71 KB
/
charts.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
import altair as alt
import pandas as pd
from statsmodels.nonparametric.smoothers_lowess import lowess
date_tick_freq = {
"Last 12 Hours": "hour",
"Last 24 Hours": "hour",
"Last 7 Days": "day",
"Season": "week"
}
def time_series_chart(data, period):
"""Build the time series chart."""
x = range(len(data))
y = data["rolling_sentiment"].values
new_data = pd.DataFrame(
lowess(exog=x, endog=y, frac=0.03),
columns=["index", "rolling_sentiment"]
)
new_data["time"] = data.index
chart = alt.layer(
alt.Chart().mark_line(),
alt.Chart().mark_line().encode(
color=alt.Color('is_neg:N', legend=None),
),
data=new_data,
title="#MetsTwitter Sentiment"
).transform_calculate(
is_neg="datum.rolling_sentiment<0.5"
).encode(
x=alt.X(
"time:T",
axis=alt.Axis(
grid=False,
title="",
tickCount=date_tick_freq[period]
)
),
y=alt.Y(
'rolling_sentiment:Q',
impute={'value': None},
axis=alt.Axis(labels=False, grid=False, title=""),
scale=alt.Scale(domain=[0, 1.05])
)
)
line = alt.Chart(pd.DataFrame({'y': [0.5]})).mark_rule().encode(y='y')
return line + chart
def pos_neg_bar_chart(data: pd.DataFrame):
"""Bar chart allows negative numbers."""
data.reset_index(inplace=True)
cht = alt.Chart(data).mark_bar().encode(
x=alt.X(
"monthdate(index):T",
axis=alt.Axis(grid=False, title="", tickCount=alt.TimeIntervalStep(
alt.TimeInterval("day"),
step=2
))
),
y=alt.Y(
"rolling_sentiment:Q",
impute={'value': None},
axis=alt.Axis(labels=False, grid=False, title=""),
scale=alt.Scale(domain=[-1.05, 1.05])
),
color=alt.condition(
alt.datum.rolling_sentiment > 0,
alt.value("steelblue"), # The positive color
alt.value("orange") # The negative color
)
)
line = alt.Chart(pd.DataFrame({'y': [0]})).mark_rule().encode(y='y')
return line + cht
def bar_chart(data, period):
"""Build a simple bar chart."""
data["TOTAL"] = data["POS"] + data["NEG"]
data["time"] = data.index
chart = alt.Chart(data, title="# of Tweets").mark_bar().encode(
x=alt.X(
"time:T",
axis=alt.Axis(grid=False, title="", tickCount=date_tick_freq[period])
),
y=alt.Y(
'TOTAL:Q',
impute={'value': None},
axis=alt.Axis(labels=False, grid=False, title="")
)
)
return chart