[453]Bokeh绘图--交互式数据可视化库Bokeh绘制基本图表

散点图

圆形散点图,调用circle(),代码如下:

# -*- coding:utf-8 -*-
from bokeh.plotting import figure, output_file, show

'''圆形散点图,调用circle()'''
# 输出到静态HTML文件
output_file("../html/line.html")
p = figure(plot_width=400, plot_height=400)
# 添加一个圆形渲染器,并设置大小,颜色,透明度
p.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=20, color="navy", alpha=0.5)
# 展示结果
show(p)

[453]Bokeh绘图--交互式数据可视化库Bokeh绘制基本图表_第1张图片
正方形散点图,调用square(),代码如下:

'''正方形散点图,调用square()'''
from bokeh.plotting import figure, output_file, show

# 输出到静态HTML文件
output_file("./html/square.html")
p = figure(plot_width=400, plot_height=400)
# 添加一个正方形渲染器,并设置大小,颜色,透明度
p.square([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=20, color="olive", alpha=0.5)
# 输出结果
show(p)

[453]Bokeh绘图--交互式数据可视化库Bokeh绘制基本图表_第2张图片
Bokeh中有很多标记类型,通过调用不同的函数实现,详细如下:

asterisk()-星号
circle()-圆形
circle_cross()-圆形加十字
circle_x()-圆形加X交叉
cross()-十字
diamond()-菱形
diamond_cross()-菱形加十字
inverted_triangle()-倒三角形
square()-正方形
square_cross()-正方形加十字
square_x()-正方形加X交叉
triangle()-三角形
x()-X字

所有标记都具有相同的一组属性:横坐标x,纵坐标y,大小和角度。 此外,circle()具有可用于指定数据空间单位的半径属性,也就是可以画我们常说的气泡图。

折线图

单线图,调用line(),代码如下:

'''单线图,调用line()'''
from bokeh.plotting import figure, output_file, show

output_file("./html/line.html")
p = figure(plot_width=400, plot_height=400)
# 添加线条渲染器
p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2)
show(p)

[453]Bokeh绘图--交互式数据可视化库Bokeh绘制基本图表_第3张图片
阶梯折线图,调用step(),代码如下:

'''阶梯折线图,调用step()'''
from bokeh.plotting import figure, output_file, show

output_file("./html/line.html")
p = figure(plot_width=400, plot_height=400)
# 添加一个阶梯线条渲染器
p.step([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2, mode="center")
show(p)

[453]Bokeh绘图--交互式数据可视化库Bokeh绘制基本图表_第4张图片
多线图,调用multi_line(),代码如下:

'''多线图,调用multi_line()'''
from bokeh.plotting import figure, output_file, show

output_file("./html/patch.html")
p = figure(plot_width=400, plot_height=400)
p.multi_line([[1, 3, 2], [3, 4, 6, 6]], [[2, 1, 4], [4, 7, 8, 5]],color=["firebrick", "navy"], alpha=[0.8, 0.3], line_width=4)
show(p)

[453]Bokeh绘图--交互式数据可视化库Bokeh绘制基本图表_第5张图片
缺失值处理:

'''缺失值处理:'''
from bokeh.plotting import figure, output_file, show

output_file("./html/line.html")
p = figure(plot_width=400, plot_height=400)
# 添加一个带有NaN值的渲染器
nan = float('nan')
p.line([1, 2, 3, nan, 4, 5], [6, 7, 2, 4, 4, 5], line_width=2)
show(p)

[453]Bokeh绘图--交互式数据可视化库Bokeh绘制基本图表_第6张图片

矩形图

通过指定左侧,右侧,顶部和底部位置可以绘制轴对齐的矩形,调用quad()函数:

'''矩形图
通过指定左侧,右侧,顶部和底部位置可以绘制轴对齐的矩形,调用quad()函数:'''
from bokeh.plotting import figure, show, output_file

output_file('./html/rectangles.html')
p = figure(plot_width=400, plot_height=400)
p.quad(top=[2, 3, 4], bottom=[1, 2, 3], left=[1, 2, 3], right=[1.2, 2.5, 3.7], color="#B3DE69")
show(p)

[453]Bokeh绘图--交互式数据可视化库Bokeh绘制基本图表_第7张图片
通过指定中心点,宽度,高度和角度来绘制任意矩形,调用rect()函数:

'''通过指定中心点,宽度,高度和角度来绘制任意矩形,调用rect()函数'''
from math import pi
from bokeh.plotting import figure, show, output_file

output_file('./html/rectangles_rotated.html')
p = figure(plot_width=400, plot_height=400)
p.rect(x=[1, 2, 3], y=[1, 2, 3], width=0.2, height=40, color="#CAB2D6", angle=pi/3, height_units="screen")
show(p)

[453]Bokeh绘图--交互式数据可视化库Bokeh绘制基本图表_第8张图片
Bokeh提供了vbar()和hbar()函数来绘制垂直条和水平条图。

通过指定x中心坐标,宽度以及顶部和底部端点来绘制垂直条,调用vbar()函数:

'''通过指定x中心坐标,宽度以及顶部和底部端点来绘制垂直条,调用vbar()函数'''
from bokeh.plotting import figure, show, output_file

output_file('./html/vbar.html')
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="firebrick")
show(p)

