最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python可視化庫(kù)之HoloViews的使用教程

 更新時(shí)間:2022年02月23日 10:32:00   作者:Python學(xué)習(xí)與數(shù)據(jù)挖掘  
本文主要為大家介紹了Python中一個(gè)優(yōu)秀的可視化庫(kù)—HoloViews,不僅能實(shí)現(xiàn)一些常見(jiàn)的統(tǒng)計(jì)圖表繪制,而且其還擁有Matplotlib、Seaborn等庫(kù)所不具備的交互效果,快跟隨小編一起了解一下吧

最近一直在整理統(tǒng)計(jì)圖表的繪制方法,發(fā)現(xiàn)Python中除了經(jīng)典Seaborn庫(kù)外,還有一些優(yōu)秀的可交互的第三方庫(kù)也能實(shí)現(xiàn)一些常見(jiàn)的統(tǒng)計(jì)圖表繪制,而且其還擁有Matplotlib、Seaborn等庫(kù)所不具備的交互效果。

當(dāng)然,同時(shí)也能繪制出版級(jí)別的圖表要求,此外,一些在使用Matplotlib需自定義函數(shù)才能繪制的圖表在一些第三方庫(kù)中都集成了,這也大大縮短了繪圖時(shí)間。

今天我就詳細(xì)介紹一個(gè)優(yōu)秀的第三方庫(kù)-HoloViews,內(nèi)容主要如下:

  • Python-HoloViews庫(kù)介紹
  • Python-HoloViews庫(kù)樣例介紹

Python-HoloViews庫(kù)介紹

Python-HoloViews庫(kù)作為一個(gè)開(kāi)源的可視化庫(kù),其目的是使數(shù)據(jù)分析結(jié)果和可視化完美銜接,其默認(rèn)的繪圖主題和配色以及較少的繪圖代碼量,可以使你專(zhuān)注于數(shù)據(jù)分析本身,同時(shí)其統(tǒng)計(jì)繪圖功能也非常優(yōu)秀。更多關(guān)于HoloViews庫(kù)的介紹,可參考:Python-HoloViews庫(kù)官網(wǎng)[1]

Python-HoloViews庫(kù)樣例介紹

這一部分小編重點(diǎn)放在一些統(tǒng)計(jì)圖表上,其繪制結(jié)果不僅可以在網(wǎng)頁(yè)上交互,同時(shí)其默認(rèn)的繪圖結(jié)果也完全滿(mǎn)足出版界別的要求,主要內(nèi)容如下(以下圖表都是可交互的):

密度圖+箱線(xiàn)圖

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

密度圖+箱線(xiàn)圖

散點(diǎn)圖+橫線(xià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)圖+橫線(xià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可視化庫(kù)HoloViews,著重介紹了其中統(tǒng)計(jì)圖表部分,這個(gè)庫(kù)也會(huì)在小編整理的資料中出現(xiàn),對(duì)于一些常見(jiàn)且使用Matplotlib較難繪制的圖表較為友好,感興趣的小伙伴可以學(xué)習(xí)下哦~~

參考資料

[1]Python-HoloViews庫(kù)官網(wǎng): https://holoviews.org/。

[2]Python-HoloViews樣例: https://holoviews.org/gallery/index.html。

以上就是Python可視化庫(kù)之HoloViews的使用教程的詳細(xì)內(nèi)容,更多關(guān)于Python HoloViews庫(kù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

靖远县| 察隅县| 德化县| 蒙阴县| 常宁市| 牡丹江市| 东莞市| 汕尾市| 五指山市| 灵川县| 资中县| 日照市| 潞西市| 龙游县| 刚察县| 渑池县| 平谷区| 吉首市| 墨江| 蓝山县| 林州市| 子长县| 咸宁市| 瑞丽市| 明星| 田阳县| 北京市| 鄂托克旗| 信宜市| 玉门市| 景谷| 方山县| 牡丹江市| 睢宁县| 龙岩市| 尤溪县| 普陀区| 鄂尔多斯市| 旬阳县| 乐安县| 镇宁|