2022年 11月 3日

超全的 Python 可视化教程,收藏

今天小编总结归纳了若干个常用的可视化图表,并且通过调用plotlymatplotlibaltairbokehseaborn等模块来分别绘制这些常用的可视化图表,最后无论是绘制可视化的代码,还是会指出来的结果都会通过调用streamlit模块展示在一个可视化大屏,出来的效果如下图所示

那我们接下去便一步一步开始可视化大屏的制作吧!

标题、副标题以及下拉框

首先我们对标题、副标题部分的内容,代码如下

  1. with st.container():
  2.     st.title("Python可视化合集")
  3.     st.header("经典常用的Python可视化模块")
  4.     st.write("""包括代码和可视化图表展示""")

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

  1. plot_types = (
  2.     "Scatter",
  3.     "Histogram",
  4.     "Bar",
  5.     "Line",
  6.     "Boxplot"
  7. )
  8. # 选择绘制的图表种类
  9. chart_type = st.selectbox("Choose your chart type", plot_types)
  10. with st.container():
  11.     st.subheader(f"Showing:  {chart_type}")
  12.     st.write("")

对于图表的展示可以选择是“双排式”的,如下图所示

也可以选择是沉浸式的,也即是“单排式”的,如下图所示

代码如下

  1. two_cols = st.checkbox("2 columns?"True)
  2. if two_cols:
  3.     col1, col2 = st.columns(2)
  4.     
  5. # 展示图表
  6. if two_cols:
  7.     with col1:
  8.         show_plot(kind="Matplotlib")
  9.     with col2:
  10.         show_plot(kind="Seaborn")
  11.     with col1:
  12.         show_plot(kind="Plotly Express")
  13.     with col2:
  14.         show_plot(kind="Altair")
  15.     with col1:
  16.         show_plot(kind="Pandas Matplotlib")
  17.     with col2:
  18.         show_plot(kind="Bokeh")
  19. else:
  20.     with st.container():
  21.         for lib in libs:
  22.             show_plot(kind=lib)

对于双排式的展示方式而言,col1也就是左边,放置的是matplotlibplotly、以及pandas绘制出来的图表,右边也就是col2也就是右边,放置的是seabornaltair以及bokeh绘制出来的图表,而上述代码中调用的show_plot()函数代码如下

  1. # 生成图表
  2. def show_plot(kind: str):
  3.     st.write(kind)
  4.     if kind == "Matplotlib":
  5.         plot = matplotlib_plot(chart_type, df)
  6.         st.pyplot(plot)
  7.     elif kind == "Seaborn":
  8.         plot = sns_plot(chart_type, df)
  9.         st.pyplot(plot)
  10.     elif kind == "Plotly Express":
  11.         plot = plotly_plot(chart_type, df)
  12.         st.plotly_chart(plot, use_container_width=True)
  13.     elif kind == "Altair":
  14.         plot = altair_plot(chart_type, df)
  15.         st.altair_chart(plot, use_container_width=True)
  16.     elif kind == "Pandas Matplotlib":
  17.         plot = pd_plot(chart_type, df)
  18.         st.pyplot(plot)
  19.     elif kind == "Bokeh":
  20.         plot = bokeh_plot(chart_type, df)
  21.         st.bokeh_chart(plot, use_container_width=True)

是一系列if...else...的判断,当绘制图表的模块是matplotlib时就调用对应的matplotlib_plot()函数,当绘制图表的模块是seaborn时就调用对应的sns_plot()函数,依次同理。我们来看其中一个函数sns_plot()的具体逻辑,代码如下

  1. def sns_plot(chart_type: str, df):
  2.     """ 生成seaborn绘制的图表 """
  3.     fig, ax = plt.subplots()
  4.     if chart_type == "Scatter":
  5.         with st.echo():
  6.             sns.scatterplot(
  7.                 data=df,
  8.                 x="bill_depth_mm",
  9.                 y="bill_length_mm",
  10.                 hue="species",
  11.             )
  12.             plt.title("Bill Depth by Bill Length")
  13.     elif chart_type == "Histogram":
  14.         with st.echo():
  15.             sns.histplot(data=df, x="bill_depth_mm")
  16.             plt.title("Count of Bill Depth Observations")
  17.     elif chart_type == "Bar":
  18.         with st.echo():
  19.             sns.barplot(data=df, x="species", y="bill_depth_mm")
  20.             plt.title("Mean Bill Depth by Species")
  21.     elif chart_type == "Boxplot":
  22.         with st.echo():
  23.             sns.boxplot(data=df["bill_depth_mm"].dropna())
  24.             plt.title("Bill Depth Observations")
  25.     elif chart_type == "Line":
  26.         with st.echo():
  27.             sns.lineplot(data=df, x=df.index, y="bill_length_mm")
  28.             plt.title("Bill Length Over Time")
  29.     return fig

其实也是一系列if...else...的判断,当所要绘制的图表是散点图时,调用的是sns.scatterplot()函数,所要绘制的是直方图时,调用的是sns.histplot(),绘制的柱状图或者是折线图时也是同理

最后要是我们想要查看源数据时,也可以查看,代码如下

  1. # 展示源数据
  2. with st.container():
  3.     show_data = st.checkbox("See the raw data?")
  4.     if show_data:
  5.         df
  6.     # 要点
  7.     st.subheader("Notes")
  8.     st.write(
  9.         """
  10.         - 这个应用是通过python当中的streamlit模块制作出来的
  11.         - 关注"关于数据分析与可视化",学习更多数据分析和可视化知识与技能
  12.         """
  13.     )

output

以上就是本次分享的所有内容,如果你觉得文章还不错,欢迎关注公众号:Python编程学习圈,每日干货分享,发送“J”还可领取大量学习资料。或是前往编程学习网,了解更多编程技术知识。