柱状图/堆叠图/直方图

① 单系列柱状图
② 多系列柱状图
③ 堆叠图
④ 直方图

1.单系列柱状图

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
% matplotlib inline import warnings
warnings.filterwarnings('ignore')
# 不发出警告 from bokeh.io import output_notebook
output_notebook()
# 导入notebook绘图模块 from bokeh.plotting import figure,show
from bokeh.models import ColumnDataSource
# 导入图表绘制、图标展示模块
# 导入ColumnDataSource模块

p.vbar()  p.hbar()
p.vbar(x=[1, 2, 3], width=0.5, bottom=0,top=[1.2, 2.5, 3.7], #color = ['red','blue','green'], alpha = 0.8 
line_width = 1,line_alpha = 0.8,line_color = 'black', line_dash = [5,2],fill_color = 'red',fill_alpha = 0.6 )
# 1、单系列柱状图
# vbar
df = pd.Series(np.random.randint(0,100,30))
#print(df.head()) 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], # x:横轴坐标,width:宽度,bottom:底高度,top:顶高度 ; bottom=[1,2,3]
#color = ['red','blue','green'], alpha = 0.8 # 整体颜色设置,也可单独设置 → color="firebrick"
line_width = 1,line_alpha = 0.8,line_color = 'black', line_dash = [5,2], # 单独设置线参数
fill_color = 'red',fill_alpha = 0.6 # 单独设置填充颜色参数
)
# 绘制竖向柱状图
# p.vbar(x=df.index, width=0.5, bottom=0,top=df.values, # x:横轴坐标,width:宽度,bottom:底高度,top:顶高度 ; bottom=[1,2,3]
# #color = ['red','blue','green'], alpha = 0.8 # 整体颜色设置,也可单独设置 → color="firebrick"
# line_width = 1,line_alpha = 0.8,line_color = 'black', line_dash = [5,2], # 单独设置线参数
# fill_color = 'red',fill_alpha = 0.6 # 单独设置填充颜色参数
# ) show(p)

# 1、单系列柱状图
# hbar
# df = pd.DataFrame({'value':np.random.randn(100)*10,
# 'color':np.random.choice(['red', 'blue', 'green'], 100)})
# print(df.head())
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], # y:纵轴坐标,height:厚度,left:左边最小值,right:右边最大值
color = ['red','blue','green'])
# 绘制竖向柱状图 # p.hbar(y=df.index, height=0.5, left=0,right=df['value'], # y:纵轴坐标,height:厚度,left:左边最小值,right:右边最大值
# color = df['color']) show(p)

分类标签的设置

p.vbar(x='fruits', top='counts', source=source, width=0.9, alpha = 0.8,color = factor_cmap('fruits', palette=Spectral6, factors=fruits),
legend="fruits")
# 1、单系列柱状图 - 分类设置标签
# 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))
colors = [ "salmon", "olive", "darkred", "goldenrod", "skyblue", "orange"]
# 创建一个包含标签的data,对象类型为ColumnDataSource p = figure(x_range=fruits, y_range=(0,9), plot_height=350, title="Fruit Counts",tools="") #x_range一开始就要设置成一个字符串的列表;要一一对应 p.vbar(x='fruits', top='counts', source=source, # 加载数据另一个方式
width=0.9, alpha = 0.8,
color = factor_cmap('fruits', palette=Spectral6, factors=fruits), # 设置颜色
legend="fruits")
# 绘制柱状图,横轴直接显示标签
# factor_cmap(field_name, palette, factors, start=0, end=None, nan_color='gray'):颜色转换模块,生成一个颜色转换对象
# field_name:分类名称
# palette:调色盘
# factors:用于在调色盘中分颜色的参数
# 参考文档:http://bokeh.pydata.org/en/latest/docs/reference/transform.html p.xgrid.grid_line_color = None
p.legend.orientation = "horizontal"
p.legend.location = "top_center"
# 其他参数设置
#cmd -->> conda install bokeh ; conda install json
show(p)

2. 多系列柱状图

