Python可視化庫之HoloViews的使用教程
最近一直在整理統(tǒng)計(jì)圖表的繪制方法,發(fā)現(xiàn)Python中除了經(jīng)典Seaborn庫外,還有一些優(yōu)秀的可交互的第三方庫也能實(shí)現(xiàn)一些常見的統(tǒng)計(jì)圖表繪制,而且其還擁有Matplotlib、Seaborn等庫所不具備的交互效果。
當(dāng)然,同時(shí)也能繪制出版級(jí)別的圖表要求,此外,一些在使用Matplotlib需自定義函數(shù)才能繪制的圖表在一些第三方庫中都集成了,這也大大縮短了繪圖時(shí)間。
今天我就詳細(xì)介紹一個(gè)優(yōu)秀的第三方庫-HoloViews,內(nèi)容主要如下:
- Python-HoloViews庫介紹
- Python-HoloViews庫樣例介紹
Python-HoloViews庫介紹
Python-HoloViews庫作為一個(gè)開源的可視化庫,其目的是使數(shù)據(jù)分析結(jié)果和可視化完美銜接,其默認(rèn)的繪圖主題和配色以及較少的繪圖代碼量,可以使你專注于數(shù)據(jù)分析本身,同時(shí)其統(tǒng)計(jì)繪圖功能也非常優(yōu)秀。更多關(guān)于HoloViews庫的介紹,可參考:Python-HoloViews庫官網(wǎng)[1]
Python-HoloViews庫樣例介紹
這一部分小編重點(diǎn)放在一些統(tǒng)計(jì)圖表上,其繪制結(jié)果不僅可以在網(wǎng)頁上交互,同時(shí)其默認(rèn)的繪圖結(jié)果也完全滿足出版界別的要求,主要內(nèi)容如下(以下圖表都是可交互的):
密度圖+箱線圖
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

密度圖+箱線圖
散點(diǎn)圖+橫線圖
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

散點(diǎn)圖+橫線圖
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]
總結(jié)
今天的推文,小編主要介紹了Python可視化庫HoloViews,著重介紹了其中統(tǒng)計(jì)圖表部分,這個(gè)庫也會(huì)在小編整理的資料中出現(xiàn),對(duì)于一些常見且使用Matplotlib較難繪制的圖表較為友好,感興趣的小伙伴可以學(xué)習(xí)下哦~~
參考資料
[1]Python-HoloViews庫官網(wǎng): https://holoviews.org/。
[2]Python-HoloViews樣例: https://holoviews.org/gallery/index.html。
以上就是Python可視化庫之HoloViews的使用教程的詳細(xì)內(nèi)容,更多關(guān)于Python HoloViews庫的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Linux下升級(jí)安裝python3.8并配置pip及yum的教程
這篇文章主要介紹了Linux下升級(jí)安裝python3.8并配置pip及yum的教程,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
Python獲取本機(jī)IP/MAC多網(wǎng)卡方法示例
這篇文章主要為大家介紹了Python獲取本機(jī)IP/MAC多網(wǎng)卡方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Pytorch中Tensor與各種圖像格式的相互轉(zhuǎn)化詳解
這篇文章主要介紹了Pytorch中Tensor與各種圖像格式的相互轉(zhuǎn)化詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Python發(fā)送以整個(gè)文件夾的內(nèi)容為附件的郵件的教程
這篇文章主要介紹了Python發(fā)送以整個(gè)文件夾的內(nèi)容為附件的郵件的教程,普通我們?cè)谶\(yùn)營(yíng)商免費(fèi)郵箱中發(fā)附件通常只能發(fā)文件而不能發(fā)文件夾,而該腳本則可以實(shí)現(xiàn)文件夾的發(fā)送(自己動(dòng)手編程的強(qiáng)大之處:D),需要的朋友可以參考下2015-05-05
python taipy庫輕松地將數(shù)據(jù)和機(jī)器學(xué)習(xí)模型轉(zhuǎn)為功能性Web應(yīng)用
taipy 是一個(gè)開源的 Python 庫,任何具有基本 Python 技能的人都可以使用,對(duì)于數(shù)據(jù)科學(xué)家、機(jī)器學(xué)習(xí)工程師和 Python 程序員來說,它是一個(gè)方便的工具,借助 Taipy,你可以輕松地將數(shù)據(jù)和機(jī)器學(xué)習(xí)模型轉(zhuǎn)變?yōu)楣δ苄缘?nbsp;Web 應(yīng)用程序2024-01-01
用scikit-learn和pandas學(xué)習(xí)線性回歸的方法
這篇文章主要介紹了用scikit-learn和pandas學(xué)習(xí)線性回歸的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
Python簡(jiǎn)單實(shí)現(xiàn)gif動(dòng)圖倒放示例
這篇文章主要為大家介紹了Python簡(jiǎn)單實(shí)現(xiàn)gif動(dòng)圖倒放的示例過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05

