tft每日頭條

 > 生活

 > python數據可視化初級教程

python數據可視化初級教程

生活 更新时间:2024-09-01 20:14:53

python數據可視化初級教程(面向小白的最全Python可視化教程)1

作者:俊欣

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

今天小編總結歸納了若幹個常用的可視化圖表,并且通過調用plotly、matplotlib、altair、bokeh和seaborn等模塊來分别繪制這些常用的可視化圖表,最後無論是繪制可視化的代碼,還是會指出來的結果都會通過調用streamlit模塊展示在一個可視化大屏,出來的效果如下圖所示

python數據可視化初級教程(面向小白的最全Python可視化教程)2

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

标題、副标題以及下拉框

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

with st.container(): st.title("python可視化合集") st.header("經典常用的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("")

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

python數據可視化初級教程(面向小白的最全Python可視化教程)3

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

python數據可視化初級教程(面向小白的最全Python可視化教程)4

代碼如下

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

python數據可視化初級教程(面向小白的最全Python可視化教程)5

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关生活资讯推荐

热门生活资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved