环境和数据
软件:Python 3.7、vscode(非必须,其他IDE也可的)
数据集:鸢尾花数据集
程序源代码:点此下载绘制二维图像的源代码每个图像为一个函数,只需将调用函数的语句注释取消掉即可运行。
库
iris数据集
既然要使用这个数据集,首先我们来了解下这个数据集。read_csv方法用于读取硬盘上的文件,head方法用于输出数据集前五条记录。如下图所示,从左到右看,前五条记录中,数据默认编号为[0,4],ID唯一编码每条记录,取值为[1,150],iris数据集共150条记录。后面四个属性代表了花的特征,依次是花萼长度(Sepal Length)、花萼宽度(Sepal Length)、花朵长度(Petal Length)、花朵宽度(Petal Width),单位均为厘米cm。最后一列为花朵的种类。
通过describe方法查看数据集描述性信息,如下图所示。
简单来说,iris数据集的每条记录包括了一个唯一的编号,四个特征和花的种类。本次实验所用数据均来自于此。
二维图像
散点图
散点图是数据点在直角坐标系上的分布图。
scatter方法
scatter(x, y, color, alpha),前两个参数为横坐标和纵坐标,color为数据点颜色,alpha为数据点透明度
def plotScatter(iris):
plt.scatter(iris['Id'],iris['SepalLengthCm'], c='red', alpha=0.6)
plt.title("Scatter 1")
plt.savefig('./myfigure/Scatter 1.png')
plotScatter(iris)
plot方法
plot(kind, x, y, c),kind为图像类型,其他参数同scatter方法。
def oldIrisScatter(iris):
iris.plot(kind="scatter",x="SepalLengthCm",y="SepalWidthCm",c="green")
plt.title("Scatter 2")
plt.savefig('./myfigure/Scatter 2.png')
oldIrisScatter(iris)
Scatter by facet grid
seaborn
(iris,hue=“Species”,size=5).map(plt.scatter,“SepalLengthCm”,“SepalWidthCm”).add_legend()
在数据集的子集内可视化变量分布,row,col,hue,hue将变量视为沿着深度轴的第三维,不同级别用不同的颜色绘制。
def facetGridBySeaborn(iris):
sns.FacetGrid(iris,hue="Species",size=5).map(plt.scatter,"SepalLengthCm","SepalWidthCm").add_legend()
plt.title("Scatter by FacetGrid")
plt.savefig("./myFigure/Scatter By FacetGrid.png")
facetGridBySeaborn(iris)
jointPlot
同一个figure内显示二变量和单变量的直方图。
def jointPlotBySeaborn(iris):
sns.jointplot(x="SepalLengthCm",y="SepalWidthCm",data=iris,size=5)
plt.title("Scatter By Seaborn")
plt.savefig('./myfigure/Scatter By Seaborn.png')
jointPlotBySeaborn(iris)
boxPlot
def boxplot(iris):## We can look at an individual feature in Seaborn through a boxplot
sns.boxplot(x="Species",y="PetalLengthCm",data=iris)
plt.title("Boxplot")
plt.savefig("./myFigure/Boxplot.png")
boxplot(iris)
jitter=True,使得所有的点不会落在一个垂直的线上。
def boxplotWithpoints(iris):
sns.boxplot(x="Species", y="PetalLengthCm", data=iris)
sns.stripplot(x="Species", y="PetalLengthCm", data=iris, jitter=True, edgecolor="gray")
plt.title("boxplotWithPoints")
plt.savefig("./myFigure/boxplotWithPoints.png")
boxplotWithpoints(iris)
def boxplotWithoutId(iris):
iris.drop("Id", axis=1).boxplot(by="Species", figsize=(12,6))
plt.savefig("./myFigure/boxplotGroupedBySpecies.png")
# boxplotWithoutId(iris)
def facetGridWithLine(iris):
sns.FacetGrid(iris, hue="Species", size=6).map(sns.kdeplot, "PetalLengthCm").add_legend()
plt.title("facetGridWithLine")
plt.savefig("./myFigure/FacetGridWithLine.png")
facetGridWithLine(iris)
def hist(iris):
g = sns.FacetGrid(iris, col="Species")
g = g.map(plt.hist, "SepalWidthCm",bins=20)
plt.title("histByFacetGrid")
plt.savefig("./myFigure/histByFacetGrid.png")
# hist(iris)
def pairPlot(iris):
sns.pairplot(iris.drop("Id", axis=1,), hue="Species", size=3)
plt.title("pairPlot")
plt.savefig("./myFigure/pairPlot.png")
pairPlot(iris)
def parallelCoordinates(iris):
parallel_coordinates(iris.drop("Id", axis=1), "Species")
plt.title("parallelCoordinates")
plt.savefig("./myFigure/parallelCoordinates.png")
parallelCoordinates(iris)
三维图像(缓更)
源程序和数据集
补充说明
文章如有错误,欢迎通过CSDN平台联系我,也可发email给我iamzhouling@163.com