大家平時都會用到哪些可視化的工具呢?Python中又有哪些好用的可視化模塊呢?今天就給大家分享一個Python小技能,小白也
能學會的可視化教程,

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

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

標題、副標題以及下拉框
首先我們對標題、副標題部分的內容,代碼如下
Python學習交流Q群:906715085### 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學習交流Q群:906715085### 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()的具體邏輯,代碼如下
Python學習交流Q群:906715085#### 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

最后,今天給大家分享的這個可視化資料到這里就結束了,你明天想看什么,告訴我,我給你整理出來,關于今天的文章,有問
題的可以評論留言,我看見就會回你,沒有問題的小伙伴,記得給自己一個大大的贊,也給我一個大大的贊,

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/458273.html
標籤:其他
