用Python进行奥运会奖牌榜可视化分析

用Python进行奥运会奖牌榜可视化分析

斑点鱼最近在看奥运会,看游泳看的心潮澎湃,看乒乓看的跌宕起伏,看跳水看的连连惊叹~体操不敢看😂

最喜欢马龙,最后团体赛的胜利动作太帅了!

最佩服潘展乐,小孩哥一个字,绝👍🏻 00后真是敢说啊!

最心疼张博恒,美强惨,论队友的重要性!

那作为一名数据分析师,就用python可视化来看看我们中国的🏅之旅有多么厉害吧~

巴黎奥运会中国金牌时间表

df1=df_e[['date','event']]
# 设置画布大小
fig, ax = plt.subplots(figsize=(1, 50))

# 绘制垂直条形图
ax.plot([0.5]*len(df1), df1.index, marker='|', markersize=100, linestyle='', color='red')
ax.scatter([0.5]*len(df1),df1.index,marker='o', color='red',s=100)

# 设置X轴的刻度和标签
plt.yticks(df1.index, df1.date)

# 隐藏Y轴
ax.get_xaxis().set_visible(False)

# 添加事件名称
for i, name in enumerate(df1.event):
    plt.text(0.6, i, name)

# 设置标题和网格线
plt.title('2024巴黎奥运会中国夺金时间轴')
plt.grid(True, axis='y', alpha=0.5)
# plt.savefig('paris china gold.png')
plt.show()
  • 图1
    在这里插入图片描述
    在这里插入图片描述

历年奥运中国奖牌数TOP20-动态条形图

中国名列前茅~~~

cnv = nim.Canvas()      # 创建一个画布
# 创建一个动态柱状图
bar = nim.Barplot(df_p, time_format="%Y-%m-%d", ip_freq="4y", rounded_edges=True, n_bars=20)
# 设置柱状图中的柱体,使之与统计数据中的列名对应起来,并设置插帧频率和柱体数量
bar.set_time(callback=lambda i, datafier: datafier.data.index[i].year)
cnv.add_plot(bar)   # 将柱状图添加到画布
cnv.animate()       # 开始创建动画
# 将动画结果保存为 gif 格式, 文件名为 Result_1.gif,保存在当前工作路径下
cnv.save("prize top20 white", 1, "gif") 
  • 图2
    在这里插入图片描述

优化下背景~~~

# 定义回调函数,用于设置条形图的样式
def post_update(ax, i, datafier, bar_attr):
     ax.spines["top"].set_visible(False)
     ax.spines["right"].set_visible(False)
     ax.spines["bottom"].set_visible(False)
     ax.spines["left"].set_visible(False)
     ax.set_facecolor("#001219")  # 暗黑色
    
# 创建一个画布对象 cnv,设置画布长宽和背景颜色
cnv = nim.Canvas(figsize=(17, 10), facecolor="#001219")  # 暗黑色,与柱形图填充色保持一致

# 创建一个Barplot对象bar,设置数据、时间格式、回调函数、圆角边框、网格、横轴变化等样式和效果
bar = nim.Barplot(data=df_p,      time_format ="%Y-%m-%d", 
 ip_freq='4y',  
 ip_frac=1, 
 fixed_xlim=False,                
 post_update=post_update, 
 rounded_edges=True, 
 grid=True,         
 n_bars=20, 
)

# 设置图表标题的内容和字体颜色、字体大小;w 代表 white
bar.set_title("1992-2024奥运奖牌榜TOP20",
             color="w",
             size=21)
            
# 设置横轴名称、字体颜色和大小
bar.set_xlabel("奥运奖牌数量(包含金、银、铜)", color="w", size=15
            )

# 设置柱状图右下方时间的显示方式、字体颜色、大小
bar.set_time(callback=lambda i, datafier: \
         datafier.data.index[i].strftime("%Y"),  
         x=0.95,      
         y=0.30,      
         color="w",   
         size=65)     
         
# 设置辅助文本的内容和样式
bar.set_text(
 "sum",   
 callback=lambda i, datafier: \
         f"Total : {int(datafier.data.iloc[i].sum())}\nDesigned by SpotFish 斑点鱼",
 size=25,    
 x=0.72,   
 y=0.18,    
 color="w",  
)

# 设置柱体注释(柱体顶部的文字)的样式
bar.set_bar_annots(color="w", size=13)

# 设置x轴和y轴刻度和文字的样式
bar.set_xticks(colors="w",  
             length=0,    
             labelsize=13) 
bar.set_yticks(colors="w", labelsize=13) 

# 设置柱体的样式
bar.set_bar_border_props(
 edge_color="black", 
 pad=0.1, 
 mutation_aspect=1, 
 radius=0.2, 
 mutation_scale=0.6
)

cnv.add_plot(bar)
cnv.animate()

# 保存动画图表为 gif 格式
cnv.save(filename="prize top20 black",  fps=1,  extension="gif") 
  • 图3
    在这里插入图片描述

历年奥运中国金牌数TOP10-动态条形图

美丽的大中国呀~

fig, ax = plt.subplots(figsize=(15, 8))
def draw_barchart(current_year):   
    #dff对year==current_year的行,以value从升序方式排序,取后十名也就是最大值;
    dff = df_m[df_m['year'].eq(current_year)].sort_values(by='value',ascending = True).tail(12)
    ax.barh(dff['name'],dff['value'],color = [colors[x] for x in dff['name']])
    dx = dff['value'].max()/200
    for i ,(value,name) in enumerate(zip(dff['value'], dff['name'])):
        ax.text(value-dx,i,name,size=14,weight=600,ha ='right',va = 'center')
        ax.text(value+dx,i ,f'{value:,.0f}',size = 14,ha = 'left',va ='center')    
    ax.text(1,0.4,current_year,transform = ax.transAxes,color ='#777777',size = 46,ha ='right',weight=800) 
    ax.text(0,1.06,'Gold prize amount',transform = ax.transAxes,size=12,color='#777777')    
    ax.xaxis.set_major_formatter(ticker.StrMethodFormatter('{x:,.0f}'))
    ax.xaxis.set_ticks_position('top')
    ax.tick_params(axis='x',colors='#777777',labelsize=12)
    ax.set_yticks([])
    ax.grid(which='major',axis='x',linestyle='-')
    ax.set_axisbelow(True)
    ax.text(0,1.15,'The Gold Prize in Olympics from 1992 to 2024',
           transform=ax.transAxes,size=24,weight=600,ha='left',va='top')
    ax.text(1,0,'SpotFish 斑点鱼',transform = ax.transAxes,color ='#777777',ha = 'right',
           bbox = dict(facecolor='white',alpha = 0.8,edgecolor='white'))
    plt.box(False)
  • 图4

在这里插入图片描述

  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

斑点鱼 SpotFish

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值