使用matplotlib绘制条形图的步骤其实和绘制散点图差不多,只是调用了不同的方法而已.
那么下面我粘贴上代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| from matplotlib import pyplot as plt from matplotlib import font_manager
myfont = font_manager.FontProperties(fname="/usr/share/fonts/wenquanyi/wqy-zenhei/wqy-zenhei.ttc")
plt.figure(figsize=(20, 7), dpi=60)
a = ["战狼2", "速度与激情8:\n八个压路", "功夫瑜伽", "西游伏魔篇", "变形金刚", "摔跤吧"] b = [56.01, 26.94, 17.53, 16.49, 15.45, 12.96] plt.xticks(range(len(a)), a, fontproperties=myfont) plt.ylabel("热度", fontproperties=myfont, rotation=0) plt.xlabel("电影名", fontproperties=myfont)
plt.bar(range(len(a)), b, width=0.5) plt.show()
|
下面是图片:
还有一种绘制条形图的方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| m matplotlib import pyplot as plt from matplotlib import font_manager
myfont = font_manager.FontProperties(fname="/usr/share/fonts/wenquanyi/wqy-zenhei/wqy-zenhei.ttc")
plt.figure(figsize=(20, 7), dpi=60)
a = ["战狼2", "速度与激情8:\n八个压路", "功夫瑜伽", "西游伏魔篇", "变形金刚", "摔跤吧"] b = [56.01, 26.94, 17.53, 16.49, 15.45, 12.96] plt.yticks(range(len(a)), a, fontproperties=myfont) plt.ylabel("电影名", fontproperties=myfont, rotation=0) plt.xlabel("热度", fontproperties=myfont, rotation=0)
plt.grid(alpha=0.3, color='red')
plt.barh(range(len(a)), b, height=0.8, color="red") plt.show()
|
下面是绘制的图片: