forked from BMAS30/Shark-attacks-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplots.py
More file actions
298 lines (233 loc) · 8.74 KB
/
plots.py
File metadata and controls
298 lines (233 loc) · 8.74 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# src/plots.py
# Expected columns (from shark_cleaning.py output):
# Year_final, Month, Month_name, Time_category, Country, Fatal Y/N
from __future__ import annotations
from pathlib import Path
from typing import Optional, Sequence
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
MONTH_ORDER = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December",
]
TIME_CATEGORY_ORDER = ["Early morning", "Morning", "Afternoon", "Evening", "Night", "Unknown"]
def _ensure_outdir(out_dir: Optional[str | Path]) -> Optional[Path]:
if out_dir is None:
return None
out_path = Path(out_dir)
out_path.mkdir(parents=True, exist_ok=True)
return out_path
def _finalize(fig: plt.Figure, outpath: Optional[Path], show: bool = False, dpi: int = 150) -> None:
if outpath is not None:
fig.savefig(outpath, bbox_inches="tight", dpi=dpi)
if show:
plt.show()
plt.close(fig)
def _require_cols(df: pd.DataFrame, cols: Sequence[str]) -> None:
missing = [c for c in cols if c not in df.columns]
if missing:
raise ValueError(f"Missing required columns: {missing}")
def plot_attacks_per_year(
df,
out_dir=None,
filename="attacks_per_year.png",
show=False,
min_year=1900,
):
_require_cols(df, ["Year_final"])
out_path = _ensure_outdir(out_dir)
years = pd.to_numeric(df["Year_final"], errors="coerce")
years = years[years >= min_year]
s = years.value_counts().sort_index()
fig = plt.figure()
plt.plot(s.index, s.values)
plt.grid(alpha=0.3)
plt.title(f"Shark attacks per year (>= {min_year})")
plt.xlabel("Year")
plt.ylabel("Count")
# Escala de 20 em 20 anos
ticks = list(range(min_year, int(s.index.max()) + 1, 20))
plt.xticks(ticks)
_finalize(fig, out_path / filename if out_path else None, show=show)
def plot_attacks_by_month(
df: pd.DataFrame,
out_dir: Optional[str | Path] = None,
filename: str = "attacks_by_month.png",
show: bool = False,
use_month_names: bool = True,
) -> None:
"""Bar plot: attacks by month (calendar order)."""
_require_cols(df, ["Month", "Month_name"])
out_path = _ensure_outdir(out_dir)
if use_month_names:
s = df["Month_name"].fillna("Unknown").value_counts()
s = s.reindex(MONTH_ORDER).dropna()
x = s.index.tolist()
y = s.values
fig = plt.figure()
plt.bar(x, y)
plt.title("Shark attacks by month")
plt.xlabel("Month")
plt.ylabel("Count")
plt.xticks(rotation=45, ha="right")
else:
s = df["Month"].dropna().astype(int).value_counts().sort_index()
fig = plt.figure()
plt.bar(s.index.astype(str), s.values)
plt.title("Shark attacks by month")
plt.xlabel("Month (1–12)")
plt.ylabel("Count")
_finalize(fig, out_path / filename if out_path else None, show=show)
def plot_attacks_by_time_category(
df: pd.DataFrame,
out_dir: Optional[str | Path] = None,
filename: str = "attacks_by_time_category.png",
show: bool = False,
) -> None:
"""Bar plot: attacks by time-of-day category."""
_require_cols(df, ["Time_category"])
out_path = _ensure_outdir(out_dir)
s = df["Time_category"].fillna("Unknown").value_counts()
s = s.reindex(TIME_CATEGORY_ORDER).fillna(0)
fig = plt.figure()
plt.bar(s.index, s.values)
plt.title("Shark attacks by time of day")
plt.xlabel("Time category")
plt.ylabel("Count")
plt.xticks(rotation=30, ha="right")
_finalize(fig, out_path / filename if out_path else None, show=show)
def plot_fatal_vs_nonfatal(
df: pd.DataFrame,
out_dir: Optional[str | Path] = None,
filename: str = "fatal_vs_nonfatal.png",
show: bool = False,
) -> None:
"""Bar plot: fatal vs non-fatal counts."""
_require_cols(df, ["Fatal Y/N"])
out_path = _ensure_outdir(out_dir)
s = df["Fatal Y/N"].fillna("Unknown").value_counts()
fig = plt.figure()
plt.bar(s.index, s.values)
plt.title("Fatal vs Non-Fatal")
plt.xlabel("Outcome")
plt.ylabel("Count")
plt.xticks(rotation=15, ha="right")
_finalize(fig, out_path / filename if out_path else None, show=show)
def plot_attacks_by_month_and_outcome_stacked(
df,
out_dir=None,
filename="attacks_by_month_outcome_stacked.png",
show=False,
) -> None:
"""Stacked bar: month (calendar order) split by Non-Fatal / Fatal / Unknown."""
_require_cols(df, ["Month_name", "Fatal Y/N"])
out_path = _ensure_outdir(out_dir)
# Build month x outcome table
tmp = (
df.assign(
Month_name=df["Month_name"].fillna("Unknown"),
**{"Fatal Y/N": df["Fatal Y/N"].fillna("Unknown")},
)
.groupby(["Month_name", "Fatal Y/N"])
.size()
.unstack(fill_value=0)
)
# Keep calendar months only and in order
tmp = tmp.reindex(MONTH_ORDER).dropna(how="all")
# Force a clean, consistent outcome order (ignore any unexpected categories)
desired_cols = ["Non-Fatal", "Fatal", "Unknown"]
tmp = tmp[[c for c in desired_cols if c in tmp.columns]]
# Color palette (harmonious)
color_map = {
"Non-Fatal": "#90CAF9", # light blue
"Fatal": "#0D47A1", # dark blue
"Unknown": "#B0BEC5", # bluish grey
}
fig = plt.figure()
bottom = np.zeros(len(tmp.index))
for col in tmp.columns:
vals = tmp[col].values
plt.bar(
tmp.index,
vals,
bottom=bottom,
label=col,
color=color_map.get(col, None),
)
bottom = bottom + vals
plt.title("Shark attacks by month (fatal and non-fatal)")
plt.xlabel("Month")
plt.ylabel("Count")
plt.xticks(rotation=45, ha="right")
plt.legend()
_finalize(fig, out_path / filename if out_path else None, show=show)
def plot_top_countries(
df: pd.DataFrame,
top_n: int = 15,
out_dir: Optional[str | Path] = None,
filename: str = "top_countries.png",
show: bool = False,
) -> None:
"""Horizontal bar: top N countries by number of attacks."""
_require_cols(df, ["Country"])
out_path = _ensure_outdir(out_dir)
s = df["Country"].fillna("Unknown").value_counts().head(top_n)
s = s.sort_values() # for nicer horizontal bars (small->large top-to-bottom)
fig = plt.figure()
plt.barh(s.index, s.values)
plt.title(f"Top {top_n} countries by attack count")
plt.xlabel("Count")
plt.ylabel("Country")
_finalize(fig, out_path / filename if out_path else None, show=show)
def plot_heatmap_month_by_time(
df: pd.DataFrame,
out_dir: Optional[str | Path] = None,
filename: str = "heatmap_month_by_time.png",
show: bool = False,
) -> None:
"""Heatmap: Month (rows) x Time_category (cols) counts."""
_require_cols(df, ["Month_name", "Time_category"])
out_path = _ensure_outdir(out_dir)
tmp = (
df.assign(
Month_name=df["Month_name"].fillna("Unknown"),
Time_category=df["Time_category"].fillna("Unknown"),
)
.groupby(["Month_name", "Time_category"])
.size()
.unstack(fill_value=0)
)
# Restrict to real months for cleaner seasonality view
tmp = tmp.reindex(MONTH_ORDER).dropna(how="all")
tmp = tmp.reindex(columns=TIME_CATEGORY_ORDER, fill_value=0)
fig = plt.figure()
plt.imshow(tmp.values, aspect="auto")
plt.title("Attacks heatmap: month vs time of day")
plt.xlabel("Time category")
plt.ylabel("Month")
plt.xticks(ticks=np.arange(len(tmp.columns)), labels=tmp.columns, rotation=30, ha="right")
plt.yticks(ticks=np.arange(len(tmp.index)), labels=tmp.index)
# Annotate cells useful to keep light to avoid clutter for big values
for i in range(tmp.shape[0]):
for j in range(tmp.shape[1]):
val = int(tmp.iloc[i, j])
if val > 0:
plt.text(j, i, str(val), ha="center", va="center")
plt.colorbar(label="Count")
_finalize(fig, out_path / filename if out_path else None, show=show)
def generate_all_figures(
df: pd.DataFrame,
out_dir: str | Path = "reports/figures",
show: bool = False,
top_countries_n: int = 15,
) -> None:
"""Convenience wrapper to generate a standard set of figures."""
out_path = _ensure_outdir(out_dir)
plot_attacks_per_year(df, out_dir=out_path, show=show)
plot_attacks_by_month(df, out_dir=out_path, show=show)
plot_attacks_by_time_category(df, out_dir=out_path, show=show)
plot_fatal_vs_nonfatal(df, out_dir=out_path, show=show)
plot_attacks_by_month_and_outcome_stacked(df, out_dir=out_path, show=show)
plot_top_countries(df, top_n=top_countries_n, out_dir=out_path, show=show)
plot_heatmap_month_by_time(df, out_dir=out_path, show=show)