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

相关文章

Java数字转换工具类NumberUtil的使用

《Java数字转换工具类NumberUtil的使用》NumberUtil是一个功能强大的Java工具类,用于处理数字的各种操作,包括数值运算、格式化、随机数生成和数值判断,下面就来介绍一下Number... 目录一、NumberUtil类概述二、主要功能介绍1. 数值运算2. 格式化3. 数值判断4. 随机

使用Navicat工具比对两个数据库所有表结构的差异案例详解

《使用Navicat工具比对两个数据库所有表结构的差异案例详解》:本文主要介绍如何使用Navicat工具对比两个数据库test_old和test_new,并生成相应的DDLSQL语句,以便将te... 目录概要案例一、如图两个数据库test_old和test_new进行比较:二、开始比较总结概要公司存在多

Redis的数据过期策略和数据淘汰策略

《Redis的数据过期策略和数据淘汰策略》本文主要介绍了Redis的数据过期策略和数据淘汰策略,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录一、数据过期策略1、惰性删除2、定期删除二、数据淘汰策略1、数据淘汰策略概念2、8种数据淘汰策略

轻松上手MYSQL之JSON函数实现高效数据查询与操作

《轻松上手MYSQL之JSON函数实现高效数据查询与操作》:本文主要介绍轻松上手MYSQL之JSON函数实现高效数据查询与操作的相关资料,MySQL提供了多个JSON函数,用于处理和查询JSON数... 目录一、jsON_EXTRACT 提取指定数据二、JSON_UNQUOTE 取消双引号三、JSON_KE

Python给Excel写入数据的四种方法小结

《Python给Excel写入数据的四种方法小结》本文主要介绍了Python给Excel写入数据的四种方法小结,包含openpyxl库、xlsxwriter库、pandas库和win32com库,具有... 目录1. 使用 openpyxl 库2. 使用 xlsxwriter 库3. 使用 pandas 库

SpringBoot定制JSON响应数据的实现

《SpringBoot定制JSON响应数据的实现》本文主要介绍了SpringBoot定制JSON响应数据的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录前言一、如何使用@jsonView这个注解?二、应用场景三、实战案例注解方式编程方式总结 前言

Java中基于注解的代码生成工具MapStruct映射使用详解

《Java中基于注解的代码生成工具MapStruct映射使用详解》MapStruct作为一个基于注解的代码生成工具,为我们提供了一种更加优雅、高效的解决方案,本文主要为大家介绍了它的具体使用,感兴趣... 目录介绍优缺点优点缺点核心注解及详细使用语法说明@Mapper@Mapping@Mappings@Co

使用Python在Excel中创建和取消数据分组

《使用Python在Excel中创建和取消数据分组》Excel中的分组是一种通过添加层级结构将相邻行或列组织在一起的功能,当分组完成后,用户可以通过折叠或展开数据组来简化数据视图,这篇博客将介绍如何使... 目录引言使用工具python在Excel中创建行和列分组Python在Excel中创建嵌套分组Pyt

在Rust中要用Struct和Enum组织数据的原因解析

《在Rust中要用Struct和Enum组织数据的原因解析》在Rust中,Struct和Enum是组织数据的核心工具,Struct用于将相关字段封装为单一实体,便于管理和扩展,Enum用于明确定义所有... 目录为什么在Rust中要用Struct和Enum组织数据?一、使用struct组织数据:将相关字段绑

在Mysql环境下对数据进行增删改查的操作方法

《在Mysql环境下对数据进行增删改查的操作方法》本文介绍了在MySQL环境下对数据进行增删改查的基本操作,包括插入数据、修改数据、删除数据、数据查询(基本查询、连接查询、聚合函数查询、子查询)等,并... 目录一、插入数据:二、修改数据:三、删除数据:1、delete from 表名;2、truncate