2catycm commited on
Commit
461075b
·
1 Parent(s): f6b784e

feat: 第三题基本完成

Browse files
Files changed (1) hide show
  1. app.py +66 -6
app.py CHANGED
@@ -77,7 +77,7 @@ with col1:
77
 
78
  # 图(b): 车辆移动散点图(移动传感器数据)
79
  with col2:
80
- st.header("(b) A Scatter Plot that shows how vehicles carrying mobile sensors move in the city.")
81
  with st.expander("绘制要求"):
82
  st.markdown("1. 使用 mobile 文件夹中的 .csv 文件,并筛选出时间戳在 2019-01-02 10:00:00 到 2019-01-02 10:20:00 之间的数据。")
83
  st.markdown("2. X 轴和 Y 轴分别表示经度和纬度。")
@@ -140,10 +140,70 @@ with col2:
140
  st.markdown("**解读:** 通过调整散点透明度(早期数据较透明),图中显示了车辆移动的时间演变趋势,为探索城市中车辆行驶路径提供依据。")
141
 
142
 
143
- # 图(c): 第三个图表(预留,用于展示其他分析)
144
  with col3:
145
- st.header("(c) [预留] 第三个图表")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  with st.expander("详细说明"):
147
- st.markdown("**描述:** 此图为预留图表,可用于展示PM2.5分布或其他数据分析结果。")
148
- st.markdown("**解读:** 后续将根据数据分析补充图表内容及详细解释。")
149
- st.write("在这里添加您的第三个图表")
 
77
 
78
  # 图(b): 车辆移动散点图(移动传感器数据)
79
  with col2:
80
+ st.header("b)散点图:展示车辆搭载移动传感器在城市中的移动情况")
81
  with st.expander("绘制要求"):
82
  st.markdown("1. 使用 mobile 文件夹中的 .csv 文件,并筛选出时间戳在 2019-01-02 10:00:00 到 2019-01-02 10:20:00 之间的数据。")
83
  st.markdown("2. X 轴和 Y 轴分别表示经度和纬度。")
 
140
  st.markdown("**解读:** 通过调整散点透明度(早期数据较透明),图中显示了车辆移动的时间演变趋势,为探索城市中车辆行驶路径提供依据。")
141
 
142
 
143
+ # 图(c): 3D直方图——展示整个城市在特定时段内的PM2.5分布
144
  with col3:
145
+ st.header("c)3D直方图:展示整个城市在特定时段内的PM2.5分布")
146
+ with st.expander("绘制要求"):
147
+ st.markdown("1. 使用 mobile 和 static 文件夹中的 .csv 文件,并筛选出时间戳在 2019-01-01 09:00:00 到 2019-01-01 09:10:00 之间的数据。")
148
+ st.markdown("2. 将数据的GPS坐标转换为网格坐标(例如采用 0.01 度的分辨率),并在相同网格内聚合数据。")
149
+ st.markdown("3. X 轴、Y 轴和 Z 轴分别表示经度、纬度和PM2.5数值。")
150
+
151
+ # 定义过滤时间段
152
+ start_c = datetime(2019, 1, 1, 9, 0, 0)
153
+ end_c = datetime(2019, 1, 1, 9, 10, 0)
154
+
155
+ # 读取 static 文件夹中的 CSV 文件数据
156
+ csv_files_static = [f for f in os.listdir(data_folder) if f.endswith('.csv')]
157
+ data_list = []
158
+ for file in csv_files_static:
159
+ file_path = os.path.join(data_folder, file)
160
+ df = load_and_filter_data(file_path, start_c, end_c)
161
+ if not df.empty:
162
+ data_list.append(df)
163
+
164
+ # 读取 mobile 文件夹中的 CSV 文件数据
165
+ csv_files_mobile = [f for f in os.listdir(str(this_directory / "data/Cangzhou/Mobile")) if f.endswith('.csv')]
166
+ mobile_folder = str(this_directory / "data/Cangzhou/Mobile")
167
+ for file in csv_files_mobile:
168
+ file_path = os.path.join(mobile_folder, file)
169
+ df = load_and_filter_data(file_path, start_c, end_c)
170
+ if not df.empty:
171
+ data_list.append(df)
172
+
173
+ # 合并所有数据
174
+ if data_list:
175
+ df_all = pd.concat(data_list, ignore_index=True)
176
+ else:
177
+ df_all = pd.DataFrame(columns=["timestamp", "pm2d5", "lat", "lon"])
178
+
179
+ # 将 GPS 坐标转换为网格坐标(采用 0.01 度分辨率)
180
+ resolution = 0.01
181
+ df_all["lon_grid"] = df_all["lon"].round(2)
182
+ df_all["lat_grid"] = df_all["lat"].round(2)
183
+
184
+ # 聚合每个网格内的PM2.5数据(计算平均值)
185
+ df_group = df_all.groupby(["lon_grid", "lat_grid"]).agg({"pm2d5": "mean"}).reset_index()
186
+
187
+ # 绘制3D直方图:由于 Plotly 不支持原生 Bar3d,这里采用 3D 表面图展示网格数据
188
+ # 将聚合数据转换为适合表面图的矩阵
189
+ if not df_group.empty:
190
+ pivot = df_group.pivot(index="lat_grid", columns="lon_grid", values="pm2d5")
191
+ pivot = pivot.fillna(0)
192
+ # 创建3D表面图,x轴为经度、y轴为纬度、z轴为PM2.5平均值
193
+ fig3 = go.Figure(data=[go.Surface(z=pivot.values, x=pivot.columns, y=pivot.index, colorscale='Viridis')])
194
+ else:
195
+ fig3 = go.Figure()
196
+ fig3.update_layout(
197
+ scene=dict(
198
+ xaxis_title="经度",
199
+ yaxis_title="纬度",
200
+ zaxis_title="PM2.5"
201
+ ),
202
+ title="PM2.5分布3D表面图"
203
+ )
204
+ st.plotly_chart(fig3, use_container_width=True)
205
+
206
  with st.expander("详细说明"):
207
+ st.markdown("**描述:** 本图通过汇总 mobile 和 static 两种传感器数据,展示了 2019-01-01 09:00 至 09:10 期间整个城市内PM2.5浓度的分布。")
208
+ st.markdown("**解读:** 基于GPS坐标转换为网格坐标后,对相同网格中的数据进行聚合,直观展示不同区域的空气质量水平,从而为进一步的环境分析提供参考。")
209
+