p.vbar(x=dodge('index', -0.25, range=p.x_range), top='2015', width=0.2, source=source,color="#c9d9d3", legend=value("2015")) #用dodge的方法把3个柱状图拼到了一起
p.vbar(x=dodge('index', 0.0, range=p.x_range), top='2016', width=0.2, source=source,color="#718dbf", legend=value("2016"))
p.vbar(x=dodge('index', 0.25, range=p.x_range), top='2017', width=0.2, source=source,color="#e84d60", legend=value("2017"))
# 2、多系列柱状图
# vbar from bokeh.transform import dodge
from bokeh.core.properties import value
# 导入dodge、value模块 df = pd.DataFrame({'':[2, 1, 4, 3, 2, 4],'':[5, 3, 3, 2, 4, 6], '':[3, 2, 4, 4, 5, 3]},
index = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries'])
# 创建数据
print(df)
fruits = df.index.tolist() # 横坐标
years = df.columns.tolist() # 系列名
data = {'index':fruits}
for year in years:
data[year] = df[year].tolist()
print(data)
# 生成数据,数据格式为dict; 专门把dataframe转换为字典,然后再转换为ColumnDataSource;可以把这些步骤给都省了,也可以的。 source = ColumnDataSource(data=data) #把上面转换字典步骤直接去掉也是可以的,这时把 data = df
# 将数据转化为ColumnDataSource对象 p = figure(x_range=fruits, y_range=(0, 10), plot_height=350, title="Fruit Counts by Year",tools="") p.vbar(x=dodge('index', -0.25, range=p.x_range), top='', width=0.2, source=source,color="#c9d9d3", legend=value("")) #用dodge的方法把3个柱状图拼到了一起
p.vbar(x=dodge('index', 0.0, range=p.x_range), top='', width=0.2, source=source,color="#718dbf", legend=value(""))
p.vbar(x=dodge('index', 0.25, range=p.x_range), top='', width=0.2, source=source,color="#e84d60", legend=value(""))
# 绘制多系列柱状图 0.25和width=0.2是柱状图之间的空隙间隔,都是0.2了就没有空隙了
# dodge(field_name, value, range=None) → 转换成一个可分组的对象,value为元素的位置(配合width设置)
# value(val, transform=None) → 按照年份分为dict p.xgrid.grid_line_color = None
p.legend.location = "top_left"
p.legend.orientation = "horizontal"
# 其他参数设置 show(p)

官方示例很多情况是用的列表的形式,bokeh本身不是基于pandas构建的可视化工具,所以它基本上是用的python自己的数据结构字典、列表;我们做数据分析肯定是基于pandas,以上就是做了一个模拟,如果数据结构是DataFrame,怎么把它变成一个字典,再把它变成一个ColumnDataSource,同样的也可以直接用dataframe来创建

from bokeh.transform import dodge
from bokeh.core.properties import value
# 导入dodge、value模块 df = pd.DataFrame({'':[2, 1, 4, 3, 2, 4],'':[5, 3, 3, 2, 4, 6], '':[3, 2, 4, 4, 5, 3]},
index = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries'])
# # 创建数据
# print(df) source = ColumnDataSource(data=df)
# 将数据转化为ColumnDataSource对象 p = figure(x_range=fruits, y_range=(0, 10), plot_height=350, title="Fruit Counts by Year",tools="") p.vbar(x=dodge('index', -0.25, range=p.x_range), top='', width=0.2, source=source,color="#c9d9d3", legend=value("")) #用dodge的方法把3个柱状图拼到了一起
p.vbar(x=dodge('index', 0.0, range=p.x_range), top='', width=0.2, source=source,color="#718dbf", legend=value(""))
p.vbar(x=dodge('index', 0.25, range=p.x_range), top='', width=0.2, source=source,color="#e84d60", legend=value(""))
# 绘制多系列柱状图 0.25和width=0.2是柱状图之间的空隙间隔,都是0.2了就没有空隙了
# dodge(field_name, value, range=None) → 转换成一个可分组的对象,value为元素的位置(配合width设置)
# value(val, transform=None) → 按照年份分为dict p.xgrid.grid_line_color = None
p.legend.location = "top_left"
p.legend.orientation = "horizontal"
# 其他参数设置 show(p)

    df---->>     data---->>

3. 堆叠图

p.vbar_stack(["2015", "2016", "2017"], # 设置堆叠值,这里source中包含了不同年份的值
x='fruits', # 设置x坐标
source=source, #包含了2015/2016/2017的数据的;
width=0.9, color=colors,
legend=[value(x) for x in years], name=years) #对整个数据做一个分组集合变成一个列表
# 3、堆叠图