[453]Bokeh绘图--交互式数据可视化库Bokeh绘制基本图表_第9张图片
通过指定y中心坐标,高度和左右端点绘制水平条,调用hbar()函数:

'''通过指定y中心坐标,高度和左右端点绘制水平条,调用hbar()函数:'''
from bokeh.plotting import figure, show, output_file

output_file('./html/hbar.html')
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)

[453]Bokeh绘图--交互式数据可视化库Bokeh绘制基本图表_第10张图片

六角砖图

Bokeh可以绘制通常用于显示装仓聚合的六角砖图。 调用hex_tile()需要一个尺寸参数来定义十六进制网格的大小,并使用轴向坐标来指定哪些图块存在。调用hexbin()函数计算每个bin的计数并绘制颜色映射计数:

'''六角砖图
Bokeh可以绘制通常用于显示装仓聚合的六角砖图。 调用hex_tile()需要一个尺寸参数来定义十六进制网格
的大小,并使用轴向坐标来指定哪些图块存在。调用hexbin()函数计算每个bin的计数并绘制颜色映射计数:'''
import numpy as np
from bokeh.io import output_file, show
from bokeh.plotting import figure
from bokeh.transform import linear_cmap
from bokeh.util.hex import hexbin

n = 50000
x = np.random.standard_normal(n)
y = np.random.standard_normal(n)
bins = hexbin(x, y, 0.1)
p = figure(tools="wheel_zoom,reset,save", match_aspect=True, background_fill_color='#440154')
p.grid.visible = False
p.hex_tile(q="q", r="r", size=0.1, line_color=None, source=bins, fill_color=linear_cmap('counts', 'Viridis256', 0, max(bins.counts)))
output_file("./html/hex_tile.html")
show(p)

[453]Bokeh绘图--交互式数据可视化库Bokeh绘制基本图表_第11张图片

补丁图

补丁图,听起来比较费解对不对,实质就是,把散点图首尾相连,填充颜色的效果。

单补丁图:

'''补丁图,听起来比较费解对不对,实质就是,把散点图首尾相连,填充颜色的效果。
单补丁图:'''
from bokeh.plotting import figure, output_file, show

output_file("./html/patch.html")
p = figure(plot_width=400, plot_height=400)
# 添加一个带有透明度和线宽的补丁渲染器
p.patch([1, 2, 3, 4, 5], [6, 7, 8, 7, 3], alpha=0.5, line_width=2)
show(p)

[453]Bokeh绘图--交互式数据可视化库Bokeh绘制基本图表_第12张图片
多补丁图:

'''多补丁图:'''
from bokeh.plotting import figure, output_file, show

output_file("./html/patch.html")
p = figure(plot_width=400, plot_height=400)
p.patches([[1, 3, 2], [3, 4, 6, 6]], [[2, 1, 4], [4, 7, 8, 5]], color=["firebrick", "navy"], alpha=[0.8, 0.3], line_width=2)
show(p)

[453]Bokeh绘图--交互式数据可视化库Bokeh绘制基本图表_第13张图片
缺失值处理:

'''缺失值处理:'''
from bokeh.plotting import figure, output_file, show

output_file("./html/patch.html")
p = figure(plot_width=400, plot_height=400)
# add a patch renderer with a NaN value
nan = float('nan')
p.patch([1, 2, 3, nan, 4, 5, 6], [6, 7, 5, nan, 7, 3, 6], alpha=0.5, line_width=2)
show(p)

[453]Bokeh绘图--交互式数据可视化库Bokeh绘制基本图表_第14张图片
可以看出,本想绘制单补丁,但是由于中间存在NaN,结果变成双补丁。当然,这里也给我们提供了一个绘制多补丁的简单方式(在你没有不同颜色填充的前提下),添加NaN值。

组合图表

调用多个图表函数可以制作组合图表:

'''组合图表
调用多个图表函数可以制作组合图表:'''
from bokeh.plotting import figure, output_file, show

x = [1, 2, 3, 4, 5]
y = [6, 7, 8, 7, 3]
output_file("./html/multiple.html")
p = figure(plot_width=400, plot_height=400)
# 调用折线图和散点图
p.line(x, y, line_width=2)
p.circle(x, y, fill_color="white", size=8)
show(p)

[453]Bokeh绘图--交互式数据可视化库Bokeh绘制基本图表_第15张图片
这个示例可能跟大家之前理解的组合图表不太一样,毕竟这个图在Excel中折线图加标记就可以搞定。所以,框架结构不一样的地方还需要大家多适应。

另外,这种方式可以在同一个图形中添加不限数量的图表。

设置范围

默认情况下,Bokeh会自动设置绘图的数据边界以适合数据,但有时你可能需要设定一个绘图的范围,这可以通过使用Range1d对象设置x_range或y_range的属性来完成,这个对象提供了所需范围的开始点和结束点:

