import matplotlib.pyplot as plt
import matplotlib
# 设置全局中文
font = {"family": "Microsoft YaHei"}
matplotlib.rc("font", **font)
a = ["战狼2", "速度与激情8", "功夫瑜伽", "西游伏妖篇", "变形金刚5:最后的骑士", "摔跤吧!爸爸", "加勒比海盗5:死无对证",
"金刚:骷髅岛", "极限特工:终极回归", "生化危机6:终章", "乘风破浪", "神偷奶爸3", "智取威虎山", "大闹天竺", "金刚狼3:殊死一战",
"蜘蛛侠:英雄归来", "悟空传", "银河护卫队2", "情圣", "新木乃伊"]
# 有的电影名过长,故将有冒号的电影名以冒号分隔换行
a_n = []
for str in a:
if ":" not in str:
a_n.append(str)
if ":" in str:
s = str.split(':')
s[1] = '\n' + s[1]
s = s[0] + ':' + s[1]
a_n.append(s)
b = [56.01, 26.94, 17.53, 16.49, 15.45, 12.96, 11.8, 11.61, 11.28, 11.12, 10.49, 10.3, 8.75, 7.55, 7.32, 6.99, 6.88,
6.86, 6.58, 6.23]
plt.figure(figsize=(20 ,8), dpi=80)
plt.xticks(range(len(a)), a_n,rotation=45)
min = int(min(b))
max = int(max(b))
plt.yticks([i for i in range(min, max+1)][::3])
#plt.bar是竖的条形图
plt.bar(range(len(a)), b,color='c)
plt.xlabel('电影名称')
plt.ylabel('票房 /亿元')
plt.grid(alpha=0.3)
plt.show()
Python数据分析之Matplotlib学习系列——绘制条形图01
最新推荐文章于 2024-10-31 09:23:54 发布