matplotlib.pyplot 常用的绘图
绘制基本图形 - 散点图 - 折线图 - 条形图 - 直方图 - 饼状图 - 箱型图
散点图
函数原型:matplotlib.pyplot.scatter(x,y,s=20,c='b',marker='o',cmap=None,norm=None,vmin=None,vmax=None,alpha=None,linewidths=None,verts=None,hold=None,**kwargs)
x、y为数组 s 为形状的大小 c 为颜色[可选] marker 为形状[可选] camp 为色彩盘[可选] norm 为规范[可选]//缩放等 alpha 为透明度[可选] linewidth 为线宽[可选] vmin、vmax 为颜色阵列的最小值、最大值[可选]
官网链接:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.scatter.html
# 身高 和 体重 的散点图
import numpy as np
import matplotlib.pyplot as plt
height = [161,171,182,175,178,176]
weight = [50 ,58 ,80 ,69 ,71 ,62 ]
plt.scatter(height,weight,color="black",marker= '*' ,s= 30,alpha = 0.5)
#marker->形状 (* / o / < / > / v / - / x ...)
#
plt.show()
折线图
连接散点 函数原型:matplotlib.pyplot.plot( x,y,format_string,**kwargs) x,y 可以是列表,亦可以是元组 format_string 是显示格式
官网链接:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-10,10,100)#[-10,10],平均分成100份
#取份数小,明显为折线图
y = x**2
plt.plot(x,y)
plt.show()
条形图
函数原型:matplotlib.pyplot.bar(x,height,bottom,width,align,data,**kwargs) x 为定义柱状图的x轴 height 为柱状图的上平面 bottom 为柱状图的下平面,堆叠柱状图时常使用 align 为柱状图在x轴上的位置偏向
官网链接:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.bar.html
import numpy as np
import matplotlib.pyplot as plt
n = 5
y = [20,10,30,25,15]
index =list(np.arange(n))
#竖向条形图
p1 = plt.bar(index,y)
plt.show()
###############################################
import numpy as np
import matplotlib.pyplot as plt
n = 5
y = [20,10,30,25,15]
index =list(np.arange(n))
print(type(index),type(y))
#横向条形图
p2 = plt.bar(letf=0, bottom=index,height=0.5,width=y, orientation="horizontal")
p3 = plt.bar(letf=0, bottom=index,height=0.5,width=y)
plt.show()
###############################################
#多元竖向条形图
import numpy as np
import matplotlib.pyplot as plt
index = np.arange(4)
BJ = [52,55,63,53]
SH = [44,55,66,41]
bar_width = 0.3
plt.bar(x=index,height = BJ, width=bar_width,bottom=0,color = 'b')
plt.bar(x=index+bar_width,height=SH,width=bar_width,bottom=0,color='r')
plt.show()
##############################################
#堆叠条形图
import numpy as np
import matplotlib.pyplot as plt
index = np.arange(4)
BJ = [52,55,63,53]
SH = [44,55,66,41]
bar_width = 0.3
plt.bar(x=index,height = BJ, width=bar_width,bottom=0,color = 'b')
plt.bar(x=index,height=SH,width=bar_width,bottom=BJ,color='r')
plt.show()
直方图
函数原型: matplotlib.pyplot.hist(x, bins=None, range=None, density=None, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None, stacked=False, normed=None, *, data=None, **kwargs)
x 为长度等/不等的数组序列 bins 为直方图柱子的个数,根据数据的取值范围和柱子的个数计算每个柱子的取值范围,柱子宽=$\frac{x.max( )-x.min( )}{bins}$ range 为元组,如果bins 取值为序列,那么range无效 density 如果是True ,那么为标准化,否则不是标准化 weigh 为权重 align 为靠哪边,left为靠左边,right为靠右边,mid为中间 等等 当然也可是二维的直方图
官网链接:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hist.html
#单变量直方图
import numpy as np
import matplotlib.pyplot as plt
mu = 100
#截距
sigma = 20
#斜率
x = mu+sigma*np.random.randn(2000)
plt.hist(x,bins=1000,color='r',density=True)
#normed 有BUG,换density,用于标准化
#bins 分多少个直方图
plt.show()
########################################
#双变量直方图
import numpy as np
import matplotlib.pyplot as plt
x = np.random.randn(1000)+2
y = np.random.randn(1000)+3
plt.hist2d(x,y,bins=40)
plt.show()
饼状图
matplotlib.pyplot.pie(x,lables,explode,startangle,shadow,radius,labeldistance,countterclock,...) x 为变量的列表数组 labels 为对应的标签 explode 为一块离中心的距离 startangle 为起始角度 shadow 为阴影[突出某块饼] radius 为半径,一般默认是1 等等
官网链接:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.pie.html
import matplotlib.pyplot as plt
labels = 'A','B','C','D'
fracs = [15,30,45,10]
explode=[0,0.1,0,0]#控制突出距离
plt.axes(aspect=1)#显示为正圆
plt.pie(x = fracs,labels = labels,autopct="%.0f%%",explode=explode,shadow=True)#显示比例
plt.show()
箱型图
matplotlib.pyplot.boxplot(x, notch=None, sym=None, vert=None, whis=None, positions=None, widths=None, patch_artist=None, bootstrap=None, usermedians=None, conf_intervals=None, meanline=None, showmeans=None, showcaps=None, showbox=None, showfliers=None, boxprops=None, labels=None, flierprops=None, medianprops=None, meanprops=None, capprops=None, whiskerprops=None, manage_xticks=True, autorange=False, zorder=None, hold=None, data=None)
x 为指定要绘制的箱型图数据 notch 为是否凹凸的形式展示箱型图 sym 指定异常点的形状 vert 为知否需要将箱型图垂直摆放 whis 为指定上下四分位的距离 position 为指定箱型图的位置 widths 为指定箱型图的宽度 patch_artist 为是否填充箱体的颜色 meanline 为是否用线性的形式表示均值 showmeans 是否显示均值 等等
官网链接:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.boxplot.html
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(10)
data = np.random.normal(size = 1000,loc = 0, scale = 1)
#loc 概率分布均值 -》期望
#scale 概率分布的标准差-》方差
plt.boxplot(data,sym='4',whis=1.5)#修改虚线的长度,默认是1,5
#sym->形状 (* / o / < / > / v / - / x ...)
plt.show()