交互式数据可视化工具Boken介绍

2024-06-18 23:18

本文主要是介绍交互式数据可视化工具Boken介绍,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

 

 

 

 

 

可视化工具

In [1]:

 

import bokeh
# 检查版本是否为0.12.5
bokeh.__version__

Out[1]:

'0.12.5'

In [2]:

 

 
from bokeh.io import output_notebook,output_file,show
from bokeh.charts import Scatter,Bar,BoxPlot,Chord
import seaborn as sns

In [3]:

 

 
# 导入数据
exe_data = sns.load_dataset('exercise')
exe_data.head()

Out[3]:

 Unnamed: 0iddietpulsetimekind
001low fat851 minrest
111low fat8515 minrest
221low fat8830 minrest
332low fat901 minrest
442low fat9215 minrest

In [4]:

 

output_notebook()
#output_file('test.html')

 BokehJS 0.12.5 successfully loaded.

In [6]:

 

p = Scatter(data=exe_data, x='id',y='pulse',title='散点图',xlabel='ID',ylabel='脉搏')
show(p)

 

柱状图

In [7]:

 

 
p = Bar(data=exe_data, label='id',values='pulse',title='柱状图')
show(p)

 

In [8]:

 

 
p = Bar(data=exe_data, label='diet',values='pulse',title='柱状图')
show(p)

 

In [10]:

 

 
p = Bar(data=exe_data,values='pulse', label='diet',stack='kind', title='堆叠柱状图', agg='mean',xlabel='饮食',ylabel='脉搏(均值)')
show(p)

 

In [11]:

 

 
p = Bar(data=exe_data,values='pulse', label='diet',group='kind', title='分组柱状图', agg='mean',xlabel='饮食',ylabel='脉搏均值)')
show(p)

 

盒子图

In [12]:

 

# 盒子图
box = BoxPlot(data=exe_data,values='pulse',label='diet',title='盒子图')
show(box)

 

弦线Chord

