東莞網(wǎng)頁制作免費(fèi)網(wǎng)站制作如何在百度上發(fā)布廣告
1.柱狀圖
#柱狀圖
#bar(x,height,width,*,align='center',**kwargs)
#height柱子的高度,即y軸上的數(shù)據(jù)
#width數(shù)組的寬度,默認(rèn)值0.8
#*表示后面的參數(shù)為匿名關(guān)鍵字,必須傳入?yún)?shù)
#kwargs關(guān)鍵字參數(shù)x=[1,2,3,4,5]
height=[random.randint(10,100)for i in range(5)]
plt.bar(x,height)
plt.show()df=pd.read_excel("產(chǎn)品銷售.xlsx")
x=df['產(chǎn)品名稱']
height=df['總量']
plt.figure(10,6)
plt.bar(x,height,width=0.5,alpha=0.5)
plt.grid(axis='y',linestyle='--')
plt.xlabel("產(chǎn)品名稱")
plt.yticks("銷量")
plt.title('產(chǎn)品銷售量',fontsize=18)
#設(shè)置圖例
plt.legend(('銷售額',))
#設(shè)置文本標(biāo)簽
#alpha=0.9設(shè)置透明度
for a,b in zip(x,height):plt.text(a,b,format(b,','),ha='center',va='center',fontsize=12,color='b',alpha=0.9)
plt.show()
2.多柱狀圖
df=pd.read_excel("產(chǎn)品銷售.xlsx")
plt.figure(10,6)
#x=df['產(chǎn)品名稱']
x=np.array([0,1,2,3,4,5,6,7])
y1=df['1月']+df['2月']+df['3月']
y2=df['4月']+df['5月']+df['6月']
y3=df['7月']+df['8月']+df['9月']
y4=df['10月']+df['11月']+df['12月']
bar_width=0.2#設(shè)置柱子的寬度
plt.ylabel("季度銷售")
plt.xlabel("產(chǎn)品名稱")
plt.title("季度銷售量")
plt.bar(x,y1,bar_width,color='c',alpha=0.5)
plt.bar(x+bar_width,y2,bar_width,color='b',alpha=0.5)
plt.bar(x+2*bar_width,y3,bar_width,color='y',alpha=0.5)
plt.bar(x+3*bar_width,y4,bar_width,color='r',alpha=0.5)
#設(shè)置坐標(biāo)軸刻度
data=df['產(chǎn)品名稱']
plt.xticks(x,data)
#添加文本標(biāo)簽
for a,b in zip(x,y1):plt.text(a,b,format(b,','),ha='center',va='bottom',fontsize=8)
for a,b in zip(x,y2):plt.text(a+bar_width,b,format(b,','),ha='center',va='bottom',fontsize=8)
for a,b in zip(x,y3):plt.text(a+2*bar_width,b,format(b,','),ha='center',va='bottom',fontsize=8)
for a,b in zip(x,y4):plt.text(a+3*bar_width,b,format(b,','),ha='center',va='bottom',fontsize=8)
#設(shè)置圖例
plt.legend(['第一季度','第二季度','第三季度','第四季度'])
plt.show()
3.基本直方圖
#直方圖
#plt.hist(x,bins)
#bins:統(tǒng)計(jì)數(shù)據(jù)的區(qū)間分布x=[2,34,52,62,12,35,45,88,26,13,16]
bins=[0,25,50,75,100]
plt.hist(x,bins)
plt.show()
#使用直方圖分析成績(jī)分布情況
df=pd.read_excel('成績(jī)表.xlsx')
#解決中文亂碼
plt.rcParams['font.sans-serif']=['SimHei']x=df['總成績(jī)']
#設(shè)置坐標(biāo)軸標(biāo)題
plt.xlabel('分?jǐn)?shù)')
plt.ylabel('學(xué)生姓名')
#設(shè)置圖表的標(biāo)題
plt.title('成績(jī)分布直方圖',fontsize=18)
#設(shè)置數(shù)據(jù)的區(qū)間
bins=[40,50,60,70,80,90,100]
plt.hist(x,bins,facecolor='b',edgecolor='k')
plt.show()
?