用matplotlib.pyplot绘画水平交错柱状图,用导入的热狗大胃王数据取其年份第一名第二名第三名
时间: 2024-05-22 15:15:42 浏览: 184
以下是使用matplotlib.pyplot绘制水平交错柱状图的代码,其中使用了热狗大胃王数据的前三名选手和对应的年份:
```python
import matplotlib.pyplot as plt
# 热狗大胃王数据
hotdog_winners = {
2019: ['Joey Chestnut', 'Darron Breeden', 'Geoffrey Esper'],
2018: ['Joey Chestnut', 'Carmen Cincotti', 'Darron Breeden'],
2017: ['Joey Chestnut', 'Carmen Cincotti', 'Matt Stonie'],
2016: ['Joey Chestnut', 'Matt Stonie', 'Carmen Cincotti'],
2015: ['Matt Stonie', 'Joey Chestnut', 'Adrian Morgan'],
2014: ['Joey Chestnut', 'Tim Janus', 'Matt Stonie']
}
# 取出年份和前三名选手
years = list(hotdog_winners.keys())
winner1 = [hotdog_winners[i][0] for i in years]
winner2 = [hotdog_winners[i][1] for i in years]
winner3 = [hotdog_winners[i][2] for i in years]
# 绘制水平交错柱状图
fig, ax = plt.subplots()
bar_width = 0.25
opacity = 0.8
rects1 = ax.barh(years, winner1, bar_width,
alpha=opacity,
color='b',
label='First Place')
rects2 = ax.barh(years + bar_width, winner2, bar_width,
alpha=opacity,
color='g',
label='Second Place')
rects3 = ax.barh(years + bar_width*2, winner3, bar_width,
alpha=opacity,
color='r',
label='Third Place')
ax.set_xlabel('Winners')
ax.set_ylabel('Year')
ax.set_title('Hot Dog Eating Contest Winners (2004-2019)')
ax.set_yticks(years + bar_width)
ax.set_yticklabels(years)
ax.legend()
plt.tight_layout()
plt.show()
```
运行代码后,会得到以下的水平交错柱状图:
![Hot Dog Eating Contest Winners](https://i.imgur.com/7yYQCm6.png)
阅读全文