Pandas.plot 做图 demo(scatter,bar,pie)

时间:2021-07-16 23:47:01
#coding:utf-8
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
import pandas as pd
from pandas import Series,DataFrame

'''
plt.scatter(x,y)
plt.show()

plt.bar(x,y,width=0.3,color='y')
plt.bar(x+0.3,y2,width=0.3,color='y')
plt.bar(x,y2,width=0.3,color='y',bottom=y)

plt.pie(x=x,labels=y,...)
plt.show()

plt.boxplot(data,sym='o',whis=0.05)
plt.boxplot(data,labels=labe)
plt.show()
'''
data_train=pd.read_csv(r'C:\python\demo\ML\data\TravelRecords0708.csv',encoding='gbk')
title ='(2017.07-2017.08)'
#print(data_train.loc[:,['Name','Team','TravelDays','RequesterAddress']])
def tongjiByName():

fig = plt.figure()
fig.set(alpha=0.2) # 设定图表颜色alpha参数

#plt.subplot2grid((2, 3), (0, 0)) # 在一张大图里分列几个小图
data_train.Name.value_counts().plot(kind='bar') # plots a bar graph of those who surived vs those who did not.
plt.title(u"DB出差同事出差次数情况统计"+title) # puts a title on our graph
plt.ylabel(u"出差次数")
plt.text(20,4.5,u'出差总次数(次):')
plt.text(25, 4.5, data_train.Name.value_counts().values.sum())
plt.text(20,4.0,u'出差总人数(个):')
plt.text(25, 4.0, len(data_train.Name.value_counts()))
plt.text(20,3.5,u'出差总天数(天):')
plt.text(25, 3.5, data_train.TravelDays.values.sum())
print(data_train.TravelDays.values.sum())
plt.show()

def tongjiByteam():
fig = plt.figure()
fig.set(alpha=0.2) # 设定图表颜色alpha参数

#plt.subplot2grid((2, 3), (0, 0)) # 在一张大图里分列几个小图
data_train.Team.value_counts().plot(kind='pie') # plots a bar graph of those who surived vs those who did not.
#data_train.Team.value_counts().plot(kind='bar') # plots a bar graph of those who surived vs those who did not.
print(data_train.Team.value_counts())

plt.title(u"深圳马会各team出差人数统计情况(2017.07-08)") # puts a title on our graph
plt.text(5.5,10.5,u'出差总人数(次):')
plt.text(6.8, 10.5, data_train.Team.value_counts().values.sum()) #统计series的value 总和
#plt.text(5.5, 9.5, u'出差team数(个):')
#plt.text(6.8, 9.5, len(data_train.Team.value_counts().index)) #统计series的index 总和
#plt.ylabel(u"出差人数")
plt.show()

def tongjiByteamPIE():
fig = plt.figure()
fig.set(alpha=0.2) # 设定图表颜色alpha参数

labels=data_train.Team.value_counts().index
explode = [0, 0.5,0,0,0,0,0,0,0,0,0,0] # 0.1 凸出这部分,
plt.pie(x=data_train.Team.value_counts(),labels=labels,autopct='%3.1f %%',shadow=True,labeldistance=1.1,startangle=90,pctdistance=0.6)

plt.title(u"DB各team出差人数统计情况"+title) # puts a title on our graph
plt.text(5.5,10.5,u'出差总人数(次):')
plt.text(6.8, 10.5, data_train.Team.value_counts().values.sum()) #统计series的value 总和
plt.show()

def tongjiByTravelDays():
fig = plt.figure()
fig.set(alpha=0.2) # 设定图表颜色alpha参数

#plt.subplot2grid((2, 3), (0, 0)) # 在一张大图里分列几个小图
data_train.TravelDays.value_counts().plot(kind='bar') # plots a bar graph of those who surived vs those who did not.
#data_train.Team.value_counts().plot(kind='bar') # plots a bar graph of those who surived vs those who did not.
print(data_train.Team.value_counts())

plt.title(u"DB各team出差时长统计情况"+title) # puts a title on our graph
plt.text(2.5,30.5,u'出差人次(人):')
plt.text(3.8, 30.5, data_train.TravelDays.value_counts().values.sum()) #统计series的value 总和
plt.text(2.5, 25.5, u'出差时长种类(类):')
plt.text(3.8, 25.5, len(data_train.TravelDays.value_counts().index)) #统计series的index 总和

plt.text(2.5, 20.5, u'出差时长最长天数(天):')
plt.text(3.8, 20.5, data_train.TravelDays.values.max()) #统计series的index 总和
print(data_train.TravelDays.values.max())
plt.ylabel(u"出差人数")
plt.xlabel(u"出差天数")
plt.show()

def tongjiByRequestAddress():
fig = plt.figure()
fig.set(alpha=0.2) # 设定图表颜色alpha参数

#plt.subplot2grid((2, 3), (0, 0)) # 在一张大图里分列几个小图
#data_train.RequesterAddress.value_counts().plot(kind='pie') # plots a bar graph of those who surived vs those who did not.
#data_train.Team.value_counts().plot(kind='bar') # plots a bar graph of those who surived vs those who did not.
labels='HK','SZ'
explode = [0, 0.2] # 0.1 凸出这部分,
plt.pie(x=data_train.RequesterAddress.value_counts(),labels=labels,autopct='%1.1f %%',shadow=True,labeldistance=1.1,startangle=90,pctdistance=0.6)
print(data_train.RequesterAddress.value_counts())

plt.title(u"DB同事出差邀请(香港/深圳)统计情况"+title) # puts a title on our graph
plt.show()


#tongjiByName()
#tongjiByteam()
tongjiByteamPIE()
#tongjiByTravelDays()
#tongjiByRequestAddress()