python 画柱状图 数据精确度_Python数据分析与挖掘(五)---matplotlib柱状图
柱状图(bar)
1 柱状图绘制
需求1-对比每部电影的票房收入
电影数据如下图所示:
1 准备数据
【神族三部曲:终章
[73853,57767,22354,15969,14839,8725,8716,8318,7916,6764,52222]
2 绘制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.
代码:
1)准备数据
电影名字
movies := ['The Marvels:Omega variant','The Avengers','Theagn of the Ring','Theagn of the Ring: The Second Age','Global Tsunami','Demonic Host','Chase','Seventy-seven Days (1976),'Midnight War (1999),'Rogue Element (1985),'Other']
横坐标
x = range(len(movie_name))
票房数据
y = [73853,57767,22354,15969,14839,8725,8716,8318,7916,6764,52222]
2)创建画布
plt.figure(figsize=(20, 8), dpi=100)
3)绘制柱状图
plt.bar(x, y, width=0.5, color=['b','r','g','y','c','m','y','k','c','g','b'])
修改x轴的刻度显示
plt.xticks(x, movie_name)
添加网格显示
plt.grid(linestyle="--", alpha=0.5)
添加标题
plt.title("电影票房收入对比")
4)显示图像
plt.show()
需求2-如何对比电影票房收入才更能加有说服力?
比较相同天数的票房
有时候为了公平起见,我们需要对比不同电影首日和首周的票房
效果如下:
1 准备数据
movie_name = ['雷神3:诸神黄昏','正义联盟','寻梦环游记']
first_day = [10587.6,10062.5,1275.7]
first_weekend=[36224.9,34479.6,11830]
数据来源: https://piaofang.maoyan.com/?ver=normal
2 绘制添加首日首周两部分的柱状图
x轴中文坐标位置调整
代码:
1)准备数据
movie_name = ['雷神3:诸神黄昏','正义联盟','寻梦环游记']
first_day = [10587.6,10062.5,1275.7]
first_weekend=[36224.9,34479.6,11830]
x = range(len(movie_name))
2)创建画布
plt.figure(figsize=(20, 8), dpi=100)
3)绘制柱状图
plt.bar(x, first_day, width=0.2, label="首日票房")
plt.bar([i+0.2 for i in x], first_weekend, width=0.2, label="首周票房")
显示图例
plt.legend()
修改x轴刻度显示
plt.xticks([i+0.1 for i in x], movie_name)
4)显示图像
plt.show()
