Matplotlib | 世界足球俱乐部排名可视化

大家好,我是 👉【Python当打之年(点击跳转)】

本期我们参考FiveThirtyEight发布的世界足球俱乐部排名,利用Matplotlib手把手教大家如何制作新闻级别可视化作品 ,希望对小伙伴们有所帮助,如有疑问或者需要改进的地方可以私信小编。

先看看效果:
在这里插入图片描述

🏳️‍🌈 1. 导入模块

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.image as mpimg
from matplotlib.offsetbox import OffsetImage, AnnotationBbox

🏳️‍🌈 2. 示例数据

dic_t = {'Premier League':'England','Bundesliga':'Germany','Ligue 1':'France', 'La Liga':'Spain',
         'Eredivisie':'Netherlands','Serie A':'Italy', 'Primeira Liga':'Portugal','Bundesliga_1':'Austria'}
team = ['Man.City', 'Liverpool', 'Bayern Munich', 'Chelsea', 'PSG',
        'Real Madrid', 'Ajax', 'Barcelona', 'Tottnham', 'Inter Milan',
        'RB Leipzig', 'Porto', 'RB Salzburg', 'Dortmund', 'Atlético Madrid',
        'Arsenal', 'Villarreal', 'Brighton', 'AC Milan', 'Sporting'][::-1]
league = ['Premier League', 'Premier League', 'Bundesliga', 'Premier League', 'Ligue 1',
          'La Liga', 'Eredivisie', 'La Liga', 'Premier League', 'Serie A',
          'Bundesliga', 'Primeira Liga', 'Bundesliga_1', 'Bundesliga', 'La Liga',
          'Premier League', 'La Liga', 'Premier League', 'Serie A', 'Primeira Liga'][::-1]
league_country = [dic_t[i] for i in league]
rank = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20][::-1]
OFF = [3.3,2.9,3.4,2.4,2.8,2.7,3.0,2.4,2.5,2.5, 2.5,2.3,2.5,2.6,2.0,2.2,2.3,2.1,2.1,2.2][::-1]
DEF = [0.3,0.2,0.6,0.3,0.7,0.6,0.9,0.5,0.6,0.7, 0.3,0.2,0.6,0.3,0.7,0.6,0.9,0.5,0.6,0.7][::-1]
SPI = [93.5,93.3,91.6,88.4,85.0,85.5,84.6,84.3,84.1,82.1, 81.8,81.6,81.5,81.1,80.4,80.2,80.1,80.0,80.0,79.8][::-1]

🏳️‍🌈 3. 画布设置

fig, ax = plt.subplots(1, 1, dpi=300)
# 隐藏y轴
ax.get_yaxis().set_visible(False)
ax.get_xaxis().set_visible(False)
# 将刻度小横线设置为白色,标签颜色为黑色
plt.tick_params(axis='x', color='w')
# x,y轴刻度范围
ax.set_xlim(0, 2)
# ax.set_xticks(range(0, 10, 1))
ax.set_ylim(0, 4)

🏳️‍🌈 4. 画布区域主题分配

# 设置上下左右外框变为透明
for i in ['top', 'right', 'left', 'bottom']:
    ax.spines[i].set_visible(False)
title_size = 3.2
title_pos = 40
ax.axhline(y=title_pos, xmax=.8, c='black', lw=0.4)
ax.axvline(x=.7, ymin=0.002, ymax=.909, c='black', lw=0.4)
ax.axvline(x=3.95, ymin=0.002, ymax=.909, c='black', lw=0.4)
ax.axhline(y=0.1, xmax=.8, c='black', lw=0.4)

在这里插入图片描述

🏳️‍🌈 5. 添加数据散点

x1 = [4.1] * len(rank)
y1 = [1 + i * 2 for i in range(len(rank))]
ax.scatter(x1, y1, s=40,alpha=0.8)

在这里插入图片描述

🏳️‍🌈 6. 添加表头

ax.text(0.08, 0.2 + title_pos, 'RANK', fontdict={'color': '#1C1C1C', 'size': title_size}, fontweight='bold')
ax.text(0.39, 0.2 + title_pos, '1-WEEK\nCHANGE', fontdict={'color': '#1C1C1C', 'size': title_size}, fontweight='heavy')
ax.text(0.75, 0.2 + title_pos, 'TEAM', fontdict={'color': '#1C1C1C', 'size': title_size}, fontweight='heavy')

在这里插入图片描述

🏳️‍🌈 7. 添加数据

for i in range(len(rank)):
    ax.axhline(y=0.1 + i * 2, xmax=.8, c='gray', lw=0.4, alpha=0.2)
    ax.text(0.13, 0.78 + i * 2, str(rank[i]), fontdict={'color': '#1C1C1C', 'size': 4}, fontweight='normal')
    ax.text(.75, 0.78 + i * 2, str(team[i]), fontdict={'color': '#1C1C1C', 'size': 4}, fontweight='heavy')
    ax.text(4.05, 0.75 + i * 2, str(OFF[i]), fontdict={'color': '#1C1C1C', 'size': 3.6}, fontweight='normal')

在这里插入图片描述

🏳️‍🌈 8. 设置colarbar

x1 = [4.1] * len(rank)
y1 = [1 + i * 2 for i in range(len(rank))]
ax.scatter(x1, y1, c=OFF, s=40, cmap='Greens', alpha=0.8)

在这里插入图片描述

🏳️‍🌈 9. 添加俱乐部flag

在这里插入图片描述

🏳️‍🌈 10. 添加其他数据

在这里插入图片描述

🏳️‍🌈 11. 添加标题、注释

在这里插入图片描述

🏳️‍🌈 12. 在线运行地址

在线运行地址(含全部代码):
https://www.heywhale.com/mw/project/6358ffe4d9319f9326104897


文章首发:微信公众号 Python当打之年,Python编程技巧推送,希望大家可以喜欢。

以上就是本期为大家整理的全部内容了,赶快练习起来吧,原创不易,喜欢的朋友可以点赞、收藏也可以分享注明出处)让更多人知道。

推荐阅读


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Python当打之年

您的鼓励是我创作的最大动力!

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

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

打赏作者

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

抵扣说明:

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

余额充值