Python100天55: bar函数统计奥运会各国奖牌数据

例如我们要统计和国获取的金牌,银牌、铜牌的数据



统计主要参赛国家的奖牌数据,因此每个坐标上其实要画三个柱图,只不过每个柱图的篇移要重新计算。

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

countries = ['USA', 'GB', 'China', 'Russia', 'Germany']
bronzes = np.array([38, 17, 26, 19, 15])
silvers = np.array([37, 23, 18, 18, 10])
golds = np.array([46, 27, 26, 19, 17])
ind = [x for x, _ in enumerate(countries)]

plt.bar(ind, golds, width=0.8, label='golds', color='gold', bottom=silvers+bronzes)
plt.bar(ind, silvers, width=0.8, label='silvers', color='silver', bottom=bronzes)
plt.bar(ind, bronzes, width=0.8, label='bronzes', color='#CD853F')
 
plt.xticks(ind, countries)
plt.ylabel("Medals")
plt.xlabel("Countries")
plt.legend(loc="upper right")
plt.title("2012 Scorers")

plt.show()


构造奖牌数据


计算底部偏 移量 bottomc参数实际等于前面两个奖牌的数据之和,依此类推

plt.bar(ind, golds, width=0.8, label='golds', color='gold', bottom=silvers+bronzes)
plt.bar(ind, silvers, width=0.8, label='silvers', color='silver', bottom=bronzes)


如何将X方向的刻度 显示为标题

plt.xticks(ind, countries)

仔细发现横向,纵向的标签字体有点小,修改一下


对比一下label的字体大小

'''
Created on 2023年1月2日
@author: admin
'''
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

countries = ['USA', 'GB', 'China', 'Russia', 'Germany']
bronzes = np.array([38, 17, 26, 19, 15])
silvers = np.array([37, 23, 18, 18, 10])
golds = np.array([46, 27, 26, 19, 17])
ind = [x for x, _ in enumerate(countries)]
plt.bar(ind, golds, width=0.8, label='golds', color='gold', bottom=silvers+bronzes)
plt.bar(ind, silvers, width=0.8, label='silvers', color='silver', bottom=bronzes)
plt.bar(ind, bronzes, width=0.8, label='bronzes', color='#CD853F')

font2 = {'family' : 'Times New Roman',
    'weight' : 'normal',
    'size' : 28,
}
plt.xticks(ind, countries)
plt.ylabel("Medals",font2)
plt.xlabel("Countries",font2)

plt.legend(loc="upper right")

plt.title("2012 Scorers")
plt.show()


最后是运行视频

视频加载中...

展开阅读全文

页面更新:2024-04-23

标签:标上   奖牌   数据   依此类推   刻度   铜牌   之和   银牌   纵向   函数   奥运会   视频

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top