p.x_range = Range1d(0,100)

为了方便起见,figure()函数还可以接受(start,end)元组作为x_range或y_range参数的值。 下面是一个示例,显示了设置范围的两种方法:

'''设置范围'''
from bokeh.plotting import figure, output_file, show
from bokeh.models import Range1d

output_file("./html/title.html")
# 创建一个图表,设置大小和横轴范围
p = figure(plot_width=400, plot_height=400, x_range=(0, 20))
# 使用Range1d对象设置纵轴范围
p.y_range = Range1d(0, 15)
p.circle([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=10)
show(p)

[453]Bokeh绘图--交互式数据可视化库Bokeh绘制基本图表_第16张图片

范围还可以接受最小和最大属性,这样就可以指定不希望用户能够平移/缩放的图的边缘。

指定轴类型

上面的示例都是使用的默认线性轴,适用于需要以线性刻度显示数字数据的绘图。 而我们可能还会有分类数据,或需要在日期时间或对数坐标轴上显示数据。这部分将介绍如何在使用bokeh.plotting界面时指定轴类型。

分类轴

分类轴是通过为其中一个轴指定因子范围来创建的。

'''分类轴'''
from bokeh.plotting import figure, output_file, show

factors = ["a", "b", "c", "d", "e", "f", "g", "h"]
x = [50, 40, 65, 10, 25, 37, 80, 60]
output_file("./html/categorical.html")
p = figure(y_range=factors)
p.circle(x, factors, size=15, fill_color="orange", line_color="green", line_width=3)
show(p)

[453]Bokeh绘图--交互式数据可视化库Bokeh绘制基本图表_第17张图片

时间轴

在处理时间序列数据或涉及日期或时间的数据时,有一个可显示适合不同日期和时间标度的标签的轴是极好的。

我们已经看到如何使用figure.p()函数使用bokeh.plotting接口创建图。 该函数接受x_axis_type和y_axis_type作为参数。 要指定日期时间轴,请将“datetime”作为这些参数中的任何一个的值。

'''时间轴'''
import pandas as pd
from bokeh.plotting import figure, output_file, show
from bokeh.sampledata.stocks import AAPL
# import bokeh
# bokeh.sampledata.download()

df = pd.DataFrame(AAPL)
df['date'] = pd.to_datetime(df['date'])
output_file("./html/datetime.html")
# 创建一个带有时间轴的图
p = figure(plot_width=800, plot_height=250, x_axis_type="datetime")
p.line(df['date'], df['close'], color='navy', alpha=0.5)
show(p)

[453]Bokeh绘图--交互式数据可视化库Bokeh绘制基本图表_第18张图片

对数轴

当处理指数增长或者数量级数据的数据时,通常需要使用一个对数刻度轴。 另一种情况是涉及绘制具有幂律关系的数据,当需要在两个轴上使用对数刻度时。

正如我们上面看到的,figure()函数接受x_axis_type和y_axis_type作为参数。 要指定一个记录轴,请将“log”作为这些参数中的任何一个的值。

'''对数轴'''
from bokeh.plotting import figure, output_file, show

x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
y = [10**xx for xx in x]
output_file("./html/log.html")
# 创建一个带有对数轴的图
p = figure(plot_width=400, plot_height=400, y_axis_type="log")
p.line(x, y, line_width=2)
p.circle(x, y, fill_color="white", size=8)
show(p)

[453]Bokeh绘图--交互式数据可视化库Bokeh绘制基本图表_第19张图片

双轴

此外,我们还可以将多个代表不同范围的轴添加到一个绘图中,使用extra_x_range和extra_y_range命名额外的轴范围来配置图表。 然后,在添加新的图形时可以引用这些命名的范围,还可以在绘图上使用add_layout方法添加新的轴对象。 下面给出一个例子:

'''双轴'''
from numpy import pi, arange, sin, linspace
from bokeh.plotting import output_file, figure, show
from bokeh.models import LinearAxis, Range1d

x = arange(-2*pi, 2*pi, 0.1)
y = sin(x)
y2 = linspace(0, 100, len(y))
output_file("./html/twin_axis.html")
p = figure(x_range=(-6.5, 6.5), y_range=(-1.1, 1.1))
p.circle(x, y, color="red")
p.extra_y_ranges = {"foo": Range1d(start=0, end=100)}
p.circle(x, y2, color="blue", y_range_name="foo")
p.add_layout(LinearAxis(y_range_name="foo"), 'left')
show(p)

[453]Bokeh绘图--交互式数据可视化库Bokeh绘制基本图表_第20张图片
如果将倒数第二行的“left”修改为“right”,则左右侧各有一轴。
[453]Bokeh绘图--交互式数据可视化库Bokeh绘制基本图表_第21张图片

来源:http://wemedia.ifeng.com/55457387/wemedia.shtml
http://wemedia.ifeng.com/56562503/wemedia.shtml

你可能感兴趣的:(数据分析)