python-绘图与可视化( 四 )


python-绘图与可视化

文章插图
import matplotlib.pyplot as pltnum_list = [1506,3500,3467,1366,200]pyplot.xlabel("Types of Students")pyplot.ylabel("Frequency")pyplot.title("Numbers of Books Students Read")plt.bar(range(len(num_list)), num_list,color="green")import seaborn as snssns.set_style("whitegrid")plt.show()
python-绘图与可视化

文章插图
②饼形图 。饼形图是以扇形的面积来指代某种类型的频率,使用Matplotlib对图书借阅量这一定性变量绘制饼形图的代码如下:
import numpy as npimport matplotlib.mlab as mlabimport matplotlib.pyplot as pltlabels=['A','B','C','D','E']X=[257,145,32,134,252]fig = plt.figure()plt.pie(X,labels=labels,autopct='%1.1f%%') #画饼图(数据,数据对应的标签,百分数保留两位小数点)plt.title("Numbers of Books Student Read")plt.show()
python-绘图与可视化

文章插图
(2)定量分析直方图类似于柱状图,是用柱的高度来指代频数,不同的是其将定量数据划分为若干连续的区间,在这些连续的区间上绘制柱 。①直方图 。使用Matplotlib对身高这一定量变量绘制直方图的代码如下:
#绘制直方图def drawHist(heights):    #创建直方图    #第一个参数为待绘制的定量数据,不同于定性数据,这里并没有实现进行频数统计    #第二个参数为划分的区间个数    pyplot.hist(heights,100)    pyplot.xlabel('Heights')    pyplot.ylabel('Frequency')    pyplot.title('Height of Students')    pyplot.show()drawHist(heights)
python-绘图与可视化

文章插图
累积曲线:使用Matplotlib对身高这一定量变量绘制累积曲线的代码如下:
#绘制累积曲线def drawCumulativaHist(heights):    #创建累积曲线    #第一个参数为待绘制的定量数据    #第二个参数为划分的区间个数    #normal参数为是否无量纲化    #histtype参数为‘step’,绘制阶梯状的曲线    #cumulative参数为是否累积    pyplot.hist(heights,20,normed=True,histtype='step',cumulative=True)    pyplot.xlabel('Heights')    pyplot.ylabel('Frequency')    pyplot.title('Heights of Students')    pyplot.show()drawCumulativaHist(heights)
python-绘图与可视化

文章插图
(3)关系分析
散点图 。在散点图中,分别以自变量和因变量作为横坐标 。当自变量与因变量线性相关时,散点图中的点近似分布在一条直线上 。我们以身高作为自变量,体重作为因变量,讨论身高对体重的影响 。使用Matplotlib绘制散点图的代码如下:
#绘制散点图def drawScatter(heights,weights):    #创建散点图    #第一个参数为点的横坐标    #第二个参数为点的纵坐标    pyplot.scatter(heights,weights)    pyplot.xlabel('Heights')    pyplot.ylabel('Weight')    pyplot.title('Heights & Weight of Students')    pyplot.show()drawScatter(heights,weights)
python-绘图与可视化

文章插图
(4)探索分析
箱型图 。在不明确数据分析的目标时,我们对数据进行一些探索性的分析,可以知道数据的中心位置、发散程度及偏差程度 。使用Matplotlib绘制关于身高的箱型图代码如下:
#绘制箱型图def drawBox(heights):    #创建箱型图    #第一个参数为待绘制的定量数据    #第二个参数为数据的文字说明    pyplot.boxplot([heights],labels=['Heights'])    pyplot.title('Heights of Students')    pyplot.show()drawBox(heights)
python-绘图与可视化

文章插图
注:

推荐阅读