第三章?
代碼3-1
p=data.boxplot(reutrn_type='dict')#更正~?
The parameter return_type can be used to select the type of element returned by boxplot.? When return_type='axes' is selected, the matplotlib axes on which the boxplot is drawn are returned:
正確代碼如下:
import pandas as pd
catering_sale='E:\python程序編輯\Python數(shù)據(jù)分析與挖掘?qū)崙?zhàn)\chapter3\demo\data\catering_sale.xls'
data=pd.read_excel(catering_sale,index_col=u'日期')#讀取數(shù)據(jù),以日期列為索引列
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']#顯示中文正常標(biāo)簽
plt.rcParams['axes.unicode_minus']=False#用來顯示正常負號
plt.figure()#建立圖像
p = data.boxplot(return_type='dict') #畫箱線圖抒寂,直接使用DataFrame的方法
x=p['fliers'][0].get_xdata()#'flies'即為異常值的標(biāo)簽
y=p['fliers'][0].get_ydata()
y.sort()
for i in range(len(x)):
? ? if i>0:
? ? ? ? plt.annotate(y[i],xy=(x[i],y[i]),xytext=(x[i]+0.05-0.8/(y[i]-y[i-1]),y[i]))
? ? else:
? ? ? ? plt.annotate(y[i],xy=(x[i],y[i]),xytext=(x[i]+0.08,y[i]))
plt.show()
fliers層表示離散點寸齐,使用 P['fliers'][0]獲取點坐標(biāo)斋竞,然后get_ydata()獲取點縱坐標(biāo)焚辅,get_xdata()是橫坐標(biāo)隧熙。
plt.annotate()的函數(shù)解析:
更詳細的解釋:plt.annotate()函數(shù)解析 - TeFuirnever的博客 - CSDN博客
plt.annotate(string, xy=(np.pi/2, 1.0), xytext=((np.pi/2)+0.15, 1,5), weight="bold", color="b", arrowprops=dict(arrowstyle="->", connectionstyle="arc3", color="b"))
string:圖形內(nèi)容的注釋文本杖小,xy:被注釋圖形內(nèi)容的位置坐標(biāo)奶赔,xytext:注釋文本的位置坐標(biāo)豪筝,weight:注釋文本的字體粗細風(fēng)格痰滋,color:注釋文本的字體顏色,arrowprops:指示被注釋內(nèi)容的箭頭的屬性字典续崖。
代碼3-2
from __future__ imports must occur at the beginning of the file問題解決敲街。解決方法(將 from_future_impotrt----前面所有的注釋和文字全部刪除即可)
pandas中l(wèi)oc和iloc的用法的區(qū)別:
詳細說明見Pandas中l(wèi)oc和iloc函數(shù)用法詳解 - 破曉時刻的博客 - CSDN博客
在python中出現(xiàn)了:
AttributeError: 'Series' object has no attribute 'sort'
將data.sort(ascending=False),改為data.sort_values(ascending=False)
2019.4.19
python中D.T表示矩陣D的轉(zhuǎn)置
pandas中提供了一些非常方便使用的計算統(tǒng)計特征的函數(shù)严望,主要有累積計算cum()多艇,和滾動計算pd.rolling()
cum系列函數(shù)是作為DataFrame和Series對象的方法而出現(xiàn)的,命令為D.cumsum()著蟹,而rolling系列是pandas的是函數(shù)墩蔓,并不是DataFrame或Series的函數(shù)梢莽,它們的使用格式為pd.rolling_mean(D,k),意思是每k列計算一次均值,滾動計算奸披。
3.3.2中代碼復(fù)現(xiàn)出現(xiàn)的錯誤
運行pd.rolling_mean(D,1)會出現(xiàn)
AttributeError: module 'pandas' has no attribute 'rolling_mean'
將上述代碼改為D.rolling(2).sum()
x=np.linspace(0,2*np.pi,50) #其中2*np.pi就是2pi的意思昏名,只是在利用pi是調(diào)用了Python中的numpy庫
plt.pie(sizes,explode=explode,labels=labels,colors=colors,autopct='%1.2f%%',startangle=90,shadow=True)
autopct='%1.2f%%' 中1.2小數(shù)點前面的數(shù)對結(jié)果沒有影響,后面的數(shù)表示保留幾位小數(shù)阵面。這個輸出的是百分比形式的數(shù)字轻局。
x.plot(label=u'原始數(shù)據(jù)',legend=True) 中的legend是是否顯示標(biāo)簽的意思