99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀「Plotly快速入門」用Plotly繪制了幾張精湛的圖表,美翻了
「Plotly快速入門」用Plotly繪制了幾張精湛的圖表,美翻了
2022-06-06
收藏

作者:俊欣

來源:關(guān)于數(shù)據(jù)分析與可視化

說到Python當(dāng)中的可視化模塊,相信大家用的比較多的還是matplotlib、seaborn等模塊,今天小編來嘗試用Plotly模塊為大家繪制可視化圖表,和前兩者相比,用Plotly模塊會指出來的可視化圖表有著很強(qiáng)的交互性。

柱狀圖

我們先導(dǎo)入后面需要用到的模塊并且生成一批假數(shù)據(jù),

import numpy as np

import plotly.graph_objects as go

# create dummy data

vals = np.ceil(100 * np.random.rand(5)).astype(int)

keys = ["A", "B", "C", "D", "E"]

我們基于所生成的假數(shù)據(jù)來繪制柱狀圖,代碼如下

fig = go.Figure()

fig.add_trace(

go.Bar(x=keys, y=vals)

fig.update_layout(height=600, width=600)

fig.show()

output

可能讀者會感覺到繪制出來的圖表略顯簡單,我們再來完善一下,添加上標(biāo)題和注解,代碼如下

# create figure

fig = go.Figure()

# 繪制圖表

fig.add_trace(

go.Bar(x=keys, y=vals, hovertemplate="<b>Key:</b> %{x}<br><b>Value:</b> %{y}<extra></extra>")

# 更新完善圖表

fig.update_layout(

font_family="Averta",

hoverlabel_font_family="Averta",

title_text="直方圖",

xaxis_title_text="X軸-鍵",

xaxis_title_font_size=18,

xaxis_tickfont_size=16,

yaxis_title_text="Y軸-值",

yaxis_title_font_size=18,

yaxis_tickfont_size=16,

hoverlabel_font_size=16,

height=600,

width=600

fig.show()

output

分組條形圖和堆積條形圖

例如我們有多組數(shù)據(jù)想要繪制成柱狀圖的話,我們先來創(chuàng)建好數(shù)據(jù)集

vals_2 = np.ceil(100 * np.random.rand(5)).astype(int)

vals_3 = np.ceil(100 * np.random.rand(5)).astype(int)

vals_array = [vals, vals_2, vals_3]

然后我們遍歷獲取列表中的數(shù)值并且繪制成條形圖,代碼如下

# 生成畫布

fig = go.Figure()

# 繪制圖表

for i, vals in enumerate(vals_array):

fig.add_trace(

go.Bar(x=keys, y=vals, name=f"Group {i+1}", hovertemplate=f"<b>Group {i+1}</b><br><b>Key:</b> %{{x}}<br><b>Value:</b> %{{y}}<extra></extra>")

# 完善圖表

fig.update_layout(

barmode="group",

fig.show()

output

而我們想要變成堆積狀的條形圖,只需要修改代碼中的一處即可,將fig.update_layout(barmode="group")修改成fig.update_layout(barmode="group")即可,我們來看一下出來的樣子

箱型圖

箱型圖數(shù)據(jù)統(tǒng)計分析當(dāng)中也是應(yīng)用相當(dāng)廣泛的,我們先來創(chuàng)建兩個假數(shù)據(jù)

# create dummy data for boxplots

y1 = np.random.normal(size=1000)

y2 = np.random.normal(size=1000)

我們將上面生成的數(shù)據(jù)繪制成箱型圖,代碼如下

# 生成畫布

fig = go.Figure()

# 繪制圖表

fig.add_trace(

go.Box(y=y1, name="Dataset 1"),

fig.add_trace(

go.Box(y=y2, name="Dataset 2"),

fig.update_layout(

fig.show()

output

散點圖氣泡圖

接下來我們嘗試來繪制一張散點圖,也是一樣的步驟,我們想嘗試生成一些假數(shù)據(jù),代碼如下

x = [i for i in range(1, 10)]

y = np.ceil(1000 * np.random.rand(10)).astype(int)

然后我們來繪制散點圖,調(diào)用的是Scatter()方法,代碼如下

# create figure

fig = go.Figure()

fig.add_trace(

go.Scatter(x=x, y=y, mode="markers", hovertemplate="<b>x:</b> %{x}<br><b>y:</b> %{y}<extra></extra>")

fig.update_layout(

fig.show()

output

那么氣泡圖的話就是在散點圖的基礎(chǔ)上,根據(jù)數(shù)值的大小來設(shè)定散點的大小,我們再來創(chuàng)建一些假數(shù)據(jù)用來設(shè)定散點的大小,代碼如下

s = np.ceil(30 * np.random.rand(5)).astype(int)

我們將上面用作繪制散點圖的代碼稍作修改,通過marker_size參數(shù)來設(shè)定散點的大小,如下所示

fig = go.Figure()

fig.add_trace(

go.Scatter(x=x, y=y, mode="markers", marker_size=s, text=s, hovertemplate="<b>x:</b> %{x}<br><b>y:</b> %{y}<br><b>Size:</b> %{text}<extra></extra>")

fig.update_layout(

fig.show()

output

直方圖

直方圖相比較于上面提到的幾種圖表,總體上來說會稍微有點丑,但是通過直方圖,讀者可以更加直觀地感受到數(shù)據(jù)的分布,我們先來創(chuàng)建一組假數(shù)據(jù),代碼如下

## 創(chuàng)建假數(shù)據(jù)

data = np.random.normal(size=1000)

然后我們來繪制直方圖,調(diào)用的是Histogram()方法,代碼如下

# 創(chuàng)建畫布

fig = go.Figure()

# 繪制圖表

fig.add_trace(

go.Histogram(x=data, hovertemplate="<b>Bin Edges:</b> %{x}<br><b>Count:</b> %{y}<extra></extra>")

fig.update_layout(

height=600,

width=600

fig.show()

output

我們再在上述圖表的基礎(chǔ)之上再進(jìn)行進(jìn)一步的格式優(yōu)化,代碼如下

# 生成畫布

fig = go.Figure()

# 繪制圖表

fig.add_trace(

go.Histogram(x=data, histnorm="probability", hovertemplate="<b>Bin Edges:</b> %{x}<br><b>Count:</b> %{y}<extra></extra>")

fig.update_layout(

fig.show()

output

多個子圖拼湊到一塊兒

相信大家都知道在matplotlib模塊當(dāng)中的subplots()方法可以將多個子圖拼湊到一塊兒,那么同樣地在plotly當(dāng)中也可以同樣地將多個子圖拼湊到一塊兒,調(diào)用的是plotly模塊當(dāng)中make_subplots函數(shù)

from plotly.subplots import make_subplots

## 2行2列的圖表

fig = make_subplots(rows=2, cols=2)

## 生成一批假數(shù)據(jù)用于圖表的繪制

x = [i for i in range(1, 11)]

y = np.ceil(100 * np.random.rand(10)).astype(int)

s = np.ceil(30 * np.random.rand(10)).astype(int)

y1 = np.random.normal(size=5000)

y2 = np.random.normal(size=5000)

接下來我們將所要繪制的圖表添加到add_trace()方法當(dāng)中,代碼如下

# 繪制圖表

fig.add_trace(

go.Bar(x=x, y=y, hovertemplate="<b>x:</b> %{x}<br><b>y:</b> %{y}<extra></extra>"),

row=1, col=1

fig.add_trace(

go.Histogram(x=y1, hovertemplate="<b>Bin Edges:</b> %{x}<br><b>Count:</b> %{y}<extra></extra>"),

row=1, col=2

fig.add_trace(

go.Scatter(x=x, y=y, mode="markers", marker_size=s, text=s, hovertemplate="<b>x:</b> %{x}<br><b>y:</b> %{y}<br><b>Size:</b> %{text}<extra></extra>"),

row=2, col=1

fig.add_trace(

go.Box(y=y1, name="Dataset 1"),

row=2, col=2

fig.add_trace(

go.Box(y=y2, name="Dataset 2"),

row=2, col=2

fig.update_xaxes(title_font_size=18, tickfont_size=16)

fig.update_yaxes(title_font_size=18, tickfont_size=16)

fig.update_layout(

fig.show()

output

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }