面向小白的最全 Python 可视化教程,超全的!
source link: http://www.yitb.com/article-15138
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
作者 |俊欣
来源丨关于数据分析与可视化
今天小编总结归纳了若干个常用的可视化图表,并且通过调用plotly
、matplotlib
、altair
、bokeh
和seaborn
等模块来分别绘制这些常用的可视化图表,最后无论是绘制可视化的代码,还是会指出来的结果都会通过调用streamlit
模块展示在一个可视化大屏,出来的效果如下图所示
标题、副标题以及下拉框
首先我们对标题、副标题部分的内容,代码如下:withst.container():
st.title("Python可视化合集")
st.header("经典常用的Python可视化模块")
st.write("""包括代码和可视化图表展示""")
然后便是下拉框的制作,代码如下:plot_types=(
"Scatter",
"Histogram",
"Bar",
"Line",
"Boxplot"
)
#选择绘制的图表种类
chart_type=st.selectbox("Chooseyourcharttype",plot_types)
withst.container():
st.subheader(f"Showing:{chart_type}")
st.write("")
对于图表的展示可以选择是“双排式”的,如下图所示也可以选择是沉浸式的,也即是“单排式”的,如下图所示代码如下:two_cols=st.checkbox("2columns?",True)
iftwo_cols:
col1,col2=st.columns(2)
#展示图表
iftwo_cols:
withcol1:
show_plot(kind="Matplotlib")
withcol2:
show_plot(kind="Seaborn")
withcol1:
show_plot(kind="PlotlyExpress")
withcol2:
show_plot(kind="Altair")
withcol1:
show_plot(kind="PandasMatplotlib")
withcol2:
show_plot(kind="Bokeh")
else:
withst.container():
forlibinlibs:
show_plot(kind=lib)
对于双排式的展示方式而言,col1
也就是左边,放置的是matplotlib
、plotly
、以及pandas
绘制出来的图表,右边也就是col2
也就是右边,放置的是seaborn
、altair
以及bokeh
绘制出来的图表,而上述代码中调用的show_plot()
函数代码如下:
#生成图表
defshow_plot(kind:str):
st.write(kind)
ifkind=="Matplotlib":
plot=matplotlib_plot(chart_type,df)
st.pyplot(plot)
elifkind=="Seaborn":
plot=sns_plot(chart_type,df)
st.pyplot(plot)
elifkind=="PlotlyExpress":
plot=plotly_plot(chart_type,df)
st.plotly_chart(plot,use_container_width=True)
elifkind=="Altair":
plot=altair_plot(chart_type,df)
st.altair_chart(plot,use_container_width=True)
elifkind=="PandasMatplotlib":
plot=pd_plot(chart_type,df)
st.pyplot(plot)
elifkind=="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()
的具体逻辑,代码如下:
defsns_plot(chart_type:str,df):
"""生成seaborn绘制的图表"""
fig,ax=plt.subplots()
ifchart_type=="Scatter":
withst.echo():
sns.scatterplot(
data=df,
x="bill_depth_mm",
y="bill_length_mm",
hue="species",
)
plt.title("BillDepthbyBillLength")
elifchart_type=="Histogram":
withst.echo():
sns.histplot(data=df,x="bill_depth_mm")
plt.title("CountofBillDepthObservations")
elifchart_type=="Bar":
withst.echo():
sns.barplot(data=df,x="species",y="bill_depth_mm")
plt.title("MeanBillDepthbySpecies")
elifchart_type=="Boxplot":
withst.echo():
sns.boxplot(data=df["bill_depth_mm"].dropna())
plt.title("BillDepthObservations")
elifchart_type=="Line":
withst.echo():
sns.lineplot(data=df,x=df.index,y="bill_length_mm")
plt.title("BillLengthOverTime")
returnfig
其实也是一系列if...else...
的判断,当所要绘制的图表是散点图时,调用的是sns.scatterplot()
函数,所要绘制的是直方图时,调用的是sns.histplot()
,绘制的柱状图或者是折线图时也是同理
#展示源数据
withst.container():
show_data=st.checkbox("Seetherawdata?")
ifshow_data:
df
#要点
st.subheader("Notes")
st.write(
"""
-这个应用是通过python当中的streamlit模块制作出来的
-关注"关于数据分析与可视化",学习更多数据分析和可视化知识与技能
"""
)
output
分享
点收藏
点点赞
点在看
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK