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

熱線電話:13121318867

登錄
首頁精彩閱讀面向小白的最全Python可視化教程,超全的
面向小白的最全Python可視化教程,超全的
2022-03-28
收藏
面向小白的最全<a href='/map/<a href='/map/python/' style='color:#000;font-size:inherit;'>python</a>keshihua/' style='color:#000;font-size:inherit;'>Python可視化</a>教程,超全的

作者:俊欣

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

今天小編總結歸納了若干個常用的可視化圖表,并且通過調(diào)用plotly、matplotlibaltair、bokehseaborn等模塊來分別繪制這些常用的可視化圖表,最后無論是繪制可視化的代碼,還是會指出來的結果都會通過調(diào)用streamlit模塊展示在一個可視化大屏,出來的效果如下圖所示

面向小白的最全<a href='/map/<a href='/map/python/' style='color:#000;font-size:inherit;'>python</a>keshihua/' style='color:#000;font-size:inherit;'>Python可視化</a>教程,超全的

那我們接下去便一步一步開始可視化大屏的制作吧!

標題、副標題以及下拉框

首先我們對標題、副標題部分的內(nèi)容,代碼如下

with st.container():
    st.title("pythonkeshihua/' style='color:#000;font-size:inherit;'>Python可視化合集")
    st.header("經(jīng)典常用的pythonkeshihua/' style='color:#000;font-size:inherit;'>Python可視化模塊")
    st.write("""包括代碼和可視化圖表展示""")

然后便是下拉框的制作,代碼如下

plot_types = ( "Scatter", "Histogram", "Bar", "Line", "Boxplot" ) # 選擇繪制的圖表種類 chart_type = st.selectbox("Choose your chart type", plot_types)

with st.container():
    st.subheader(f"Showing:  {chart_type}")
    st.write("")

對于圖表的展示可以選擇是“雙排式”的,如下圖所示

面向小白的最全<a href='/map/<a href='/map/python/' style='color:#000;font-size:inherit;'>python</a>keshihua/' style='color:#000;font-size:inherit;'>Python可視化</a>教程,超全的

也可以選擇是沉浸式的,也即是“單排式”的,如下圖所示

面向小白的最全<a href='/map/<a href='/map/python/' style='color:#000;font-size:inherit;'>python</a>keshihua/' style='color:#000;font-size:inherit;'>Python可視化</a>教程,超全的

代碼如下

two_cols = st.checkbox("2 columns?", True) if two_cols:
    col1, col2 = st.columns(2) # 展示圖表 if two_cols: with col1:
        show_plot(kind="Matplotlib") with col2:
        show_plot(kind="Seaborn") with col1:
        show_plot(kind="Plotly Express") with col2:
        show_plot(kind="Altair") with col1:
        show_plot(kind="Pandas Matplotlib") with col2:
        show_plot(kind="Bokeh") else: with st.container(): for lib in libs:
            show_plot(kind=lib)

對于雙排式的展示方式而言,col1也就是左邊,放置的是matplotlib、plotly、以及pandas繪制出來的圖表,右邊也就是col2也就是右邊,放置的是seaborn、altair以及bokeh繪制出來的圖表,而上述代碼中調(diào)用的show_plot()函數(shù)代碼如下

# 生成圖表 def show_plot(kind: str): st.write(kind) if kind == "Matplotlib":
        plot = matplotlib_plot(chart_type, df)
        st.pyplot(plot) elif kind == "Seaborn":
        plot = sns_plot(chart_type, df)
        st.pyplot(plot) elif kind == "Plotly Express":
        plot = plotly_plot(chart_type, df)
        st.plotly_chart(plot, use_container_width=True) elif kind == "Altair":
        plot = altair_plot(chart_type, df)
        st.altair_chart(plot, use_container_width=True) elif kind == "Pandas Matplotlib":
        plot = pd_plot(chart_type, df)
        st.pyplot(plot) elif kind == "Bokeh":
        plot = bokeh_plot(chart_type, df)
        st.bokeh_chart(plot, use_container_width=True)

是一系列if...else...的判斷,當繪制圖表的模塊是matplotlib時就調(diào)用對應的matplotlib_plot()函數(shù),當繪制圖表的模塊是seaborn時就調(diào)用對應的sns_plot()函數(shù),依次同理。我們來看其中一個函數(shù)sns_plot()的具體邏輯,代碼如下

def sns_plot(chart_type: str, df): """ 生成seaborn繪制的圖表 """ fig, ax = plt.subplots() if chart_type == "Scatter": with st.echo():
            sns.scatterplot(
                data=df,
                x="bill_depth_mm",
                y="bill_length_mm",
                hue="species",
            )
            plt.title("Bill Depth by Bill Length") elif chart_type == "Histogram": with st.echo():
            sns.histplot(data=df, x="bill_depth_mm")
            plt.title("Count of Bill Depth Observations") elif chart_type == "Bar": with st.echo():
            sns.barplot(data=df, x="species", y="bill_depth_mm")
            plt.title("Mean Bill Depth by Species") elif chart_type == "Boxplot": with st.echo():
            sns.boxplot(data=df["bill_depth_mm"].dropna())
            plt.title("Bill Depth Observations") elif chart_type == "Line": with st.echo():
            sns.lineplot(data=df, x=df.index, y="bill_length_mm")
            plt.title("Bill Length Over Time") return fig

其實也是一系列if...else...的判斷,當所要繪制的圖表是散點圖時,調(diào)用的是sns.scatterplot()函數(shù),所要繪制的是直方圖時,調(diào)用的是sns.histplot(),繪制的柱狀圖或者是折線圖時也是同理

最后要是我們想要查看源數(shù)據(jù)時,也可以查看,代碼如下

# 展示源數(shù)據(jù) with st.container():
    show_data = st.checkbox("See the raw data?") if show_data:
        df # 要點 st.subheader("Notes")
    st.write( """
        - 這個應用是通過python當中的streamlit模塊制作出來的
        - 關注"關于數(shù)據(jù)分析與可視化",學習更多數(shù)據(jù)分析和可視化知識與技能
        """ )

output

面向小白的最全<a href='/map/<a href='/map/python/' style='color:#000;font-size:inherit;'>python</a>keshihua/' style='color:#000;font-size:inherit;'>Python可視化</a>教程,超全的

數(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(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 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); }