Visualizing China’s Olympic Medal History with Python and Plotly
This tutorial walks through collecting China’s Olympic medal data from 1984 to 2016, reshaping it into wide and long tables, and creating a series of interactive visualizations—including line charts, scatter plots, radar charts, and sunburst diagrams—using Python's pandas and Plotly libraries.
Hello, I'm Peter. This article visualizes China's Olympic medal performance from 1984 to 2016 using Python and Plotly.
Data Preparation
The raw data was collected from the web and organized into two formats:
Wide table : each row contains all fields for a given Olympic edition.
Long table : each row records a single metric, allowing repeated fields.
Both tables are used for different visualizations.
Import Libraries
# Data processing
import pandas as
import numpy as
# Plotting
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots # for multi‑subplot figuresTotal Medal Count Trend
A line chart shows the total number of medals China won at each Summer Olympics.
fig = px.line(df, x="地点", y="总数", text="总数")
fig.update_layout(title="中国获得总奖牌数")
fig.show()Medal Distribution by Gender
A scatter plot compares gold medals won by men and women across editions.
fig = px.scatter(df, x="地点", y="总数", color="金牌", size="总数", text="总数")
fig.show()Multi‑Metric Subplots
Using make_subplots, the script creates a 4×2 grid showing total medals, gold, silver, bronze, gold‑rank, male gold, female gold, and gold‑share.
# Set rows and columns
fig = make_subplots(rows=4, cols=2,
subplot_titles=["奖牌总数","金牌","银牌","铜牌","金牌榜","男子金牌","女子金牌","金牌占比"])
# Add traces (example for total medals)
fig.add_trace(go.Scatter(x=df["年份"].tolist(), y=df["总数"].tolist(), name="总数"), 1, 1)
# ... (additional traces omitted for brevity)
fig.update_layout(height=600, width=800, title_text="奥运会奖牌可视化")
fig.show()Radar Chart
The radar chart displays gold, silver, and bronze counts for each Olympic edition.
categories = ['金牌','银牌','铜牌']
fig = go.Figure()
fig.add_trace(go.Scatterpolar(r=df.iloc[0,2:5].tolist(), theta=categories, fill='tonext', name='洛杉矶-1984'))
# ... (additional years omitted)
fig.update_layout(polar=dict(radialaxis=dict(visible=True, range=[0,54])), showlegend=True)
fig.show()Pie Charts by Year
Donut charts compare the proportion of gold, silver, and bronze medals for each Olympic edition.
fig = make_subplots(rows=3, cols=3, specs=[[{'type':'domain'}]*3]*3,
subplot_titles=["1984-洛杉矶","1988-汉城","1992-巴塞罗那","1996-亚特兰大","2000-悉尼","2004-雅典","2008-北京","2012-伦敦","2016-里约热内卢"])
fig.add_trace(go.Pie(labels=df2["奖牌"][:3].tolist(), values=df2["数量"][:3].tolist(), name="1984-洛杉矶"),1,1)
# ... (additional pies omitted)
fig.update_traces(hole=0.2)
fig.show()Sunburst Diagram
A sunburst visualizes the hierarchical relationship between medal type, Olympic edition, and count.
px.sunburst(df2, path=["奖牌","地点"], values="数量", color="年份", color_continuous_scale="RdBu")Conclusion
The visual analysis shows that China’s most successful Olympics was the 2008 Beijing Games, with the highest total medal count and gold‑medal share. Across all editions, women’s gold medals consistently outperformed men’s, highlighting the strength of the Chinese female athletes.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
