matplotlib绘图【条形图】
程序员文章站
2022-06-01 18:37:59
...
2019年票房前20电影的票房成绩条形图
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname="/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc",
size="x-small")
x = ['哪吒之魔童降世', '流浪地球', '复仇者联盟4\n:终局之战', '我和我的祖国', '中国机长', '疯狂的外星人', '飞驰人生', '烈火英雄',
'少年的你', '速度与激情\n:特别行动', '蜘蛛侠\n:英雄远征', '扫毒2天地对决', '大黄蜂', '攀登者', '惊奇队长', '比悲伤更悲伤的故事',
'哥斯拉2\n:怪兽之王', '阿丽塔\n:战斗天使', '银河补习班', '狮子王']
y = [49.34, 46.18, 42.05, 29.09, 28.38, 21.83, 17.03, 16.76, 14.73, 14.18, 14.01, 12.85, 11.28, 10.7, 10.25, 9.46, 9.27,
8.88, 8.64, 8.23]
# 设置图形大小
plt.figure(figsize=(20, 15), dpi=80)
# 设置字符串到x轴
plt.xticks(range(len(x)), x, fontproperties=my_font, rotation=90)
# 绘制条形图
plt.bar(range(len(x)), y, width=0.3)
# 展示图片
plt.show()
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname="/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc",
size="x-small")
x = ['哪吒之魔童降世', '流浪地球', '复仇者联盟4\n:终局之战', '我和我的祖国', '中国机长', '疯狂的外星人', '飞驰人生', '烈火英雄',
'少年的你', '速度与激情\n:特别行动', '蜘蛛侠\n:英雄远征', '扫毒2天地对决', '大黄蜂', '攀登者', '惊奇队长', '比悲伤更悲伤的故事',
'哥斯拉2\n:怪兽之王', '阿丽塔\n:战斗天使', '银河补习班', '狮子王']
y = [49.34, 46.18, 42.05, 29.09, 28.38, 21.83, 17.03, 16.76, 14.73, 14.18, 14.01, 12.85, 11.28, 10.7, 10.25, 9.46, 9.27,
8.88, 8.64, 8.23]
# 设置图形大小
plt.figure(figsize=(20, 8), dpi=80)
# 设置字符串到x轴
plt.yticks(range(len(x)), x, fontproperties=my_font)
# 绘制条形图 height:条形图宽度
plt.barh(range(len(x)), y, height=0.3, color="orange")
# 绘制网格
plt.grid(alpha=0.3)
# 展示图片
plt.show()