柱状图(bar)| 学习笔记

简介: 快速学习柱状图(bar)

开发者学堂课程【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)用户数量对比分析

相关文章
|
12月前
172Echarts - 象形柱图(Dotted bar)
172Echarts - 象形柱图(Dotted bar)
41 0
172Echarts - 象形柱图(Dotted bar)
|
6天前
|
数据可视化 索引 Python
Tips:Matplotlib中柱状图Bar显示数值
Tips:Matplotlib中柱状图Bar显示数值
13 0
|
4月前
|
计算机视觉
Echarts饼图,自定义饼图图例的排列方式, formatter使用语法
Echarts饼图,自定义饼图图例的排列方式, formatter使用语法
|
5月前
|
数据可视化 数据处理
灯芯柱状图代码解读
灯芯柱状图代码解读
60 0
|
12月前
234Echarts - 3D 柱状图(Bar3D - Simplex Noise)
234Echarts - 3D 柱状图(Bar3D - Simplex Noise)
73 0
234Echarts - 3D 柱状图(Bar3D - Simplex Noise)
|
12月前
239Echarts - 3D 柱状图(Stacked Bar3D)
239Echarts - 3D 柱状图(Stacked Bar3D)
95 0
|
12月前
40Echarts - 柱状图(Bar Simple)
40Echarts - 柱状图(Bar Simple)
30 0
|
12月前
32Echarts - 柱状图(Bar Label Rotation)
32Echarts - 柱状图(Bar Label Rotation)
48 0
|
12月前
228Echarts - 3D 柱状图(3D Bar with Dataset)
228Echarts - 3D 柱状图(3D Bar with Dataset)
63 0
|
12月前
233Echarts - 3D 柱状图(Bar3D - Punch Card)
233Echarts - 3D 柱状图(Bar3D - Punch Card)
77 0