人们通常使用Excel软件或者程序进行数据分析,python语言成为进行人们进行数据分析的常用语言。下面是我使用python语言进行数据分析所绘画的各种图:

1.引用所需要的库及设置正常显示中文标签和负号

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif']=['SimHei']

plt.rcParams['axes.unicode_minus']=False

2.箱型图

#箱型图

df='d:/numberfx/catering_fish_congee(1).xls'

data=pd.read_excel(df,index_col='date')

print(data.describe())

plt.figure()

p=data.boxplot(return_type='dict')

x=p['fliers'][0].get_xdata()

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.title('3008箱型图')

plt.show()

效果图:

3.直方分布图

#直方图

data2=pd.read_excel(df,names=['date','sale'])

print(data2.describe())

bins=[0,500,1000,1500,2000,2500,3000,3500,4000]

labels=['[0,500)','[500,1000)','[1000,1500)','[1500,2000)','[2000,2500)','[2500,3000)','[3000,3500)','[3500,4000)']

data2['sale分层']=pd.cut(data2.sale,bins,labels=labels)

aggResult=data2.groupby(by=['sale分层'])['sale'].agg([("sale", np.size)])

pAggResult=round(aggResult/aggResult.sum(),2,)*100

plt.figure(figsize=(10,6))

pAggResult['sale'].plot(kind='bar',width=0.8,fontsize=10)

plt.title('3008分布直方图',fontsize=20)

plt.show()

效果图:

 

4.饼状图:

#饼状图

df2='d:/numberfx/catering_dish_profit(1).xls'

data3=pd.read_excel(df2)

x=data3['盈利']

labels=data3['菜品名']

plt.figure(figsize=(8,6))

plt.pie(x,labels=labels)

plt.title('3008饼图')

plt.axis('equal')

plt.show()

效果图:

 

5.条形图

#条形图

x=data3['菜品名']

y=data3['盈利']

plt.figure(figsize=(8,4))

plt.bar(x,y)

plt.xlabel('菜名')

plt.ylabel('销量')

plt.title('3008条形图')

plt.show()

 效果图:

6.折线图比较数据 

#折线图比较数据

df3='d:/numberfx/dish_sale(1).xls'

data4=pd.read_excel(df3)

plt.figure(figsize=(8,4))

plt.plot(data4['月份'],data4['A部门'],color='green',label='A部门',marker='o')

plt.plot(data4['月份'],data4['B部门'],color='red',label='B部门',marker='s')

plt.plot(data4['月份'],data4['C部门'],color='skyblue',label='C部门',marker='x')

plt.legend()

plt.ylabel('销售额(万元)')

plt.show()

df4='d:/numberfx/dish_sale_b(1).xls'

data5=pd.read_excel(df4)

plt.figure(figsize=(8,4))

plt.plot(data5['月份'],data5['2012年'],color='green',label='2012年',marker='o')

plt.plot(data5['月份'],data5['2013年'],color='red',label='2013年',marker='s')

plt.plot(data5['月份'],data5['2014年'],color='skyblue',label='2014年',marker='x')

plt.legend()

plt.ylabel('销售额(万元)')

plt.show()

效果图:

 

7.帕累托图

#帕累托图

data6=pd.read_excel(df2,index_col='菜品名')

data6=data6['盈利'].copy()

data6.sort_values(ascending=False)

plt.figure()

data6.plot(kind='bar')

plt.ylabel('盈利(元)')

p=1.0*data6.cumsum()/data6.sum()

p.plot(color='r',secondary_y=True,style='-o',linewidth=2)

plt.annotate(format(p[6],'.4%'), xy=(6,p[6]), xytext=(6*0.9,p[6]*0.9), arrowprops=dict(arrowstyle="->",connectionstyle="arc3,rad=.2"))

plt.ylabel('盈利(比例)')

plt.title('3008帕累托图')

plt.show()

效果图:

 

8.散点图

#散点图

years=[2001,2002,2003,2004,2005,2006,2007,2008,2009,2010]

turnovers=[1,1.5,2,3,4,5.5,6,7.6,8,20]

plt.figure()

plt.scatter(years,turnovers,c='green',s=100,label='legend')

plt.xticks(range(2000,2011,1))

plt.yticks(range(0,21,1))

plt.xlabel("Year",fontdict={'size':16})

plt.ylabel("number",fontdict={'size':16})

plt.title("3008散点图",fontdict={'size':20})

plt.legend(loc='best')

plt.show()

 效果图:

结论:使用python进行数据分析容易上手,并能准确看出数据的分布情况和分布特点。 

 

精彩文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: