python交互式绘图库_一个交互式可视化Python库——Bokeh
本篇为《Python数据可视化实战》第十篇文章,我们一起学习一个交互式可视化Python库——Bokeh。
Bokeh基础
Bokeh是一个专门针对Web浏览器的呈现功能的交互式可视化Python库。这是Bokeh与其它可视化库最核心的区别。
Bokeh绘图步骤
①获取数据
②构建画布figure()
③添加图层,绘图line,circle,square,scatter,multi_line等;参数co
lor,legend
④自定义视觉属性
⑤选择性展示折线数据,建立复选框激活显示,复选框(checkbox)
导入库和数据
import numpy as np
import bokeh
from bokeh.layouts import gridplot
from bokeh.plotting import figure, output_file, show
图表实例
1.散点图
import numpy as np
import bokeh
from bokeh.layouts import gridplot
from bokeh.plotting import figure, output_file, show
# output_file("patch.html") #输出网页形式
p = figure(plot_width=100, plot_height=100)
#数据
N=9
x=np.linspace(-2,2,N)
y=x**2
sizes=np.linspace(10,20,N)
xpts=np.array([-0.09,-0.12,0.0,0.12,0.09])
ypts=np.array([-0.1,0.02,0.1,0.02,-0.1])
p=figure(title="annular_wedge")
p.annular_wedge(x,y,10,20,0.3,4.1,color="#8888ee",inner_radius_units="screen",outer_radius_units="screen")
# Set to output the plot in the notebook
output_notebook()
show(p)
2.多分类的散点图
from bokeh.sampledata.iris import flowers
from bokeh.plotting import figure
from bokeh.io import show, output_notebook
#配色
colormap={'setosa':'red','versicolor':'green','virginica':'blue'}
colors=[colormap[x] for x in flowers['species']]
#画布
p=figure(title='Tris Morphology')
#绘图
#flowers['petal_length']为x,flowers['petal_width']为y,fill_alpha=0.3为填充透明度
p.circle(flowers['petal_length'],flowers['petal_width'],color=colors,fill_alpha=0.3,size=10)
#显示
output_notebook()
show(p)
3.数值大小以散点图大小来表示
import numpy as np
from bokeh.sampledata.iris import flowers
from bokeh.plotting import figure
from bokeh.io import show, output_notebook
x=[1,2,3,4]
y=[5,7,9,12]
sizes=np.array(y)+10 #气泡大小
p=figure(title='bubble chart')
p=figure(plot_width=300,plot_height=300)
p.scatter(x,y,marker="circle",size=sizes,color="navy")
output_notebook()
show(p)
4.折线图line
from bokeh.layouts import column, gridplot
from bokeh.models import BoxSelectTool, Div
from bokeh.plotting import figure
from bokeh.io import show, output_notebook
# 数据
x = [1, 2, 3, 4, 5, 6, 7]
y = [6, 7, 2, 4, 5, 10, 4]
# 画布:坐标轴标签,画布大小
p = figure(title="line example", x_axis_label='x', y_axis_label='y', width=400, height=400)
# 画图:数据、图例、线宽
p.line(x, y, legend="Temp.", line_width=2) # 折线图
# 显示
output_notebook()
show(p)
5.同时展示不同函数,以散点和折线方式
# 数据,同时展示不同函数,以散点和折线方式
x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
y0 = [i**2 for i in x]
y1 = [10**i for i in x]
y2 = [10**(i**2) for i in x]
# 创建画布
p = figure(
tools="pan,box_zoom,reset,save",
y_axis_type="log", title="log axis example",
x_axis_label='sections', y_axis_label='particles',
width=700, height=350) # y轴类型:log指数或linear线性
# 增加图层,绘图
p.line(x, x, legend="y=x")
p.circle(x, x, legend="y=x", fill_color="white", size=8)
p.line(x, y0, legend="y=x^2", line_width=3)
p.line(x, y1, legend="y=10^x", line_color="red")
p.circle(x, y1, legend="y=10^x", fill_color="red", line_color="red", size=6)
p.line(x, y2, legend="y=10^x^2", line_color="orange", line_dash="4 4")
# 显示
output_notebook()
show(p)
6.不同颜色不同形状表示不同类别的事物
# 数据,同时展示不同函数,以散点和折线方式
x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
y0 = [i**2 for i in x]
y1 = [10**i for i in x]
y2 = [10**(i**2) for i in x]
# 创建画布
p = figure(
tools="pan,box_zoom,reset,save",
y_axis_type="log", title="log axis example",
x_axis_label='sections', y_axis_label='particles',
width=700, height=350) # y轴类型:log指数或linear线性
# 增加图层,绘图
p.line(x, x, legend="y=x")
p.circle(x, x, legend="y=x", fill_color="white", size=8)
p.line(x, y0, legend="y=x^2", line_width=3)
p.line(x, y1, legend="y=10^x", line_color="red")
p.circle(x, y1, legend="y=10^x", fill_color="red", line_color="red", size=6)
p.line(x, y2, legend="y=10^x^2", line_color="orange", line_dash="4 4")
# 显示
output_notebook()
show(p)
7.不同函数设置创建复选框库选择性显示
x = np.linspace(0, 4 * np.pi, 100)
# 画布
p = figure()
# 折线属性
props = dict(line_width=4, line_alpha=0.7)
# 绘图3条函数序列
l0 = p.line(x, np.sin(x), color=Viridis3[0], legend="Line 0", **props)
l1 = p.line(x, 4 * np.cos(x), color=Viridis3[1], legend="Line 1", **props)
l2 = p.line(x, np.tan(x), color=Viridis3[2], legend="Line 2", **props)
# 复选框激活显示,复选框(checkbox),三个函数序列可选择性展示出来
checkbox = CheckboxGroup(labels=["Line 0", "Line 1", "Line 2"],
active=[0, 1, 2], width=100)
#
checkbox.callback = CustomJS(args=dict(l0=l0, l1=l1, l2=l2, checkbox=checkbox), code="""
l0.visible = 0 in checkbox.active;
l1.visible = 1 in checkbox.active;
l2.visible = 2 in checkbox.active;
""")
# 添加图层
layout = row(checkbox, p)
output_notebook()
# 显示
show(layout)
8.收盘价的时序图走势和散点图
import numpy as np
from bokeh.plotting import figure
from bokeh.io import show, output_notebook
from bokeh.layouts import row #row()的作用是将多个图像以行的方式放到同一张图中
from bokeh.palettes import Viridis3
from bokeh.models import CheckboxGroup, CustomJS #CheckboxGroup 创建复选框库
# 数据
aapl = np.array(AAPL['adj_close'])
aapl_dates = np.array(AAPL['date'], dtype=np.datetime64)
window_size = 30
window = np.ones(window_size)/float(window_size)
aapl_avg = np.convolve(aapl, window, 'same')
# 画布
p = figure(width=800, height=350, x_axis_type="datetime")
# 图层
p.circle(aapl_dates, aapl, size=4, color='darkgrey', alpha=0.2, legend='close') #散点图
p.line(aapl_dates, aapl_avg, color='red', legend='avg') #折线时序图
# 自定义视觉属性
p.title.text = "AAPL One-Month Average"
p.legend.location = "top_left"
p.grid.grid_line_alpha=0
p.xaxis.axis_label = 'Date'
p.yaxis.axis_label = 'Price'
p.ygrid.band_fill_color="gray"
p.ygrid.band_fill_alpha = 0.1
p.legend.click_policy="hide" # 点击图例显示隐藏数据
# 显示结果
output_notebook()
show(p)
python交互式绘图库_一个交互式可视化Python库——Bokeh相关推荐
- python安装绘图库matplotlib_Python基础教程:Python 2D绘图库 Matplotlib 简介和安装
原标题:Python基础教程:Python 2D绘图库 Matplotlib 简介和安装 来自:Linux迷https://www.linuxmi.com/python-2d-matplotlib.h ...
- python刷微博转发_一个简单的python刷新浪微博粉丝小程序
代码简陋没有什么技术,还有个txt的配置文件才可以用.但是配置文件不发出来了.只要你自己好好看代码很容易猜得到.希望大家不要拿去刷了.因为实在是没意思. 代码中需要用到的相关python模块方法有: ...
- python爬微博关键字_一个简单的python爬虫实践,爬取包含关键词的新浪微博
此项目主要功能是通过微博"搜索"页面,每天自动爬取所有包含自定list中词汇的微博原数据.低速可控,简单粗暴,适合用来有针对性的搜集数据量不是很大的包含关键词的微博,每日可爬3-6 ...
- python入侵数据库数据库_一个简单的Python访问Mysql数据库例子
2020/11/3操作记录 搭建好Python的数据环境之后,接下来就是在Python代码中访问数据库我先在Navicat图形化界面创建一个数据库命名为pythontest,再在数据库中创建了一个表s ...
- python编写通讯录管理系统_一个简单的python程序实例(通讯录)
{"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],&q ...
- python 三维绘图库_Python第三方库matplotlib(2D绘图库)入门与进阶
Matplotlib 一 简介: Matplotlib是一个Python 2D绘图库,它可以在各种平台上以各种硬拷贝格式和交互式环境生成出具有出版品质的图形. Matplotlib可用于Python脚 ...
- 用python绘制熊猫图案_使用熊猫在Python中绘制数据
用python绘制熊猫图案 在关于基于Python的绘图库的系列文章中 ,我们将对使用pandas(一种非常流行的Python数据操作库)的绘图进行概念性的研究. Pandas是Python中用于可缩 ...
- python自动源码_谷歌推出Tangent开源库,在Python源代码上做自动微分
原标题:谷歌推出Tangent开源库,在Python源代码上做自动微分 李林 编译自 Google Research Blog 谷歌今天推出了一个新的开源Python自动微分库:Tangent. 和现 ...
- linux python复制安装,复制一个Python全部环境到另一个环境,python另一个,导出此环境下安装的包...
复制一个Python全部环境到另一个环境,python另一个,导出此环境下安装的包 导出此环境下安装的包的版本信息清单pipfreeze>requirements.txt联网,下载清单中的包到a ...
最新文章
- 字符串转换成utf-8编码
- 体感(Kinect)技术开发和应用简介
- PHP的getimagesize获取图像信息
- HDU - 1429 胜利大逃亡(续) (BFS+状压)
- 滴滴为啥值3600亿?看它的数据中台就知道了
- mysql duplicate key与replace into对比
- python压缩教程_Python压缩模块zipfile实现原理及用法解析
- tablueau地图标记圆形_高德地图实现自定义小蓝点 自定义点标记 绘制多边形/圆形区域 根据地图的移动显示或者隐藏自定义点标记的相关实现...
- Matlab Tricks(三十) —— 任意区间的均匀分布
- django 1.8 官方文档翻译: 3-6-2 内建的中间件
- mysql 批量添加更新_mysql 批量添加 更新
- CTF之做题总结(二)
- 微信小程序选项卡切换
- CF140C New Year Snowmen (#贪心+优先队列)
- flappy bird用java实现_java实现Flappy Bird游戏源代码
- 聚焦时代起点 智启云上未来——IMPACT2018紫光云峰会在津举办
- 生成和扫描二维码(ZXing库)
- Oracle 中的各种读
- 【分享】pushplus入驻集简云平台,实现无代码集成数百款应用
- 光敏电阻5506主要参数_光敏电阻器的主要参数