pandas之数据可视化
- 一、散点图
- 二、矩阵图
- 三、折线图
- 四、饼图
- 五、柱形图与条形图
data都为数据框
一、散点图
import matplotlib
import matplotlib.pyplot as plt
mainColor = (91/255, 155/255, 213/255, 1)
plt.scatter(
data['广告费'],
data['购买用户数'],
c = mainColor
)
font = matplotlib.font_manager.FontProperties(
fname='D:/1.otf',size=30
)
plt.figure()
pinlColor=(255/255,0/255,102/255,1)
blueColor= (91/255,155/255,213/255,1)
plt.scatter(
data[data['促销']=='是']['广告费用'],
data[data['促销']=='是']['购买用户数'],
c=pinkColor,
marker='o',
s=data[data['促销']=='是']['渠道数']*80
)
plt.scatter(
data[data['促销']=='否']['广告费用'],
data[data['促销']=='否']['购买用户数'],
c=pinkColor,
marker='x',
s=data[data['促销']=='否']['渠道数']*80
)
plt.xlabel(
'广告费用',
color=mainColor,
fontproperties=font
)
plt.ylabel(
'购买用户数',
color=mainColor,
fontproperties=font
)
plt.xticks(
color=mainColor,
fontproperties=font
)
plt.yticks(
color=mainColor,
fontproperties=font
)
legend = plt.legend(labels=['促销','不促销'],prop=font)
二、矩阵图
costAgg = data.groupby(
by = '省份',
as_index=False,
)['月消费'].agg('mean')
dataAgg = data.groupby(
by = '省份',
as_index=False,
)['月流量'].agg('mean')
addData = costAgg.merge(dataAgg)
import matplotlib
import matplotlib.pyplot as plt
font = matplotlib.font_manager.FontProperties(
fname='D:/1.otf',size=20
)
labelFont=matplotlib.font_manager.FontProperties(
fname='D:/1.otf',size=35
)
mainColor = (91/255,155/255,213/255,1)
fontColor = (110/255,110/255,110/255,1)
fig=plt.figure()
gap = 0.01
xMin = aggData['月消费'].min()*(1-gap)
xMax = aggData['月消费'].max()*(1+gap)
yMin = aggData['月流量'].min()*(1-gap)
yMax = aggData['月流量'].max()*(1+gap)
plt.xlim(xMin,xMax)
plt.ylim(yMin,yMax)
plt.xticks([])
plt.xticks([])
plt.scatter(
aggData['月消费'],
aggData['月流量'],
s=90,marker = 'o',color=mainColor
)
plt.xlabel(
'人均月消费(元)',
color=fontColor,
fontproperties=labelFont
)
plt.ylabel(
'人均月流量(MB)',
color=fontColor,
fontproperties=labelFont
)
plt.vlines(
x=data['月消费'].mean(),
ymin=yMin,ymax=yMax,
linewidth=1,color=mainColor
)
plt.vlines(
y=data['月流量'].mean(),
xmin=xMin,xmax=xMax,
linewidth=1,color=mainColor
)
plt.text(
xMax - 0.5,yMax-5,
'一',color=fontColor,fontsize=50
)
plt.text(
xMin ,yMax-5,
'二',color=fontColor,fontsize=50
)
plt.text(
xMin,yMin,
'三',color=fontColor,fontsize=50
)
plt.text(
xMax - 0.5,yMin,
'四',color=fontColor,fontsize=50
)
for i,r in aggData.iterrows():
plt.text(
r['月消费']+0.25,
r['月流量']-1,
r['省份'],
color=fontColor,
fontsize=30
)
三、折线图
data['reg_date'] = pandas.to_datetime(
data['reg_date']
)
ga = data.groupby(
by=['reg_date'],as_index=False
)['id'].agg['count']
ga.columns = ['注册日期','注册用户数']
import matplotlib
import matplotlib.pyplot as plt
mainColor=(91/255,155/255,213/255,1)
font = matplotlib.font_manager.FontProperties(
fname='D:/1.otf',size=25
)
labelFont = matplotlib.font_manager.FontProperties(
fname='D:/1.otf',size=35
)
plt.ylim(0,500)
plt.title('注册用户数', color=mainColor,fontproperties=labelFont)
plt.xlabel('注册日期', color=mainColor,fontproperties=labelFont)
plt.ylabel('注册用户数', color=mainColor,fontproperties=labelFont)
plt.xticks(color=mainColor,fontproperties=font)
plt.yticks(color=mainColor,fontproperties=font)
plt.plot(ga['注册日期'],ga['注册用户数'],'-',lw=8,color=mainColor)
四、饼图
ga = data.groupby(
by=['gender']
)['id'].agg('count')
import matplotlib
import matplotlib.pyplot as plt
maleColor=(91/255,155/255,213/255,1)
female = (91/255,155/255,213/255,05)
font = matplotlib.font_manager.FontProperties(
fname='D:/1.otf',size=25
)
plt.axis('equql')
plt.pie(
ga,
labels=['女','男'],
colors=[femaleColor,maleColor],
autopct='%.2f%%'
textprops={'fontproperties':font}
)
五、柱形图与条形图
result=data.groupby(
by=['手机品牌'],as_index=False
)['月消费'],sum()
import matplotlib
import matplotlib.pyplot as plt
plt.figure()
index = [1,2,3,4,5,6,7,8]
mainColor=(91/255,155/255,213/255,1)
font = matplotlib.font_manager.FontProperties(
fname='D:/1.otf',size=20
)
sgb = result.sort_values(
by='月消费',
ascending=False
)
plt.bar(index,
sgb['月消费']
color=mainColor
)
'''
# 对数据进行排序
sgb = result.sort_values(
by='月消费',
ascending=True
)
# 绘制条形图
plt.barh(
sgb['月消费']
color=mainColor
)
# 配置y轴刻度
plt.yticks(index,sgb.手机品牌,fontproperties=font)
'''
plt.xticks(index,sgb.手机品牌,fontproperties=font)