例如我们要统计和国获取的金牌,银牌、铜牌的数据
统计主要参赛国家的奖牌数据,因此每个坐标上其实要画三个柱图,只不过每个柱图的篇移要重新计算。
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()
构造奖牌数据
bronzes = np.array([38, 17, 26, 19, 15])
silvers = np.array([37, 23, 18, 18, 10])
golds = np.array([46, 27, 26, 19, 17])
计算底部偏 移量 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()
最后是运行视频
视频加载中...
特别声明
本文仅代表作者观点,不代表本站立场,本站仅提供信息存储服务。