In [16]:

 

 
chord = Chord(data=exe_data, source='id', target='kind', value='pulse')
show(chord)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-16-c32cc5b7881b> in <module>
----> 1 chord = Chord(data=exe_data, source='id', target='kind', value='pulse')2 show(chord)C:\software\ANACONDA\Anaconda3\lib\site-packages\bokeh\charts\builders\chord_builder.py in Chord(data, source, target, value, square_matrix, label, xgrid, ygrid, **kw)304     kw['ygrid'] = ygrid305 
--> 306     chart = create_and_build(ChordBuilder, data, **kw)307 308     chart.left[0].visible = FalseC:\software\ANACONDA\Anaconda3\lib\site-packages\bokeh\charts\builder.py in create_and_build(builder_class, *data, **kws)54     chart_kws = {k: v for k, v in kws.items() if k not in builder_props}55     chart = Chart(**chart_kws)
---> 56     chart.add_builder(builder)57     chart.start_plot()58 C:\software\ANACONDA\Anaconda3\lib\site-packages\bokeh\charts\chart.py in add_builder(self, builder)151     def add_builder(self, builder):152         self._builders.append(builder)
--> 153         builder.create(self)154 155     def add_ranges(self, dim, range):C:\software\ANACONDA\Anaconda3\lib\site-packages\bokeh\charts\builder.py in create(self, chart)503         """504         # call methods that allow customized setup by subclasses
--> 505         self.setup()506         self.process_data()507 C:\software\ANACONDA\Anaconda3\lib\site-packages\bokeh\charts\builders\chord_builder.py in setup(self)118                     for _, row in self.values._data.iterrows():119                         m[row[self.origin]][row[self.destination]] = row[self.value]
--> 120                     self.matrix = m.get_values().T121         else:122             # It's already a square matrixC:\software\ANACONDA\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)5137             if self._info_axis._can_hold_identifiers_and_holds_name(name):5138                 return self[name]
-> 5139             return object.__getattribute__(self, name)5140 5141     def __setattr__(self, name: str, value) -> None:AttributeError: 'DataFrame' object has no attribute 'get_values'

Bokeh 绘制常用图形元素

In [17]:

 

 
from bokeh.plotting import figure
from bokeh.io import output_notebook,output_file,show
import numpy as np

In [18]:

 

 
output_notebook()

 BokehJS 0.12.5 successfully loaded.

In [21]:

 

 
p = figure(plot_width=400,plot_height=400)
#圆形
p.circle(np.random.randint(1,10,5),np.random.randint(1,10,5),size=10,color='green')
# 方形
p.square(np.random.randint(1,10,5),np.random.randint(1,10,5),size=10,color='navy')
# 折形
p.line(np.random.randint(1,10,5),np.random.randint(1,10,5),size=10,color='red')
show(p)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-21-e16157b7fbfd> in <module>9 10 # 折形
---> 11 p.line(np.random.randint(1,10,5),np.random.randint(1,10,5),size=10,color='red')12 13 show(p)fakesource in line(self, x, y, **kwargs)C:\software\ANACONDA\Anaconda3\lib\site-packages\bokeh\plotting\helpers.py in func(self, **kwargs)559             mglyph_ca = None560 
--> 561         glyph = _make_glyph(glyphclass, kwargs, glyph_ca)562         nsglyph = _make_glyph(glyphclass, kwargs, nsglyph_ca)563         sglyph = _make_glyph(glyphclass, kwargs, sglyph_ca)C:\software\ANACONDA\Anaconda3\lib\site-packages\bokeh\plotting\helpers.py in _make_glyph(glyphclass, kws, extra)152     kws = kws.copy()153     kws.update(extra)
--> 154     return glyphclass(**kws)155 156 C:\software\ANACONDA\Anaconda3\lib\site-packages\bokeh\model.py in __init__(self, **kwargs)224         self._id = kwargs.pop("id", make_id())225         self._document = None
--> 226         super(Model, self).__init__(**kwargs)227         default_theme.apply_to_model(self)228 C:\software\ANACONDA\Anaconda3\lib\site-packages\bokeh\core\has_props.py in __init__(self, **properties)239 240         for name, value in properties.items():
--> 241             setattr(self, name, value)242 243     def __setattr__(self, name, value):C:\software\ANACONDA\Anaconda3\lib\site-packages\bokeh\core\has_props.py in __setattr__(self, name, value)274                 matches, text = props, "possible"275 
--> 276             raise AttributeError("unexpected attribute '%s' to %s, %s attributes are %s" %277                 (name, self.__class__.__name__, text, nice_join(matches)))278 AttributeError: unexpected attribute 'size' to Line, possible attributes are js_event_callbacks, js_property_callbacks, line_alpha, line_cap, line_color, line_dash, line_dash_offset, line_join, line_width, name, subscribed_events, tags, x or y

3D绘图--mplot3d

3D曲线可视化

In [22]:

 

 
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

In [23]:

 

# 准备数据
zline = np.linspace(0,15,1000)
xline = np.sin(zline)
yline = np.cos(zline)

In [24]:

 

 
# 创建3D图像对象
fig = plt.figure()
ax = Axes3D(fig)
# 绘制图像
ax.plot(xline, yline, zline)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()

In [25]:

 

 
# 创建3D图像对象
fig = plt.figure()
ax = Axes3D(fig)
# 绘制图像,以X轴为竖轴
ax.plot(xline,yline,zline,zdir='x')
plt.show()

In [26]:

 

 
# 创建3D图像对象
fig = plt.figure()
ax = Axes3D(fig)
# 绘制图像,以X轴为竖轴
ax.plot(xline,yline,zline,zdir='y')
plt.show()

In [27]:

 

# 创建3D图像对象
fig = plt.figure()
ax = Axes3D(fig)
# 绘制图像,以X轴为竖轴
ax.plot(xline,yline,zline,zdir='z')
plt.show()

3D 散点图可视化

In [28]:

 

 
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

In [29]:

 

 
# 准备2组散点数据
x1 = np.random.rand(100)
y1 = np.random.rand(100)
z1 = np.random.rand(100)
x2 = np.random.rand(100)
y2 = np.random.rand(100)
z2 = np.random.rand(100)

In [30]:

 

 
# 创建3D图像对象
fig = plt.figure()
ax = Axes3D(fig)
# 绘制图像
ax.scatter(x1, y1, z1, s=10, c='r',marker='o')
ax.scatter(x2, y2, z2, s=80, c='g',marker='^')
plt.show()

3D 柱状图可视化

In [31]:

 

 
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

In [32]:

 

# 准备数据
x = np.arange(10)
y1 = np.random.rand(10)
y2 = np.random.rand(10)

In [33]:

 

 
# 创建3D图像对象
fig = plt.figure()
ax = Axes3D(fig)
# 绘制图像
ax.scatter(x, y1,0)
ax.scatter(x, y2,1)
ax.set_yticks([0, 1])
plt.show()

Pandas 绘图

In [34]:

 

 
import matplotlib.pyplot as plt
import pandas as pd

In [36]:

 

 
# 加载鸢尾花数据集
iris_data = pd.read_csv(r'C:\Users\ML Learning\Projects\第四章-数据分析预习内容\第四章-数据分析预习内容\第三节-数据可视化\lesson_07\lesson_07\examples\dataset\iris.csv')
iris_data.head()

Out[36]:

 sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
05.13.51.40.2setosa
14.93.01.40.2setosa
24.73.21.30.2setosa
34.63.11.50.2setosa
45.03.61.40.2setosa

折线图

In [37]:

 

 
iris_data.plot()

Out[37]:

<AxesSubplot:>

柱状图

In [39]:

 

 
# 分组柱状图
iris_data.groupby('species').mean().plot(kind='bar')

Out[39]:

<AxesSubplot:xlabel='species'>

In [40]:

 

 
# 堆叠柱状图
iris_data.groupby('species').mean().plot(kind='bar',stacked=True)

Out[40]:

<AxesSubplot:xlabel='species'>

In [ ]:

 

 

 

这篇关于交互式数据可视化工具Boken介绍的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1073333

相关文章

Linux下利用select实现串口数据读取过程

《Linux下利用select实现串口数据读取过程》文章介绍Linux中使用select、poll或epoll实现串口数据读取,通过I/O多路复用机制在数据到达时触发读取,避免持续轮询,示例代码展示设... 目录示例代码(使用select实现)代码解释总结在 linux 系统里,我们可以借助 select、

基于Python开发Windows自动更新控制工具

《基于Python开发Windows自动更新控制工具》在当今数字化时代,操作系统更新已成为计算机维护的重要组成部分,本文介绍一款基于Python和PyQt5的Windows自动更新控制工具,有需要的可... 目录设计原理与技术实现系统架构概述数学建模工具界面完整代码实现技术深度分析多层级控制理论服务层控制注

5 种使用Python自动化处理PDF的实用方法介绍

《5种使用Python自动化处理PDF的实用方法介绍》自动化处理PDF文件已成为减少重复工作、提升工作效率的重要手段,本文将介绍五种实用方法,从内置工具到专业库,帮助你在Python中实现PDF任务... 目录使用内置库(os、subprocess)调用外部工具使用 PyPDF2 进行基本 PDF 操作使用

C#使用iText获取PDF的trailer数据的代码示例

《C#使用iText获取PDF的trailer数据的代码示例》开发程序debug的时候,看到了PDF有个trailer数据,挺有意思,于是考虑用代码把它读出来,那么就用到我们常用的iText框架了,所... 目录引言iText 核心概念C# 代码示例步骤 1: 确保已安装 iText步骤 2: C# 代码程

Pandas处理缺失数据的方式汇总

《Pandas处理缺失数据的方式汇总》许多教程中的数据与现实世界中的数据有很大不同,现实世界中的数据很少是干净且同质的,本文我们将讨论处理缺失数据的一些常规注意事项,了解Pandas如何表示缺失数据,... 目录缺失数据约定的权衡Pandas 中的缺失数据None 作为哨兵值NaN:缺失的数值数据Panda

C++中处理文本数据char与string的终极对比指南

《C++中处理文本数据char与string的终极对比指南》在C++编程中char和string是两种用于处理字符数据的类型,但它们在使用方式和功能上有显著的不同,:本文主要介绍C++中处理文本数... 目录1. 基本定义与本质2. 内存管理3. 操作与功能4. 性能特点5. 使用场景6. 相互转换核心区别

python库pydantic数据验证和设置管理库的用途

《python库pydantic数据验证和设置管理库的用途》pydantic是一个用于数据验证和设置管理的Python库,它主要利用Python类型注解来定义数据模型的结构和验证规则,本文给大家介绍p... 目录主要特点和用途:Field数值验证参数总结pydantic 是一个让你能够 confidentl

基于Go语言开发一个 IP 归属地查询接口工具

《基于Go语言开发一个IP归属地查询接口工具》在日常开发中,IP地址归属地查询是一个常见需求,本文将带大家使用Go语言快速开发一个IP归属地查询接口服务,有需要的小伙伴可以了解下... 目录功能目标技术栈项目结构核心代码(main.go)使用方法扩展功能总结在日常开发中,IP 地址归属地查询是一个常见需求:

JAVA实现亿级千万级数据顺序导出的示例代码

《JAVA实现亿级千万级数据顺序导出的示例代码》本文主要介绍了JAVA实现亿级千万级数据顺序导出的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 前提:主要考虑控制内存占用空间,避免出现同时导出,导致主程序OOM问题。实现思路:A.启用线程池

使用python制作一款文件粉碎工具

《使用python制作一款文件粉碎工具》这篇文章主要为大家详细介绍了如何使用python制作一款文件粉碎工具,能够有效粉碎密码文件和机密Excel表格等,感兴趣的小伙伴可以了解一下... 文件粉碎工具:适用于粉碎密码文件和机密的escel表格等等,主要作用就是防止 别人用数据恢复大师把你刚删除的机密的文件恢