熱線電話: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>教程,超全的

作者:俊欣

來源:關于數據分析與可視化

今天小編總結歸納了若干個常用的可視化圖表,并且通過調用plotly、matplotlib、altair、bokehseaborn等模塊來分別繪制這些常用的可視化圖表,最后無論是繪制可視化的代碼,還是會指出來的結果都會通過調用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>教程,超全的

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

標題、副標題以及下拉框

首先我們對標題、副標題部分的內容,代碼如下

with st.container():
    st.title("pythonkeshihua/' style='color:#000;font-size:inherit;'>Python可視化合集")
    st.header("經典常用的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繪制出來的圖表,而上述代碼中調用的show_plot()函數代碼如下

# 生成圖表 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時就調用對應的matplotlib_plot()函數,當繪制圖表的模塊是seaborn時就調用對應的sns_plot()函數,依次同理。我們來看其中一個函數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...的判斷,當所要繪制的圖表是散點圖時,調用的是sns.scatterplot()函數,所要繪制的是直方圖時,調用的是sns.histplot(),繪制的柱狀圖或者是折線圖時也是同理

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

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

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>教程,超全的

數據分析咨詢請掃描二維碼

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

數據分析師資訊
更多

OK
客服在線
立即咨詢
日韩人妻系列无码专区视频,先锋高清无码,无码免费视欧非,国精产品一区一区三区无码
客服在線
立即咨詢