且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

柱状图(bar)| 学习笔记

更新时间:2022-09-07 23:22:32

开发者学堂课程【Python 数据可视化库 Matplotlib 快速入门柱状图(bar)学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址https://developer.aliyun.com/learning/course/606/detail/8842


柱状图(bar)


内容介绍

一、学习目标

二、柱状图绘制

三、例题

四、应用场景


一、学习目标

(1)目标

应用 bar 实现柱状图的绘制。知道柱状图的应用场景●应用

电影票房收入绘制

(2)内容预览

2.4.1柱状图绘制

2.4.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 posit ions.

'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、准备数据

movie_ names = [ '雷神3:诸神黄昏','正义联盟','东方快车谋杀案’,,寻梦环游记' , '全球风暴’,,降魔传', '追捕','七十七天','密战' tickets = [73853,57767,22354, 15969,14839,8725,8716,8318, 7916, 6764,52222]

# 2、创建画布

plt.figure(figsize=(20,8),dpi=80)

# 3、绘制柱状图

x_ ticks = range( len (movie_ names) )

plt.bar(x_ ticks, tickets, color=['b','r','g','y','c','m','y','k','c','g','b'])

#修改 x 刻度

plt.xticks(x_ ticks, movie names )

#添加标题

plt. title("电影票房收入对比“)

#添加网格显示

plt.grid(linestyle="--", alpha=0.5)

# 4、显示图像

plt. show( )

需求二:如何对比电影票房收入才更加有说服力?

比较相同天数的票房

需求2-如何对比电影票房收入才更能加有说服力?

#1、准备数据

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

first_ _day = [10587.6,10062.5,1275.7]

first_ weekend= [36224.9,34479.6,118301

#2、创建画布

plt. figure( figsize=(20, 8),dpi=80)

# 3、绘制柱状图

plt. bar(range(3),first_ day, width=0.2, label="首日票房")

plt.bar([0.2,1.2, 2.2], first_ weekend, width=0.2, label=" 首周票房")

#显示图例

plt. legend( )

#修改刻度

plt.xticks([0.1, 1.1, 2.1], movie_ name )

# 4、显示图像

plt. show( )


四、柱状图的应用场景

(1)适合用在分类数据对比场景上

(2)数量统计

(3)用户数量对比分析