首页 > 编程语言 > 详细

python 使用matplotlib.pyplot.bar绘制柱状图

时间:2020-07-10 22:14:15      阅读:72      评论:0      收藏:0      [点我收藏+]

柱状图图绘制

技术分享图片

效果如下:

技术分享图片

 

 电影数据如下图所示:

技术分享图片

1、绘制

  • matplotlib.pyplot.bar(x, width, align=‘center‘, **kwargs)
Parameters:    
x : sequence of scalars.

width : scalar or array-like, optional
柱状图的宽度

align : {‘center’, ‘edge’}, optional, default: ‘center’
Alignment of the bars to the x coordinates:
‘center’: Center the base on the x positions.
‘edge’: Align the left edges of the bars with the x positions.
每个柱状图的位置对齐方式

**kwargs :
color:选择柱状图的颜色

Returns:    
`.BarContainer`
Container with all the bars and optionally errorbars.

代码实现:

# 完成简单的条形图展现不同的电影票房之间的对比
plt.figure(figsize=(20, 8), dpi=80)

# 准备电影的名字以及电影的票房数据
movie_name = [雷神3:诸神黄昏,正义联盟,东方快车谋杀案,寻梦环游记,全球风暴,降魔传,追捕,七十七天,密战,狂兽,其它]
y = [73853,57767,22354,15969,14839,8725,8716,8318,7916,6764,52222]
# 放进横坐标的数字列表
x = range(len(movie_name))

# 画出条形图
plt.bar(x, y, width=0.5, color=[b,r,g,y,c,m,y,k,c,g,g])

# 修改刻度名称
plt.xticks(x, movie_name)

plt.show()

3、比较相同天数的票房

# 三部电影的首日和首周票房对比
plt.figure(figsize=(20, 8), dpi=80)

movie_name = [雷神3:诸神黄昏,正义联盟,寻梦环游记]

first_day = [10587.6,10062.5,1275.7]
first_weekend=[36224.9,34479.6,11830]

x = range(len(movie_name))

# 画出柱状图
plt.bar(x, first_day, width=0.2, label="首日票房")
# 首周柱状图显示的位置在首日的位置右边
plt.bar([i+0.2 for i in x], first_weekend, width=0.2, label="首周票房")

# 显示X轴中文,固定在首日和首周的中间位置
plt.xticks([i+0.1 for i in x], movie_name)
plt.legend(loc=best)

plt.show()

 

python 使用matplotlib.pyplot.bar绘制柱状图

原文:https://www.cnblogs.com/caijunchao/p/13281458.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!