前言matplotlib画的直方图是用hist函数画的,但有时也可能想画直方图和累计的概率曲线。 既然没有现成的东西,就只好自己做
累计曲线ax1=fig.add_subplot(111 ) a1,a2,a3=ax1.hist(data,bins=10,alpha=0.65,normed=1,edge color ' )
[ 0.000428820.000393080.00046455.002191730.004562130.001060130.00042882.0002025.00014294.00002222632 ] a2是狒狒
[顺利的乐曲a3会返回patch对象,对我们的需求的实现并不特别重要
因为是a list of 10 Patch objects,所以接下来我们要做的是获取累计概率曲线的横轴
累计概率曲线的横坐标indexs=[]A2=A2.tolist(forI,valueinenumerate ) A2 ) :ifI=len(A2 )-2:index=) A2 ) I ) A2 )
累计概率曲线的纵轴dis=a2 [1]-a2 [0] freq=[ f * disforfina1] ACC _ freq=[ ] forinrange (0,len(freq ) ) : ifi==033330
生成双轴,描绘累积概率曲线#这是双轴坐标的重要的一步ax2=ax1.twinx(#描绘累积概率曲线ax2.plot(indexs,acc_freq ) )将累积概率曲线的纵轴以百分比形式ax2.yaxis .
完整代码defcum_prob_curve(data,bins,title,xlabel, PIC_path(: ) "概率分布直方图和累积概率分布曲线(" ' importmatplotlib.pyplotaspltimportmatplotlibasmplfrommatplotlib.tickerimporimpotib 该类用于设置刻度间隔frommatplotlib.pyplotimportmultiplelocatorfig=PLT.figure (fig size=) 8,4。 dpi=100 ) #图形视觉样式PLT.style.use(ggplot ) (中文和减号的常规显示mpl.rcparams ) ) font.sans-serif )=(timesnewRoman ) mpl.rcparams [ ' axes.unicode _ MINUS ' ]=false ax1=fig.add _ subplot (111 ) ##概率分布直方图a1、a2、aa2 normed=1 edgecolor='k ' ) ##累计概率曲线#生成累计概率曲线的横坐标indexs=[] a2=a2.tolist () for i, valueinenumerate(A2 ) :ifI=Len(A2 )-2:index=) A2[I]A2[I1] )/2indexs.append ) index累积概率曲线的纵轴deff temp ) ) dis=A2(1)-A2 )0) freq=[ f * disforfina1] ACC _ freq=[ len (freq ) ) : ifi==03360 temp=freq ) ACC_freq.append(temp ) )此累计概率曲线ax2.plot(indexs, acc_freq ) )将累计概率曲线的垂直轴设定为百分比格式ax2.yaxis.set _ major _ formatter (func formatter ) to_percent ) ) fontsize=8)将#x轴的刻度间隔设定为1,然后变量设定为# x _ major _ locator=multiple locator (xlocator ) ax1.xaxis.set _ major _ locator=locator