导读:柱状图是当前应用最广泛的图表之一,你几乎每天都可以在电子产品上看到它。它有哪些分类?可以展示哪些数据关系?怎样用Python绘制?本文带你逐一了解。
作者:屈希峰,资深Python工程师,知乎多个专栏作者
来源:大数据DT(ID:hzdashuju)
01 概述
柱状图(Histogram)是一种以长方形的长度为变量的表达图形的统计报告图,由一系列高度不等的纵向条纹表示数据分布的情况,用来比较两个或两个以上的价值(不同时间或者不同条件),只有一个变量,通常用于较小的数据集分析。
柱状图也可横向排列,或用多维方式表达。其主要用于数据统计与分析,早期主要用于数学统计学科中,用柱状图表示数码相机的曝光值,到现代使用已经比较广泛,比如现代的电子产品和一些软件的分析测试,如电脑、数码相机的显示器和Photoshop上都能看到相应的柱状图。
1. 基础柱状图
基础柱状图经常用来对比数值的大小,使用范围非常广泛,例如科比在不同赛季的得分、不同游戏App下载量、不同时期手机端综合搜索用户规模等,图2-33显示不同种类水果的销量。
▲图2-33 基本柱状图
需要注意的是,分类太多不适合使用竖向柱状图,如图2-34所示。
▲图2-34 竖向柱状图
此时,需要用到横向柱状图,如图2-35所示。
▲图2-35 横向柱状图
2. 分组柱状图
分组柱状图,又叫聚合柱状图。当使用者需要在同一个轴上显示各个分类下不同的分组时,需要用到分组柱状图。
跟柱状图类似,使用柱子的高度来映射和对比数据值。每个分组中的柱子使用不同颜色或者相同颜色不同透明的方式区别各个分类,各个分组之间需要保持间隔。
分组柱状图经常用于不同组间数据的比较,这些组都包含了相同分类的数据。例如,展示改革开放以来城镇与农村人口的变化,不同游戏公司的休闲、益智、格斗类App的下载量对比等。图2-36对比了2015—2017年间不同水果的销量。
▲图2-36 分组柱状图
3. 堆叠柱状图
与并排显示分类的分组柱状图不同,堆叠柱状图将每个柱子进行分割以显示相同类型下各个数据的大小情况。它可以形象地展示一个大分类包含的每个小分类的数据,以及各个小分类的占比,显示的是单个项目与整体之间的关系。我们将堆叠柱状图分为两种类型:
1)一般的堆叠柱状图:每一根柱子上的值分别代表不同的数据大小,各层的数据总和代表整根柱子的高度。非常适用于比较每个分组的数据总量。
2)百分比的堆叠柱状图:柱子的各个层代表的是该类别数据占该分组总体数据的百分比。
堆叠柱状图的一个缺点是当柱子上的堆叠太多时会导致数据很难区分对比,同时很难对比不同分类下相同维度的数据,因为它们不是按照同一基准线对齐的。
图2-37是显示2015—2017年间不同水果的累计数量。
▲图2-37 堆叠柱状图
4. 双向柱状图
双向柱状图,又名正负条形图,使用正向和反向的柱子显示类别之间的数值比较。其中分类轴表示需要对比的分类维度,连续轴代表相应的数值,分为两种情况,一种是正向刻度值与反向刻度值完全对称,另一种是正向刻度值与反向刻度值反向对称,即互为相反数。
图2-38是显示2015—2017年间不同水果的进出口数量。
▲图2-38 双向柱状图
5. 瀑布图
瀑布图是由麦肯锡顾问公司所独创的图表类型,因为形似瀑布流水而称之为瀑布图(Waterfall Plot)。此种图表采用绝对值与相对值结合的方式,适用于表达数个特定数值之间的数量变化关系。图2-39显示历年短跑冠军的时间跨度,由此可以看出人类体能极限越来越高了。
▲图2-39 瀑布图
接下来,我们看看如何用Bokeh依次实现这些柱状图。
02 实例
柱状图代码示例如下所示。
p = figure(plot_width=400, plot_height=400)
p.vbar(x=[1, 2, 3], width=0.5, bottom=0,
top=[1.2, 2.5, 3.7], color="red") # 垂直柱状图
show(p) # 显示
运行结果如图2-40所示。
▲图2-40 代码示例2-27运行结果
代码示例2-27第2行采用vbar()方法实现垂直柱状图,该方法具体的参数说明如下。
p.vbar(x, width, top, bottom=0, **kwargs)参数说明。
p = figure(plot_width=400, plot_height=400)
p.hbar(y=[1, 2, 3], height=0.5, left=0,
right=[1.2, 2.5, 3.7], color="navy") # 水平柱状图
show(p) # 显示
运行结果如图2-41所示。
▲图2-41 代码示例2-28运行结果
代码示例2-28第2行采用hbar()方法实现横向柱状图,该方法具体的参数说明如下。
p.hbar(y, height, right, left=0, **kwargs)参数说明。
from bokeh.models import ColumnDataSource
from bokeh.palettes import Spectral6
import pandas as pd
df=pd.read_csv('data/visualization-20190505.csv')
p = figure(x_range=df['Visualization_tools'],title="2019年5月常见可视化工具源码GitHub标星数量")
p.vbar(x=df['Visualization_tools'], top=df['Star'] , width=0.8, color=Spectral6)
p.xgrid.grid_line_color = None
p.y_range.start = 0
show(p)
运行结果如图2-42所示。
▲图2-42 代码示例2-29运行结果
代码示例2-29第6行采用vbar()方法展示集中可视化开源工具在GitHub上的Stars数,可以看出Bokeh已经超过了Matplotlib。
# 数据
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
counts = [5, 3, 4, 2, 4, 6]
# 画布
p = figure(x_range=fruits, plot_height=350, title="Fruit Counts",
# toolbar_location=None,
# tools=""
)
# 柱状图
p.vbar(x=fruits, top=counts, width=0.9)
# 坐标轴设置
p.xgrid.grid_line_color = None
p.y_range.start = 0
# 显示
show(p)
运行结果如图2-43所示。
▲图2-43 代码示例2-30运行结果
代码示例2-30第10行采用vbar()方法展示了几种水果的销量。
# 数据
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
counts = [5, 3, 4, 2, 4, 6]
# 排序
sorted_fruits = sorted(fruits, key=lambda x: counts[fruits.index(x)])
# 画布
p = figure(x_range=sorted_fruits, plot_height=350, title="Fruit Counts",
# toolbar_location=None, tools=""
)
# 绘图
p.vbar(x=fruits, top=counts, width=0.9)
# 其他
p.xgrid.grid_line_color = None
p.y_range.start = 0
# 显示
show(p)
运行结果如图2-44所示。
▲图2-44 代码示例2-31运行结果
代码示例2-31第5行先用sorted()方法对原始数据进行排序;然后在第11行采用vbar()方法展示了几种水果的销量。
from bokeh.models import ColumnDataSource
from bokeh.palettes import Spectral6
from bokeh.transform import factor_cmap
# 数据
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
counts = [5, 3, 4, 2, 4, 6]
source = ColumnDataSource(data=dict(fruits=fruits, counts=counts))
# 画布
p = figure(x_range=fruits, plot_height=350, toolbar_location=None, title="Fruit Counts")
# 绘图,分组颜色映射
p.vbar(x='fruits', top='counts', width=0.9, source=source, legend="fruits",
line_color='white', fill_color=factor_cmap('fruits', palette=Spectral6, factors=fruits))
# 坐标轴、图例设置
p.xgrid.grid_line_color = None
p.y_range.start = 0
p.y_range.end = 9
p.legend.orientation = "horizontal"
p.legend.location = "top_center"
show(p)
运行结果如图2-45所示。
▲图2-45 代码示例2-32运行结果
代码示例2-32第11行采用vbar()方法展示了几种水果的销量,其中line_color、fill_color分别为柱的轮廓线颜色和填充颜色,factor_cmap采用数据分类进行颜色映射。在代码实例2-27中,也可以通过color直接定义颜色列表。
from bokeh.models import ColumnDataSource
from bokeh.palettes import Spectral6 # ['#3288bd', '#99d594', '#e6f598', '#fee08b', '#fc8d59', '#d53e4f']
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
counts = [5, 3, 4, 2, 4, 6]
source = ColumnDataSource(data=dict(fruits=fruits, counts=counts, color=Spectral6))
p = figure(x_range=(0,9), y_range=fruits, plot_height=250, title="Fruit Counts",
# toolbar_location=None, tools=""
)
p.hbar(y='fruits',left=0,right='counts', height=0.5 ,color='color', legend="fruits", source=source)
p.xgrid.grid_line_color = None
# p.legend.orientation = "horizontal"
p.legend.location = "top_right"
show(p)
运行结果如图2-46所示。
代码示例2-33第9行采用hbar()方法展示了几种水果的销量,并使用color直接定义颜色列表。
▲图2-46 代码示例2-33运行结果
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ['2015', '2016', '2017']
data = {'fruits' : fruits,
'2015' : [2, 1, 4, 3, 2, 4],
'2016' : [5, 3, 3, 2, 4, 6],
'2017' : [3, 2, 4, 4, 5, 3]}
# 创建复合列表 [ ("Apples", "2015"), ("Apples", "2016"), ("Apples", "2017"), ("Pears", "2015), ... ]
x = [ (fruit, year) for fruit in fruits for year in years ]
counts = sum(zip(data['2015'], data['2016'], data['2017']), ()) # 分组求和(堆叠总数)
source = ColumnDataSource(data=dict(x=x, counts=counts))
# 画布
p = figure(x_range=FactorRange(*x), plot_height=350, title="Fruit Counts by Year",
# toolbar_location=None, tools=""
)
# 柱状图
p.vbar(x='x', top='counts', width=0.9, source=source)
# 其他
p.y_range.start = 0
p.x_range.range_padding = 0.1
p.xaxis.major_label_orientation = 1
p.xgrid.grid_line_color = None
# 显示
show(p)
运行结果如图2-47所示。
代码示例2-34第8、9行数据预处理,读者可以打印数据格式;笔者建议在实践中多采用Pandas进行数据预处理,其DataFrames的复合序列可以直接作为分组柱状图的数据。
▲图2-47 代码示例2-34运行结果
# 数据
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ['2015', '2016', '2017']
data = {'fruits' : fruits,
'2015' : [2, 1, 4, 3, 2, 4],
'2016' : [5, 3, 3, 2, 4, 6],
'2017' : [3, 2, 4, 4, 5, 3]}
palette = ["#c9d9d3", "#718dbf", "#e84d60"]
x = [ (fruit, year) for fruit in fruits for year in years ]
counts = sum(zip(data['2015'], data['2016'], data['2017']), ()) # like an hstack
source = ColumnDataSource(data=dict(x=x, counts=counts))
# 画布
p = figure(x_range=FactorRange(*x), plot_height=350, title="Fruit Counts by Year",
# toolbar_location=None, tools=""
)
# 绘图
p.vbar(x='x', top='counts', width=0.9, source=source, line_color="white",
fill_color=factor_cmap('x', palette=palette, factors=years, start=1, end=2))
# 其他
p.y_range.start = 0
p.x_range.range_padding = 0.1
p.xaxis.major_label_orientation = 1
p.xgrid.grid_line_color = None
# 显示
show(p)
运行结果如图2-48所示。
▲图2-48 代码示例2-35运行结果
代码示例2-35在代码示例2-33的基础上增加了柱状图颜色(第18行),factor_cmap方法是将色板对应的颜色列表映射到相应的分类数据上。
from bokeh.core.properties import value
from bokeh.transform import dodge
# 数据
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ['2015', '2016', '2017']
data = {'fruits' : fruits,
'2015' : [2, 1, 4, 3, 2, 4],
'2016' : [5, 3, 3, 2, 4, 6],
'2017' : [3, 2, 4, 4, 5, 3]}
source = ColumnDataSource(data=data)
# 画布
p = figure(x_range=fruits, y_range=(0, 10), plot_height=350, title="Fruit Counts by Year",
# toolbar_location=None, tools=""
)
# 绘图,采用doge数据转换,按产品种类不同年份分组显示
p.vbar(x=dodge('fruits', -0.25, range=p.x_range), top='2015', width=0.2, source=source,
color="#c9d9d3", legend=value("2015"))
p.vbar(x=dodge('fruits', 0.0, range=p.x_range), top='2016', width=0.2, source=source,
color="#718dbf", legend=value("2016"))
p.vbar(x=dodge('fruits', 0.25, range=p.x_range), top='2017', width=0.2, source=source,
color="#e84d60", legend=value("2017"))
# 其他参数设置
p.x_range.range_padding = 0.1
p.xgrid.grid_line_color = None
p.legend.location = "top_left"
p.legend.orientation = "horizontal"
# 显示
show(p)
运行结果如图2-49所示。
▲图2-49 代码示例2-36运行结果
代码示例2-36第16、19、22使用vbar()方法分别绘制2015—2017年各种水果的销量;其中dodge方法按每年不同种类水果的数据分散绘制在x轴范围内,是将色板对应的颜色列表映射到相应的分类数据上,dodge第二个参数表示该分类的起始绘制点。
from bokeh.core.properties import value
# 数据
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ["2015", "2016", "2017"]
colors = ["#c9d9d3", "#718dbf", "#e84d60"]
data = {'fruits' : fruits,
'2015' : [2, 1, 4, 3, 2, 4],
'2016' : [5, 3, 4, 2, 4, 6],
'2017' : [3, 2, 4, 4, 5, 3]}
# 画布
p = figure(x_range=fruits, plot_height=250, title="Fruit Counts by Year",
# toolbar_location=None,
# tools="hover",
tooltips="$name @fruits: @$name")
# 绘图,直接堆叠各年数据
p.vbar_stack(years, x='fruits', width=0.9, color=colors, source=data,
legend=[value(x) for x in years]) # legend=[{'value': '2015'}, {'value': '2016'}, {'value': '2017'}]
# 其他
p.y_range.start = 0
p.x_range.range_padding = 0.1
p.xgrid.grid_line_color = None
p.axis.minor_tick_line_color = None
p.outline_line_color = None
p.legend.location = "top_left"
p.legend.orientation = "horizontal"
# 显示
show(p)
运行结果如图2-50所示。
▲图2-50 代码示例2-37运行结果
代码示例2-37第16行使用vbar_stack()方法实现竖向堆叠柱状图,该方法具体的参数说明如下。
p.vbar_stack(stackers, **kw)参数说明。
其他参数基本上同vbar()方法。
from bokeh.models import Legend
p = figure(y_range=fruits, plot_height=250,title="Fruit Counts by Year",
# toolbar_location=None, tools=""
)
source = ColumnDataSource(data=data)
p.hbar_stack(years, y='fruits',height=0.8, color=colors, source=source,
legend=[value(x) for x in years] ) # 堆叠柱状图,逐年堆叠
p.x_range.start = 0
p.y_range.range_padding = 0.1 # x轴两侧空白
p.ygrid.grid_line_color = None
p.axis.minor_tick_line_color = None
p.outline_line_color = None
p.legend.location = "top_right"
# p.legend.orientation = "horizontal"
p.legend.click_policy="hide"
show(p)
运行结果如图2-51所示。
▲图2-51 代码示例2-38运行结果
代码示例2-38第6行使用hbar_stack()方法实现横向堆叠柱状图,该方法具体的参数说明如下。
p.hbar_stack(stackers, **kw)参数说明。
其他参数基本上同vbar()方法。
在学习或时间过程中,图例可能遮盖图表,此时可以将图例移到坐标轴外或单独作为一个图层。
from bokeh.palettes import Spectral5
from bokeh.sampledata.autompg import autompg as df
from bokeh.transform import factor_cmap
# 数据,预处理
df.cyl = df.cyl.astype(str)
group = df.groupby('cyl')
cyl_cmap = factor_cmap('cyl', palette=Spectral5, factors=sorted(df.cyl.unique())) # 分组颜色映射
# 画布
p = figure(plot_height=350, x_range=group, title="MPG by # Cylinders",
# toolbar_location=None, tools=""
)
# 绘图
p.vbar(x='cyl', top='mpg_mean', width=0.9, source=group,
line_color=cyl_cmap, fill_color=cyl_cmap)
# 其他
p.y_range.start = 0
p.xgrid.grid_line_color = None
p.xaxis.axis_label = "some stuff"
p.xaxis.major_label_orientation = 1.2
p.outline_line_color = None
# 显示
show(p)
运行结果如图2-52所示。
▲图2-52 代码示例2-39运行结果
代码示例2-39第13行使用vbar()用柱状图展示了汽车缸数与每加仑汽油能行驶的英里数之间的关系。
from bokeh.sampledata.autompg import autompg_clean as df
df.cyl = df.cyl.astype(str)
df.yr = df.yr.astype(str)
group = df.groupby(['cyl', 'mfr']) # 复合条件分组,[缸数、厂家]
index_cmap = factor_cmap('cyl_mfr', palette=Spectral5, factors=sorted(df.cyl.unique()), end=1)
# 画布
p = figure(plot_width=800, plot_height=300, title="Mean MPG by # Cylinders and Manufacturer",
x_range=group, tooltips=[("MPG", "@mpg_mean"), ("Cyl, Mfr", "@cyl_mfr")])
# 绘图
p.vbar(x='cyl_mfr', top='mpg_mean', width=1, source=group,
line_color="white", fill_color=index_cmap, ) # 尾气排放量均值
# 其他
p.y_range.start = 0
p.x_range.range_padding = 0.05 # 同css中的padding
p.xgrid.grid_line_color = None
p.xaxis.axis_label = "Manufacturer grouped by # Cylinders"
p.xaxis.major_label_orientation = 1.2 # x轴标签旋转
p.outline_line_color = None
# 显示
show(p)
运行结果如图2-53所示。
▲图2-53 代码示例2-40运行结果
代码示例2-40第10行使用vbar()绘制分组柱状图,数据分组采用Pandas的groupby方法,该数据为复合序列,展示了汽车缸数与每加仑汽油能行驶的英里数之间的关系。
# 数据
from bokeh.sampledata.sprint import sprint
sprint.Year = sprint.Year.astype(str)
group = sprint.groupby('Year')
source = ColumnDataSource(group)
# 画布
p = figure(y_range=group, x_range=(9.5,12.7), plot_width=400, plot_height=550,
# toolbar_location=None,
title="Time Spreads for Sprint Medalists (by Year)")
# 绘图
p.hbar(y="Year", left='Time_min', right='Time_max', height=0.4, source=source) # 水平柱状图
# 其他
p.ygrid.grid_line_color = None
p.xaxis.axis_label = "Time (seconds)"
p.outline_line_color = None
# 显示
show(p)
运行结果如图2-54所示。
▲图2-54 代码示例2-41运行结果
代码示例2-41第11行使用hbar()绘制瀑布图,参数中left、right为柱左、右坐标。若左侧的起始坐标均为某一定值,则变回横向柱状图。
from bokeh.core.properties import value
from bokeh.models import ColumnDataSource, FactorRange
# 数据
factors = [
("Q1", "jan"), ("Q1", "feb"), ("Q1", "mar"),
("Q2", "apr"), ("Q2", "may"), ("Q2", "jun"),
("Q3", "jul"), ("Q3", "aug"), ("Q3", "sep"),
("Q4", "oct"), ("Q4", "nov"), ("Q4", "dec"),
]
regions = ['east', 'west']
source = ColumnDataSource(data=dict(
x=factors,
east=[ 5, 5, 6, 5, 5, 4, 5, 6, 7, 8, 6, 9 ],
west=[ 5, 7, 9, 4, 5, 4, 7, 7, 7, 6, 6, 7 ],
))
# 画布
p = figure(x_range=FactorRange(*factors), plot_height=250,
# toolbar_location=None, tools=""
)
# 绘图
p.vbar_stack(regions, x='x', width=0.9, alpha=0.5, color=["blue", "red"], source=source,
legend=[value(x) for x in regions])
# 其他
p.y_range.start = 0
p.y_range.end = 18
p.x_range.range_padding = 0.1
p.xaxis.major_label_orientation = 1
p.xgrid.grid_line_color = None
p.legend.location = "top_center"
p.legend.orientation = "horizontal"
# 显示
show(p)
运行结果如图2-55所示。
▲图2-55 代码示例2-42运行结果
代码示例2-42第18行使用FactorRange ()方法预定义x轴的范围(factors的数据格式与Pandas复合序列相似);第19行绘制竖向堆叠柱状图。与常规竖向堆叠柱状图相比,该图采用了复合序列,多展示了一个维度。
from bokeh.models import ColumnDataSource
from bokeh.palettes import GnBu3, OrRd3
# 数据
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ["2015", "2016", "2017"]
exports = {'fruits' : fruits,
'2015' : [2, 1, 4, 3, 2, 4],
'2016' : [5, 3, 4, 2, 4, 6],
'2017' : [3, 2, 4, 4, 5, 3]}
imports = {'fruits' : fruits,
'2015' : [-1, 0, -1, -3, -2, -1],
'2016' : [-2, -1, -3, -1, -2, -2],
'2017' : [-1, -2, -1, 0, -2, -2]}
# 画布
p = figure(y_range=fruits, plot_height=350, x_range=(-16, 16), title="Fruit import/export, by year",
# toolbar_location=None
)
# 水平堆积柱状图出口(正向)
p.hbar_stack(years, y='fruits', height=0.9, color=GnBu3, source=ColumnDataSource(exports),
legend=["%s exports" % x for x in years])
# 水平堆积柱状图进口(负向)
p.hbar_stack(years, y='fruits', height=0.9, color=OrRd3, source=ColumnDataSource(imports),
legend=["%s imports" % x for x in years])
# 其他
p.y_range.range_padding = 0.1
p.ygrid.grid_line_color = None
p.legend.location = "top_left"
p.axis.minor_tick_line_color = None
p.outline_line_color = None
# 显示
show(p)
运行结果如图2-56所示。
代码示例2-43第19、22行分别使用hbar_stack ()方法向左、右两个方向绘制,实现横向堆叠柱状图;注意,当y轴为分类数据(字符串)时,一般需要预先定义y_range。笔者在实践中习惯用该图,不受纵向长度约束,适合数据较多的长图,例如全国各省某类型数据的比较。
▲图2-56 代码示例2-43运行结果
from bokeh.models import FactorRange
factors = [
("Q1", "jan"), ("Q1", "feb"), ("Q1", "mar"),
("Q2", "apr"), ("Q2", "may"), ("Q2", "jun"),
("Q3", "jul"), ("Q3", "aug"), ("Q3", "sep"),
("Q4", "oct"), ("Q4", "nov"), ("Q4", "dec"),
] # 复合数列
p = figure(x_range=FactorRange(*factors), plot_height=350,
# toolbar_location=None, tools=""
) # 如果不采用ColumnDataSource,就必须预定义factors
x = [ 10, 12, 16, 9, 10, 8, 12, 13, 14, 14, 12, 16 ]
# 水平柱状图
p.vbar(x=factors, top=x, width=0.9, alpha=0.5)
# 折线
p.line(x=["Q1", "Q2", "Q3", "Q4"], y=[12, 9, 13, 14], color="red", line_width=2)
# 其他
p.y_range.start = 0
p.x_range.range_padding = 0.1
p.xaxis.major_label_orientation = 1
p.xgrid.grid_line_color = None
# 显示
show(p)
运行结果如图2-57所示。
▲图2-57 代码示例2-44运行结果
关于作者:屈希峰,资深Python工程师,Bokeh领域的实践者和布道者,对Bokeh有深入的研究。擅长Flask、MongoDB、Sklearn等技术,实践经验丰富。知乎多个专栏(Python中文社区、Python程序员、大数据分析挖掘)作者,专栏累计关注用户十余万人。
本文摘编自《Python数据可视化:基于Bokeh的可视化绘图》,经出版方授权发布。
有话要说?
Q: 你在哪些案例中使用柱状图?
欢迎留言与大家分享猜你想看?