You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Plotly animation_frame滑块仅显示首年数据问题求助

问题

使用Plotly处理水库数据的animation_frame时遇到异常:通过滑块切换年份展示藻类数据时,2019年数据显示正常,但切换到2020、2021、2022或2023年时,尽管DataFrame中存在对应年份的数据,却没有任何数据显示。尝试过按年份排序,也参考了Plotly动画教程,均未解决问题。

代码片段

import pandas as pd
import plotly.express as px
import plotly.io as pio

# 读取Excel数据到DataFrame
df = pd.read_excel('Master Reservoir Data.xlsx')

# 添加Year列,从Date提取年份并转为字符串,然后按年份排序
df['Year'] = pd.to_datetime(df['Date']).dt.year.astype(str)
df = df.sort_values(by='Year')

# 筛选Rockwell地点的数据
filtered_df = df[(df['Location'] == 'Rockwell Dam/Pier')]

# 创建散点图,按年份生成动画滑块
fig = px.scatter(filtered_df, x='Date', y='Depth (m)',
                 title='Blue-Green Algae Profiles at Rockwell Dam, YSI Sonde, All Depths',
                 color='Blue-Green Algae (cells/mL)',
                 color_continuous_scale=['green', 'yellow', 'red'],
                 range_color=(0,10000), animation_frame='Year')
fig.update_layout(yaxis_autorange='reversed')
fig.show()

数据片段(filtered_df)

DateYearLocationDepth (m)Blue-Green Algae (cells/mL)
8/25/20192019Rockwell Dam/Pier6.010000.0
8/25/20192019Rockwell Dam/Pier5.011140.0
8/25/20192019Rockwell Dam/Pier0.08000.0
9/15/20202010Rockwell Dam/Pier1.010900.0
7/25/20202010Rockwell Dam/Pier2.011200.0
6/10/20202020Rockwell Dam/Pier2.0NaN
5/25/20212021Rockwell Dam/Pier3.0NaN
4/13/20212021Rockwell Dam/Pier4.0NaN
5/2/20222022Rockwell Dam/Pier5.0NaN
8/20/20222022Rockwell Dam/Pier0.0560.0

解决方案

问题根源

  1. Year列数据错误:数据片段中2020年的两条记录被错误标记为2010,导致动画滑块切换到2020时无法匹配对应数据。
  2. NaN值过滤:Plotly默认会忽略Blue-Green Algae (cells/mL)为NaN的行,2020、2021、2022年多数记录为NaN,仅有的有效数据要么年份标记错误,要么被颜色范围限制隐藏。
  3. 颜色范围限制:设置的range_color=(0,10000)小于部分实际数据值(如11140、10900),超范围数据点会被隐藏。

修复步骤

  1. 修正Year列提取逻辑,确保从Date列正确解析年份。
  2. 处理NaN值:可选择填充NaN(如用0或均值),或保留NaN但设置默认显示样式。
  3. 调整颜色范围:根据数据最大值动态设置range_color,避免数据点被隐藏。

修正后的代码

import pandas as pd
import plotly.express as px
import plotly.io as pio

# 读取Excel数据到DataFrame
df = pd.read_excel('Master Reservoir Data.xlsx')

# 确保Date列转为datetime类型,正确提取年份并转为字符串
df['Date'] = pd.to_datetime(df['Date'])
df['Year'] = df['Date'].dt.year.astype(str)
df = df.sort_values(by='Year')

# 筛选Rockwell地点的数据
filtered_df = df[(df['Location'] == 'Rockwell Dam/Pier')]

# 处理藻类数据的NaN值:用0填充(可根据需求选择其他填充方式)
filtered_df['Blue-Green Algae (cells/mL)'] = filtered_df['Blue-Green Algae (cells/mL)'].fillna(0)

# 获取藻类数据的最大值,动态设置颜色范围
max_algae = filtered_df['Blue-Green Algae (cells/mL)'].max()

# 创建散点图,按年份生成动画滑块
fig = px.scatter(filtered_df, x='Date', y='Depth (m)',
                 title='Blue-Green Algae Profiles at Rockwell Dam, YSI Sonde, All Depths',
                 color='Blue-Green Algae (cells/mL)',
                 color_continuous_scale=['green', 'yellow', 'red'],
                 range_color=(0, max_algae),  # 动态匹配最大值
                 animation_frame='Year')
fig.update_layout(yaxis_autorange='reversed')
fig.show()

额外说明

  • 若不想填充NaN,仅显示有有效藻类数据的点,可在筛选时添加条件:filtered_df = filtered_df.dropna(subset=['Blue-Green Algae (cells/mL)']),但需确保每个年份至少有一条有效数据。
  • 检查原始Excel数据中的Date格式是否一致,避免因日期解析错误导致Year列生成异常。

内容的提问来源于stack exchange,提问作者VirtualBobby

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 13:23:23