from bokeh.core.properties import value
# 导入value模块 fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ["", "", ""]
colors = ["#c9d9d3", "#718dbf", "#e84d60"]
data = {'fruits' : fruits,
'' : [2, 1, 4, 3, 2, 4],
'' : [5, 3, 4, 2, 4, 6],
'' : [3, 2, 4, 4, 5, 3]}
# df = pd.DataFrame(data)
# print(df)
# source = ColumnDataSource(data = df) #也可以使用DataFrame
source = ColumnDataSource(data=data) # 创建数据 p = figure(x_range=fruits, plot_height=350, title="Fruit Counts by Year",tools="")
renderers = p.vbar_stack(["", "", ""], #可以用years代替,就是上边设置的变量 # 设置堆叠值,这里source中包含了不同年份的值,years变量用于识别不同堆叠层
x='fruits', # 设置x坐标
source=source, #包含了2015/2016/2017的数据的; 主要设置的就是这3个参数
width=0.9, color=colors,
legend=[value(x) for x in years], name=years) #对整个数据做一个分组集合变成一个列表
# 绘制堆叠图
# 注意第一个参数需要放years 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)
[value(x) for x in years]

[{'value': ''}, {'value': ''}, {'value': ''}]
# 3、堆叠图

from bokeh.palettes import GnBu3, OrRd3
# 导入颜色模块 fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ["", "", ""]
exports = {'fruits' : fruits,
'' : [2, 1, 4, 3, 2, 4],
'' : [5, 3, 4, 2, 4, 6],
'' : [3, 2, 4, 4, 5, 3]}
imports = {'fruits' : fruits,
'' : [-1, 0, -1, -3, -2, -1],
'' : [-2, -1, -3, -1, -2, -2],
'' : [-1, -2, -1, 0, -2, -2]} p = figure(y_range=fruits, plot_height=350, x_range=(-16, 16), title="Fruit import/export, by year") 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.2 # 调整边界间隔
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)

4. 直方图

hist, edges = np.histogram(df['value'],bins=20) 
p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:],   # 分别代表每个柱子的四边值,上下左右,top=hist计数的值,第一个箱子的左跟右就是第一个值+第二个值
fill_color="#036564", line_color="#033649")
 
# 4、直方图
# np.histogram + figure.quad()
# 不需要构建ColumnDataSource对象 df = pd.DataFrame({'value': np.random.randn(1000)*100})
df.index.name = 'index'
print(df.head())
# 创建数据 hist, edges = np.histogram(df['value'],bins=20) #把这1000个值拆分成20个箱子,整个箱子肯定会有一个value值的也就是它的个数
print(hist) #代表不同箱子的个数
print(edges) #把它分成不同箱子之后,每个箱子的位置坐标
# 将数据解析成直方图统计格式
# 高阶函数np.histogram(a, bins=10, range=None, weights=None, density=None)
# a:数据
# bins:箱数
# range:最大最小值的范围,如果不设定则为(a.min(), a.max())
# weights:权重
# density:为True则返回“频率”,为False则返回“计数”
# 返回值1 - hist:每个箱子的统计值(top)
# 返回值2 - edges:每个箱子的位置坐标,这里n个bins将会有n+1个edges;假如有10个箱子,就会有10+1个位置 p = figure(title="HIST", tools="save",background_fill_color="#E8DDCB")
p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], # 分别代表每个柱子的四边值,上下左右,top=hist计数的值,第一个箱子的左跟右就是第一个值+第二个值
fill_color="#036564", line_color="#033649")
# figure.quad绘制直方图 show(p)
            value
index
0 109.879109
1 -49.315234
2 -149.590185
3 83.290324
4 -16.367743
[ 1 2 2 3 21 25 59 85 104 151 140 110 101 82 47 31 22 8 #这里1就是在-356.79345893到-323.021329之间有1个;在-323.021329到-289.2491919906之间有2个
2 4]
[-356.79345893 -323.021329 -289.24919906 -255.47706913 -221.7049392
-187.93280926 -154.16067933 -120.3885494 -86.61641947 -52.84428953
-19.0721596 14.69997033 48.47210027 82.2442302 116.01636013
149.78849006 183.56062 217.33274993 251.10487986 284.8770098
318.64913973]
In [ ]:

