cwadayi commited on
Commit
7fcf7f4
·
verified ·
1 Parent(s): 36527a2

Update plotting_service.py

Browse files
Files changed (1) hide show
  1. plotting_service.py +44 -53
plotting_service.py CHANGED
@@ -1,53 +1,44 @@
1
- # plotting_service.py
2
- import os
3
- import uuid
4
- import pandas as pd
5
- import matplotlib
6
- matplotlib.use("Agg") # 使用非互動式後端
7
- import matplotlib.pyplot as plt
8
- from matplotlib.colors import Normalize
9
- import matplotlib.cm as cm
10
- from matplotlib import font_manager as fm
11
- from config import STATIC_DIR, CURRENT_YEAR
12
-
13
- def setup_chinese_font():
14
- """如果系統中存在,則為 Matplotlib 設定中文字體。"""
15
- font_paths = [
16
- "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
17
- "/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc",
18
- "/usr/share/fonts/truetype/wqy/wqy-microhei.ttc",
19
- "/usr/share/fonts/truetype/arphic/ukai.ttc",
20
- ]
21
- for fp in font_paths:
22
- if os.path.exists(fp):
23
- matplotlib.rcParams["font.family"] = fm.FontProperties(fname=fp).get_name()
24
- break
25
-
26
- def create_and_save_map(df: pd.DataFrame) -> str:
27
- """建立地震地圖,儲存圖片並回傳檔案名稱。"""
28
- setup_chinese_font()
29
- fig, ax = plt.subplots(figsize=(9, 6), dpi=150)
30
- ax.set_xlim(118.5, 123.5)
31
- ax.set_ylim(20.5, 26.8)
32
- ax.set_xlabel("Longitude (°E)")
33
- ax.set_ylabel("Latitude (°N)")
34
- ax.set_title(f"今年 ({CURRENT_YEAR}) 台灣區域顯著地震 (M≥5.0) — UTC")
35
- ax.grid(True, linestyle="--", linewidth=0.5, alpha=0.4)
36
-
37
- mags = df["magnitude"].astype(float).clip(lower=0)
38
- norm = Normalize(vmin=max(4.5, mags.min()), vmax=max(6.5, mags.max()))
39
- cmap = cm.get_cmap("YlOrRd")
40
- colors = cmap(norm(mags.values))
41
- sizes = 15 + (mags - mags.min()) * 25
42
-
43
- ax.scatter(df["longitude"].values, df["latitude"].values,
44
- s=sizes, c=colors, edgecolor="k", linewidths=0.4, alpha=0.9)
45
-
46
- fig.colorbar(cm.ScalarMappable(norm=norm, cmap=cmap), ax=ax, pad=0.02).set_label("Magnitude")
47
-
48
- filename = f"map_{uuid.uuid4().hex}.png"
49
- filepath = os.path.join(STATIC_DIR, filename)
50
- fig.tight_layout()
51
- fig.savefig(filepath)
52
- plt.close(fig)
53
- return filename
 
1
+ # plotting_service.py
2
+ import os
3
+ import uuid
4
+ import pandas as pd
5
+ import matplotlib
6
+ matplotlib.use("Agg") # 使用非互動式後端
7
+ import matplotlib.pyplot as plt
8
+ from matplotlib.colors import Normalize
9
+ import matplotlib.cm as cm
10
+ from config import STATIC_DIR, CURRENT_YEAR
11
+
12
+ # Note: The setup_chinese_font function is no longer needed as all text will be in English.
13
+
14
+ def create_and_save_map(df: pd.DataFrame) -> str:
15
+ """Create an earthquake map, save the image, and return the filename."""
16
+ # The call to setup_chinese_font() has been removed.
17
+ fig, ax = plt.subplots(figsize=(9, 6), dpi=150)
18
+ ax.set_xlim(118.5, 123.5)
19
+ ax.set_ylim(20.5, 26.8)
20
+ ax.set_xlabel("Longitude (°E)")
21
+ ax.set_ylabel("Latitude (°N)")
22
+
23
+ # [MODIFIED] The title has been changed to English.
24
+ ax.set_title(f"Significant Earthquakes (M≥5.0) in Taiwan Area This Year ({CURRENT_YEAR}) — UTC")
25
+
26
+ ax.grid(True, linestyle="--", linewidth=0.5, alpha=0.4)
27
+
28
+ mags = df["magnitude"].astype(float).clip(lower=0)
29
+ norm = Normalize(vmin=max(4.5, mags.min()), vmax=max(6.5, mags.max()))
30
+ cmap = cm.get_cmap("YlOrRd")
31
+ colors = cmap(norm(mags.values))
32
+ sizes = 15 + (mags - mags.min()) * 25
33
+
34
+ ax.scatter(df["longitude"].values, df["latitude"].values,
35
+ s=sizes, c=colors, edgecolor="k", linewidths=0.4, alpha=0.9)
36
+
37
+ fig.colorbar(cm.ScalarMappable(norm=norm, cmap=cmap), ax=ax, pad=0.02).set_label("Magnitude")
38
+
39
+ filename = f"map_{uuid.uuid4().hex}.png"
40
+ filepath = os.path.join(STATIC_DIR, filename)
41
+ fig.tight_layout()
42
+ fig.savefig(filepath)
43
+ plt.close(fig)
44
+ return filename