Spaces:
Sleeping
Sleeping
Update plotting_service.py
Browse files- 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
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
ax.
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|