Jupyter世界杯夺冠队伍环形图绘制教程

本文将引导您使用Jupyter Notebook和matplotlib库绘制一个展示世界杯主客场夺冠队伍的环形图。

步骤:

  1. 导入库:
import matplotlib.pyplot as plt
  1. 准备数据:

创建两个列表:

  • teams:包含主客场夺冠队伍名称。
  • champion_counts:包含每支队伍的夺冠次数。
teams = ['Brazil', 'Germany', 'Italy', 'Argentina', 'France', 'Uruguay', 'England', 'Spain']
champion_counts = [5, 4, 4, 2, 2, 2, 1, 1]
  1. 创建环形图:
fig, ax = plt.subplots(figsize=(6, 6))  # 创建画布
ax.pie(champion_counts, labels=teams, autopct='%1.1f%%')  # 绘制环形图
ax.set_title('World Cup Champions')  # 设置标题

# 添加比例尺
ax.axis('equal')

# 显示图形
plt.show()

完整代码:

import matplotlib.pyplot as plt

teams = ['Brazil', 'Germany', 'Italy', 'Argentina', 'France', 'Uruguay', 'England', 'Spain']
champion_counts = [5, 4, 4, 2, 2, 2, 1, 1]

fig, ax = plt.subplots(figsize=(6, 6))
ax.pie(champion_counts, labels=teams, autopct='%1.1f%%')
ax.set_title('World Cup Champions')

ax.axis('equal')

plt.show()

运行代码后,您将看到一个环形图,展示了世界杯主客场夺冠队伍及其夺冠次数的比例。

Jupyter世界杯夺冠队伍环形图绘制教程

原文地址: https://www.cveoy.top/t/topic/o4hg 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录