Python可视化库之HoloViews的使用教程
作者:Python学习与数据挖掘 发布时间:2023-11-05 17:09:03
标签:Python,可视化,HoloViews
最近一直在整理统计图表的绘制方法,发现Python中除了经典Seaborn库外,还有一些优秀的可交互的第三方库也能实现一些常见的统计图表绘制,而且其还拥有Matplotlib、Seaborn等库所不具备的交互效果。
当然,同时也能绘制出版级别的图表要求,此外,一些在使用Matplotlib需自定义函数才能绘制的图表在一些第三方库中都集成了,这也大大缩短了绘图时间。
今天我就详细介绍一个优秀的第三方库-HoloViews,内容主要如下:
Python-HoloViews库介绍
Python-HoloViews库样例介绍
Python-HoloViews库介绍
Python-HoloViews库作为一个开源的可视化库,其目的是使数据分析结果和可视化完美衔接,其默认的绘图主题和配色以及较少的绘图代码量,可以使你专注于数据分析本身,同时其统计绘图功能也非常优秀。更多关于HoloViews库的介绍,可参考:Python-HoloViews库官网[1]
Python-HoloViews库样例介绍
这一部分小编重点放在一些统计图表上,其绘制结果不仅可以在网页上交互,同时其默认的绘图结果也完全满足出版界别的要求,主要内容如下(以下图表都是可交互的):
密度图+箱线图
import pandas as pd
import holoviews as hv
from bokeh.sampledata import autompg
hv.extension('bokeh')
df = autompg.autompg_clean
bw = hv.BoxWhisker(df, kdims=["origin"], vdims=["mpg"])
dist = hv.NdOverlay(
{origin: hv.Distribution(group, kdims=["mpg"])
for origin, group in df.groupby("origin")}
)
bw + dist
密度图+箱线图
散点图+横线图
scatter = hv.Scatter(df, kdims=["origin"], vdims=["mpg"]).opts(jitter=0.3)
yticks = [(i + 0.25, origin) for i, origin in enumerate(df["origin"].unique())]
spikes = hv.NdOverlay(
{
origin: hv.Spikes(group["mpg"]).opts(position=i)
for i, (origin, group) in enumerate(df.groupby("origin", sort=False))
}
).opts(hv.opts.Spikes(spike_length=0.5, yticks=yticks, show_legend=False, alpha=0.3))
scatter + spikes
散点图+横线图
Iris Splom
from bokeh.sampledata.iris import flowers
from holoviews.operation import gridmatrix
ds = hv.Dataset(flowers)
grouped_by_species = ds.groupby('species', container_type=hv.NdOverlay)
grid = gridmatrix(grouped_by_species, diagonal_type=hv.Scatter)
grid.opts(opts.Scatter(tools=['hover', 'box_select'], bgcolor='#efe8e2', fill_alpha=0.2, size=4))
Iris Splom
面积图
# create some example data
python=np.array([2, 3, 7, 5, 26, 221, 44, 233, 254, 265, 266, 267, 120, 111])
pypy=np.array([12, 33, 47, 15, 126, 121, 144, 233, 254, 225, 226, 267, 110, 130])
jython=np.array([22, 43, 10, 25, 26, 101, 114, 203, 194, 215, 201, 227, 139, 160])
dims = dict(kdims='time', vdims='memory')
python = hv.Area(python, label='python', **dims)
pypy = hv.Area(pypy, label='pypy', **dims)
jython = hv.Area(jython, label='jython', **dims)
opts.defaults(opts.Area(fill_alpha=0.5))
overlay = (python * pypy * jython)
overlay.relabel("Area Chart") + hv.Area.stack(overlay).relabel("Stacked Area Chart")
面积图
直方图系列
def get_overlay(hist, x, pdf, cdf, label):
pdf = hv.Curve((x, pdf), label='PDF')
cdf = hv.Curve((x, cdf), label='CDF')
return (hv.Histogram(hist, vdims='P(r)') * pdf * cdf).relabel(label)
np.seterr(divide='ignore', invalid='ignore')
label = "Normal Distribution (μ=0, σ=0.5)"
mu, sigma = 0, 0.5
measured = np.random.normal(mu, sigma, 1000)
hist = np.histogram(measured, density=True, bins=50)
x = np.linspace(-2, 2, 1000)
pdf = 1/(sigma * np.sqrt(2*np.pi)) * np.exp(-(x-mu)**2 / (2*sigma**2))
cdf = (1+scipy.special.erf((x-mu)/np.sqrt(2*sigma**2)))/2
norm = get_overlay(hist, x, pdf, cdf, label)
label = "Log Normal Distribution (μ=0, σ=0.5)"
mu, sigma = 0, 0.5
measured = np.random.lognormal(mu, sigma, 1000)
hist = np.histogram(measured, density=True, bins=50)
x = np.linspace(0, 8.0, 1000)
pdf = 1/(x* sigma * np.sqrt(2*np.pi)) * np.exp(-(np.log(x)-mu)**2 / (2*sigma**2))
cdf = (1+scipy.special.erf((np.log(x)-mu)/(np.sqrt(2)*sigma)))/2
lognorm = get_overlay(hist, x, pdf, cdf, label)
label = "Gamma Distribution (k=1, θ=2)"
k, theta = 1.0, 2.0
measured = np.random.gamma(k, theta, 1000)
hist = np.histogram(measured, density=True, bins=50)
x = np.linspace(0, 20.0, 1000)
pdf = x**(k-1) * np.exp(-x/theta) / (theta**k * scipy.special.gamma(k))
cdf = scipy.special.gammainc(k, x/theta) / scipy.special.gamma(k)
gamma = get_overlay(hist, x, pdf, cdf, label)
label = "Beta Distribution (α=2, β=2)"
alpha, beta = 2.0, 2.0
measured = np.random.beta(alpha, beta, 1000)
hist = np.histogram(measured, density=True, bins=50)
x = np.linspace(0, 1, 1000)
pdf = x**(alpha-1) * (1-x)**(beta-1) / scipy.special.beta(alpha, beta)
cdf = scipy.special.btdtr(alpha, beta, x)
beta = get_overlay(hist, x, pdf, cdf, label)
label = "Weibull Distribution (λ=1, k=1.25)"
lam, k = 1, 1.25
measured = lam*(-np.log(np.random.uniform(0, 1, 1000)))**(1/k)
hist = np.histogram(measured, density=True, bins=50)
x = np.linspace(0, 8, 1000)
pdf = (k/lam)*(x/lam)**(k-1) * np.exp(-(x/lam)**k)
cdf = 1 - np.exp(-(x/lam)**k)
weibull = get_overlay(hist, x, pdf, cdf, label)
直方图系列
Route Chord
import holoviews as hv
from holoviews import opts, dim
from bokeh.sampledata.airport_routes import routes, airports
hv.extension('bokeh')
# Count the routes between Airports
route_counts = routes.groupby(['SourceID', 'DestinationID']).Stops.count().reset_index()
nodes = hv.Dataset(airports, 'AirportID', 'City')
chord = hv.Chord((route_counts, nodes), ['SourceID', 'DestinationID'], ['Stops'])
# Select the 20 busiest airports
busiest = list(routes.groupby('SourceID').count().sort_values('Stops').iloc[-20:].index.values)
busiest_airports = chord.select(AirportID=busiest, selection_mode='nodes')
busiest_airports.opts(
opts.Chord(cmap='Category20', edge_color=dim('SourceID').str(),
height=800, labels='City', node_color=dim('AirportID').str(), width=800))
Route Chord
小提琴图
import holoviews as hv
from holoviews import dim
from bokeh.sampledata.autompg import autompg
hv.extension('bokeh')
violin = hv.Violin(autompg, ('yr', 'Year'), ('mpg', 'Miles per Gallon')).redim.range(mpg=(8, 45))
violin.opts(height=500, width=900, violin_fill_color=dim('Year').str(), cmap='Set1')
小提琴图
更多样例可查看:Python-HoloViews样例[2]
来源:https://blog.csdn.net/weixin_38037405/article/details/123060349
0
投稿
猜你喜欢
- 在中文网页中最常见的网页编码就是GB2312和UTF-8了,本文介绍了ASP实现GB2312编码转换为UTF-8编码的函数:Function
- OpenCVOpenCV 是计算机视觉领域最受欢迎的开源库,起初它由 C/C ++ 编写,现在用 Python 也能使用。OpenCV 可以
- 可以使用numpy中的linspace函数np.linspace(start, stop, num, endpoint, retstep,
- 1.循环删除 #这个是我选中其中的一个分支进行右键清空操作时进行的处理for i in range(self.tree.currentIte
- 演示技术栈这次用到了关于css的一些功能,和jQuery。CSS3中添加的新属性animation是用来为元素实现动画效果的,但是anima
- 前阵子刚完成一个B/S架构的学校办公系统,体会就是表太多,文件太多,而每个文件中类似的操作(代码)也太多了,例如学生信息和教师信息操作,st
- 在进行接口自动化测试时,有好多接口都基于登陆接口的响应值来关联进行操作的,在次之前试了很多方法,都没有成功,其实很简单用session来做。
- 1.sort()方法sort()是列表的方法,修改原列表使得它按照大小排序,没有返回值,返回NoneIn [90]: x = [4, 6,
- html页面一般都会指定一个编码,如何获取到是处理html页面的第一步,因为错误的编码必然带来后面处理的问题。这里我用python的正则表达
- 数据库(database)MySQL 是最流行的开源数据库系统,可运行于几乎所有的操作系统平台。在《MySQL 安装》一文中详解介绍了安装步
- 最近在做搜索设计时,发现了两个容易纠结的小问题,在这里谈谈自己的一些分析。问题一:提交的关键字是哪个?凡客的这个例子中,搜索建议“时尚斜拉链
- 请问鼠标移过去就出现二级菜单代码怎么写啊 <head><style type="tex
- 一、背景有一段代码,因为调用了封装的方法,所以会有一些本来不需要的返回值。这些返回值该怎么处理才能让它消失呢。有的人会说,清除变量不就好了吗
- 1、例子:拟合一种函数Func,此处为一个指数函数。出处:SciPy v1.1.0 Reference Guide#Headerimport
- 数据集数据集为Barcelona某段时间内的气象数据,其中包括温度、湿度以及风速等。本文将简单搭建来对风速进行预测。特征构造对于风速的预测,
- 前言我们前面对matplotlib模块底层结构学习,对其pyplot类(脚本层)类提供的绘制折线图、柱状图、饼图、直方图等统计图表的相关方法
- 简介urlparse模块主要是用于解析url中的参数 对url按照一定格式进行 拆分或拼接。urlparse库用于把url解析为
- 原始结构:Column1 Column2----------- ----------1 A1 B2 C2 D2 E3 F查询效果:Colum
- 本文实例为大家分享了wxPython绘图模块wxPyPlot实现数据可视化的具体代码,供大家参考,具体内容如下#-*- coding: ut
- 本文实例为大家分享了python实现录音功能的具体代码,供大家参考,具体内容如下# -*- coding: utf-8 -*-import