概述
堆积图柱形图
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
x = range(1,6)
y = np.random.randint(1,14,5)
y1 = np.random.randint(1,10,5)
plt.bar(x,y,align='center',color='gray',tick_label=[chr(i) for i in range(65,70)],label='样本A')
plt.bar(x,y1,align='center',bottom=y,color='b',label='样本B')
plt.xlabel('value')
plt.ylabel('sample No.')
plt.show()
python高级统计图
堆积条形图
plt.barh(x,y,align='center',color='c',tick_label=[chr(i) for i in range(65,70)],label='样本')
plt.barh(x,y1,align='center',left=y,color='b',label='样本B')
plt.show()
python高级统计图
分组条形图
bar_width = 0.35
tick_label = [chr(i) for i in range(65,70)]
x = np.arange(5)
y = np.random.randint(0,10,5)
y1 = np.random.randint(0,8,5)
plt.bar(x,y,bar_width,color='c',align='center',label='样本A',alpha=0.5)
plt.bar(x+bar_width,y1,bar_width,color='r',align='center',label='样本B',alpha=0.5)
plt.xticks(x+bar_width/2,tick_label)
plt.show()
python高级统计图
分组水平条形图
plt.barh(x,y,bar_width,color='c',align='center',label='A',alpha=.5)
plt.barh(x+bar_width,y1,bar_width,color='b',align='center',label='B',alpha=.5)
plt.yticks(x+bar_width/2,tick_label)
plt.legend()
plt.show()vv
python高级统计图
hatck参数设置填充样式
plt.bar(x,y,align='center',color='c',tick_label=tick_label,hatch='///')
plt.show()
python高级统计图
plt.bar(x,y,align='center',color='c',tick_label=tick_label,hatch='***')
plt.show()
python高级统计图
plt.bar(x,y,align='center',color='c',tick_label=tick_label,hatch='---')
plt.show()
python高级统计图
stackplot()堆积折线图
x = np.arange(1,6,1)
y = np.random.randint(0,10,5)
y1 = np.random.randint(1,8,5)
y2 = np.random.randint(0,9,5)
labels = [chr(i) for i in range(65,70)]
plt.stackplot(x,y,y1,y2,labels=labels)
plt.legend()
plt.show()
python高级统计图
broken_barh()间断条形图
plt.broken_barh(np.random.rand(3,2),(10,8))
plt.broken_barh(np.random.rand(4,2),(20,8),facecolors=('r','b','y','c'))
plt.xlim(-1,2)
plt.xlabel('action time')
plt.xticks(np.arange(0,1,10))
plt.yticks([15,25],['Opera House A','Opera House B'])
plt.grid(ls='-',lw=1,color='gray')
plt.title('Action time in diffrent Opera House')
plt.show()
python高级统计图
step()阶梯图
x = np.linspace(1,10,10)
y = np.sin(x)
plt.step(x,y,color='c',where='pre',lw=2) # where in 'pre' or 'post'
plt.xlim(0,11)
plt.xticks(np.arange(1,11,1))
plt.ylim(-1.2,1.2)
plt.show()
python高级统计图
堆积直方图
# stacked = True
score1 = np.random.randint(0,100,100)
score2 = np.random.randint(0,100,100)
x = [score1,score2]
labels = ['sampleA','sampleB']
bins = range(0,101,10)
plt.hist(x,bins=bins,histtype='bar',rwidth=1,stacked=True,label=labels)
plt.xlabel('Test Score')
plt.ylabel('Students Num')
plt.title('Test Score Histgram in different classies')
plt.legend(loc='upper left')
plt.show()
python高级统计图
# stacked = False
score1 = np.random.randint(0,100,100)
score2 = np.random.randint(0,100,100)
x = [score1,score2]
labels = ['sampleA','sampleB']
bins = range(0,101,10)
plt.hist(x,bins=bins,histtype='bar',rwidth=1,stacked=False,label=labels)
plt.xlabel('Test Score')
plt.ylabel('Students Num')
plt.title('Test Score Histgram in different classies')
plt.legend(loc='upper left')
plt.show()
python高级统计图
# histtype = 'stepfilled'
score1 = np.random.randint(0,100,100)
score2 = np.random.randint(0,100,100)
x = [score1,score2]
labels = ['sampleA','sampleB']
bins = range(0,101,10)
plt.hist(x,bins=bins,histtype='stepfilled',rwidth=1,label=labels,alpha=0.5)
plt.xlabel('Test Score')
plt.ylabel('Students Num')
plt.title('Test Score Histgram in different Samples')
plt.legend(loc='upper left')
plt.show()
python高级统计图
分列式饼图
labels = [chr(i) for i in range(65,70)]
values = list(np.random.randint(0,25,4))
values.append(100 - sum(values))
values.sort(reverse=False)
explode = list(np.repeat(0.1,5))
plt.pie(values,explode=explode,labels=labels,autopct='%3.2f%%',
startangle=45,shadow=True)
#values 饼片的百分比
#explode 饼片边缘偏离半径的百分比
#labels 饼片标签文本
#autopct 饼片文本标签内容对应的数值百分比样式
#startangle 第一个饼片逆时针旋转的角度
#shadow 饼片阴影
#colors 饼片颜色
plt.title('Different samples select percent rate')
plt.show()
python高级统计图
非分列式饼图
plt.pie(values,labels=labels,autopct='%3.2f%%',
startangle=45,pctdistance=.7,labeldistance=1.2,
shadow=True)
plt.show()
python高级统计图
绘制内嵌环形饼图
elements = [chr(i) for i in range(65,70)]
weight1 = list(np.random.randint(0,50,5))
weight2 = list(np.random.randint(10,60,5))
colormapList = ['r','b','c','y']
wedges1,texts1,autotexts1 = plt.pie(weight1,autopct='%3.1f%%',radius=1,pctdistance=.85,
colors=colormapList,textprops=dict(color='w'),
wedgeprops=dict(width=0.3,edgecolor='w'))
wedges2,text2,autotexts2 = plt.pie(weight2,autopct='%3.1f%%',radius=.7,pctdistance=.75,
colors=colormapList,textprops=dict(color='w'),
wedgeprops=dict(width=.3,edgecolor='w'))
plt.legend(wedges1,
elements,
fontsize=12,title='Content table',
loc='center left',
bbox_to_anchor=(.91,0,.3,1))
plt.setp(autotexts1,size=15,weight='bold')
plt.setp(autotexts2,size=15,weight='bold')
plt.setp(texts1,size=12)
plt.title('Content list in different product')
plt.show()
python高级统计图
多分组箱线图
sampleA = np.random.randn(5000)
sampleB = np.random.randn(5000)
samples = [sampleA,sampleB]
labels = ['SampleA','SampleB']
colors = ['#1b9e77','#d95f02']
bplot = plt.boxplot(samples,
whis=1.6,
widths=.35,
sym='o',
labels=labels,
patch_artist=True)
#sampls 输入数据
#whis 四分位间距的倍数,用来确定箱须范围值
#widths 箱的宽度
#sym 离群值的标记样式
#labels 赝本标签
#patch_artist 是否给箱子提爱妮佳颜色
for patch,color in zip(bplot['boxes'],colors):
patch.set_facecolor(color)
plt.ylabel('random number')
plt.title('Samples capacity of resisting disturbarice')
plt.grid(axis='y',ls=':',lw=1,color='gray',alpha=.4)
plt.show()
python高级统计图
水平箱线图
x = np.random.randn(1000)
plt.boxplot(x,vert=False)
plt.xlabel('random values')
plt.yticks([1],['SampleA'],rotation=90)
plt.title("SampleA's boxplot")
plt.grid(axis='x',ls=':',lw=1,color='gray',alpha=.4)
plt.show()
python高级统计图
不绘制离群值箱线图
plt.boxplot(x,vert=False,showfliers=False)
plt.xlabel('values')
plt.yticks([1],['SampleA boxplot'],rotation=90)
plt.title('Boxplto')
plt.grid(axis='x',ls=':',lw=1,color='gray',alpha=.4)
plt.show()
python高级统计图
误差棒图
x = np.linspace(.1,.6,10)
y = np.exp(x)
error = .05+.15*x
lower_error = error
upper_error = .3*error
error_limit = [lower_error,upper_error]
plt.errorbar(x,y,yerr=error_limit,fmt=':o',
ecolor='y',elinewidth=4,ms=5,mfc='c',mec='r',
capthick=1,capsize=2)
#x,y 数据点位置
#yerr 但一直的非对称形式的误差范围
#fmt 数据点的标记样式和数据点演示的连接线样式
#ecolor 乌茶邦的线条颜色
#elinewidth 乌茶邦的线条粗细
#ms 数据点的大小
#mfc 数据点的标记颜色
#mec 数据点的标记边缘颜色
#capthick 误差棒的边界横杠宽度
#capsize 误差棒边界横杠的大小
plt.xlim(0,.7)
plt.show()
python高级统计图
误差棒柱形图
x = list(range(1,6))
y = list(np.random.randint(10,100,5))
std_err = list(np.random.randint(0,10,5))
error_attri = dict(elinewidth=2,ecolor='black',capsize=3)
plt.bar(x,y,color='c',
width=.6,
align='center',
yerr=std_err,
error_kw=error_attri,
tick_label=[chr(i) for i in range(65,70)])
plt.xlabel('Samples')
plt.ylabel('values')
plt.title('Error Bar')
plt.grid(True, axis='y',ls=':',color='gray',alpha=.2)
plt.show()
python高级统计图
带误差棒的水平条形图
plt.barh(x,y,
bar_width,
color=['r','b','c','y'],
align='center',
xerr=std_err,
tick_label=[chr(i) for i in range(65,70)])
plt.xlabel('values')
plt.ylabel('SampleA')
plt.title('Error Barh')
plt.show()
python高级统计图
带误差的分组柱形图
x = np.arange(5)
y1 = np.random.randint(0,100,5)
y2 = np.random.randint(10,50,5)
std_err1 = np.random.randint(0,10,5)
std_err2 = np.random.randint(0,10,5)
error_attri = dict(elinewidth=2, ecolor='black', capsize=3)
bar_width=.4
tick_label=[chr(i) for i in range(65,70)]
plt.bar(x,y1,bar_width,color='c',
align='center',yerr=std_err1,
error_kw=error_attri,label='A')
plt.bar(x+bar_width,y2,bar_width,
color='y',align='center',
yerr=std_err2,error_kw=error_attri,label='B')
plt.xlabel('Samples')
plt.ylabel('values')
plt.title('Error Bar by group')
plt.grid(True,axis='y',ls=':',color='gray',alpha=.2)
plt.legend()
plt.show()
python高级统计图
带误差棒的堆积柱形图
plt.bar(x,y1,bar_width,color='c',
align='center',yerr=std_err1,
label='SampleA',error_kw=error_attri)
plt.bar(x,y2,bar_width,bottom=y1,color='y',
align='center',yerr=std_err2,
label='SampleB',error_kw=error_attri)
plt.xlabel('Sampls')
plt.ylabel('values')
plt.title('Error Bar by group')
plt.grid(True,axis='y',ls=':',color='gray',alpha=.2)
plt.xticks(x,tick_label)
plt.legend()
plt.show()
python高级统计图
最后
以上就是幽默唇彩为你收集整理的python stackplot_python高级统计图的全部内容,希望文章能够帮你解决python stackplot_python高级统计图所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复