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

奥运奖牌数据可视化双问:展示奖牌逐年增长的最佳图表类型及Plotly动画散点图连续化问题排查

Hey there! Let's work through your Plotly animation problem first, then dive into the best chart types for tracking Olympic medal growth over time.

Fixing the Discrete Animation Issue

The jumpy, disappearing/reappearing points in your animation are almost certainly caused by missing year data for some countries. If a country didn't participate (or didn't win any medals) in a particular Olympic year, it won't show up in your aggregated df3—so when the animation frame switches to that year, the country's point vanishes entirely.

Here's how to fix this by filling in the missing year entries for every country:

  1. First, create a complete list of all Olympic years and all countries from your dataset.
  2. Generate a full combination of every country + every year (a Cartesian product).
  3. Merge this full dataset with your original df3, then fill in missing values appropriately:
    • For Total (annual medals), fill missing values with 0 (since no medals = 0 that year).
    • For Cum_Total (cumulative medals), use forward filling (ffill) grouped by country—this keeps the cumulative count consistent even in years where the country didn't win medals.

Here's the code to implement this:

import pandas as pd
import plotly.express as px

# Extract all unique years and countries from your original data
all_years = sorted(df3['Year'].unique())
all_countries = df3['Country'].unique()

# Create a full dataset with every country-year combination
full_index = pd.MultiIndex.from_product([all_countries, all_years], names=['Country', 'Year'])
full_df = full_index.reset_index()

# Merge with your original data and fill missing values
full_df = full_df.merge(df3, on=['Country', 'Year'], how='left')
full_df['Total'] = full_df['Total'].fillna(0)
# Forward fill cumulative totals per country
full_df['Cum_Total'] = full_df.groupby('Country')['Cum_Total'].ffill()

# Generate the animation with the filled dataset
fig = px.scatter(full_df, 
                 x='Cum_Total', y='Total', 
                 animation_group='Country', animation_frame='Year', 
                 size='Cum_Total', size_max=100, 
                 color='Country', hover_name='Country', 
                 range_y=[1,300], range_x=[1,3000])
fig.show()

This ensures every country has a data point in every year, so the animation will transition smoothly without points popping in and out.

Best Chart Types for Olympic Medal Growth

The ideal chart depends on what exactly you want to highlight:

  • Single country's cumulative growth: A simple line chart is perfect. Plot Year on the x-axis and Cum_Total on the y-axis—it clearly shows the steady (or sporadic) growth of medals over time.
  • Multi-country cumulative trend comparison:
    • Animated line chart: Use px.line() with animation_frame='Year' to watch each country's cumulative medal line extend year by year. This makes it easy to spot which countries have consistent growth vs. sudden spikes.
    • Animated bar chart: If you want to emphasize rankings, create an annual bar chart showing the top N countries by Cum_Total (or Total). Use px.bar() with animation_frame='Year'—this is super engaging for audiences to see how rankings shift over decades.
    • Your animated scatter plot (fixed): Once you fill in the missing data, this is great for showing two dimensions at once: annual medal count (y-axis) and total cumulative medals (x-axis). The size and color add extra context to distinguish countries.
  • Cumulative medal share over time: An area chart can show how each country's share of total cumulative medals changes over years, but this works best if you focus on a small number of top countries (too many will make the chart cluttered).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 09:42:43