交互式数据可视化工具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

相关文章

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

MySQL 删除数据详解(最新整理)

《MySQL删除数据详解(最新整理)》:本文主要介绍MySQL删除数据的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、前言二、mysql 中的三种删除方式1.DELETE语句✅ 基本语法: 示例:2.TRUNCATE语句✅ 基本语

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

Python中win32包的安装及常见用途介绍

《Python中win32包的安装及常见用途介绍》在Windows环境下,PythonWin32模块通常随Python安装包一起安装,:本文主要介绍Python中win32包的安装及常见用途的相关... 目录前言主要组件安装方法常见用途1. 操作Windows注册表2. 操作Windows服务3. 窗口操作

MyBatisPlus如何优化千万级数据的CRUD

《MyBatisPlus如何优化千万级数据的CRUD》最近负责的一个项目,数据库表量级破千万,每次执行CRUD都像走钢丝,稍有不慎就引起数据库报警,本文就结合这个项目的实战经验,聊聊MyBatisPl... 目录背景一、MyBATis Plus 简介二、千万级数据的挑战三、优化 CRUD 的关键策略1. 查

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

mysql中的数据目录用法及说明

《mysql中的数据目录用法及说明》:本文主要介绍mysql中的数据目录用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、版本3、数据目录4、总结1、背景安装mysql之后,在安装目录下会有一个data目录,我们创建的数据库、创建的表、插入的

sqlite3 命令行工具使用指南

《sqlite3命令行工具使用指南》本文系统介绍sqlite3CLI的启动、数据库操作、元数据查询、数据导入导出及输出格式化命令,涵盖文件管理、备份恢复、性能统计等实用功能,并说明命令分类、SQL语... 目录一、启动与退出二、数据库与文件操作三、元数据查询四、数据操作与导入导出五、查询输出格式化六、实用功

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

Navicat数据表的数据添加,删除及使用sql完成数据的添加过程

《Navicat数据表的数据添加,删除及使用sql完成数据的添加过程》:本文主要介绍Navicat数据表的数据添加,删除及使用sql完成数据的添加过程,具有很好的参考价值,希望对大家有所帮助,如有... 目录Navicat数据表数据添加,删除及使用sql完成数据添加选中操作的表则出现如下界面,查看左下角从左