Python交互图表可视化Bokeh:5 柱状图| 堆叠图| 直方图的更多相关文章

  1. Python交互图表可视化Bokeh:4. 折线图| 面积图

    折线图与面积图 ① 单线图.多线图② 面积图.堆叠面积图 1. 折线图--单线图 import numpy as np import pandas as pd import matplotlib.py ...

  2. Python交互图表可视化Bokeh:1. 可视交互化原理| 基本设置

    Bokeh pandas和matplotlib就可以直接出分析的图表了,最基本的出图方式.是面向数据分析过程中出图的工具:Seaborn相比matplotlib封装了一些对数据的组合和识别的功能:用S ...

  3. Python交互图表可视化Bokeh:7. 工具栏

    ToolBar工具栏设置 ① 位置设置② 移动.放大缩小.存储.刷新③ 选择④ 提示框.十字线 1. 位置设置 import numpy as np import pandas as pd impor ...

  4. Python交互图表可视化Bokeh:6. 轴线| 浮动| 多图表

    绘图表达进阶操作 ① 轴线设置② 浮动设置③ 多图表设置 1. 轴线标签设置 设置字符串 import numpy as np import pandas as pd import matplotli ...

  5. Python交互图表可视化Bokeh:3. 散点图

    散点图 ① 基本散点图绘制② 散点图颜色.大小设置方法③ 不同符号的散点图 1. 基本散点图绘制 import numpy as np import pandas as pd import matpl ...

  6. Python交互图表可视化Bokeh:2. 辅助参数

    图表辅助参数设置 辅助标注.注释.矢量箭头 参考官方文档:https://bokeh.pydata.org/en/latest/docs/user_guide/annotations.html#col ...

  7. 06. Matplotlib 2 |折线图| 柱状图| 堆叠图| 面积图| 填图| 饼图| 直方图| 散点图| 极坐标| 图箱型图

    1.基本图表绘制 plt.plot() 图表类别:线形图.柱状图.密度图,以横纵坐标两个维度为主同时可延展出多种其他图表样式 plt.plot(kind='line', ax=None, figsiz ...

  8. Python图表数据可视化Seaborn:2. 分类数据可视化-分类散点图|分布图(箱型图|小提琴图|LV图表)|统计图(柱状图|折线图)

    1. 分类数据可视化 - 分类散点图 stripplot( ) / swarmplot( ) sns.stripplot(x="day",y="total_bill&qu ...

  9. Python 绘图与可视化 matplotlib 制作Gif动图

    参考链接:https://blog.csdn.net/theonegis/article/details/51037850 官方文档:https://matplotlib.org/3.1.0/api/ ...

随机推荐

  1. 洛谷P5072 [Ynoi2015]盼君勿忘 [莫队]

    传送门 辣鸡卡常题目浪费我一下午-- 思路 显然是一道莫队. 假设区间长度为\(len\),\(x\)的出现次数为\(k\),那么\(x\)的贡献就是\(x(2^{len-k}(2^k-1))\),即 ...

  2. vi快速查找

    用vim时,想高亮显示一个单词并查找的方发,将光标移动到所找单词. 1: shift + "*"  向下查找并高亮显示 2: shift + "#"  向上查找 ...

  3. TCP和UDP的对比

    UDP #面向报文 UDP 是一个面向报文(报文可以理解为一段段的数据)的协议.意思就是 UDP 只是报文的搬运工,不会对报文进行任何拆分和拼接操作. 具体来说 在发送端,应用层将数据传递给传输层的 ...

  4. 数据结构HashMap(Android SparseArray 和ArrayMap)

    HashMap也是我们使用非常多的Collection,它是基于哈希表的 Map 接口的实现,以key-value的形式存在.在HashMap中,key-value总是会当做一个整体来处理,系统会根据 ...

  5. 【转】Python学习路线

    Python最佳学习路线图 python语言基础 (1)Python3入门,数据类型,字符串 (2)判断/循环语句,函数,命名空间,作用域 (3)类与对象,继承,多态 (4)tkinter界面编程 ( ...

  6. SQLServer 2014 本地机房HA+灾备机房DR解决方案

    SQLServer 2014 主数据中心HA+灾备机房DR解决方案 SQLServer 2008 的时候使用 local WSFC+DR Mirror方式,对象是单数据库 两个单独的 WSFC 上使用 ...

  7. ORA-00379: no free buffers available in buffer pool DEFAULT for block size 16K

    SYS@orcl> select TABLESPACE_NAME ,AUTOEXTENSIBLE from dba_data_files ; ERROR: ORA-00379: no free ...

  8. SQL*Plus工具

    或者

  9. Java的两个实验程序

    日期:2018.10.07 星期五 博客期:015 Part1:----------------第一个是二柱子出30道小学数学题: 一.程序设计思想 本程序设计由三部分构成,第一部分因为循环30次的需 ...

  10. easyUI详解

    1.EasyUI 是前端框架,封装大量 css和封装大量 JS 2.使用前端框架时,给标签定义class 属性,就会有样式和脚本功能了 3.data-options 属性是定义 easyui 属性的, ...