matplotlib:
能将数据进行可视化,更直观的呈现
使数据更加客观,更具有说服力
matplotlib架构:
上层调用下层后端:实现绘图区域(分配绘图的资源)美工:figure,axes,axis脚本:pyplot
基础绘图:
折线图:点的坐标(横坐标、纵坐标)figure(绘制画图区域),plot,show(展示坐标轴)xticks,yticks(设置刻度,中文)xlable,ylable(设置坐标轴解释)plot(参数)(绘制坐标)lengend(设置显示图例)plt.subplots(nrows=Num,ncols=Num,figsize=(20,8))(实现多个坐标系的图绘制)
直方图:
组数:数据按照不同的范围分成几个组
组距:每个组两个端点的差
API:
plt.hist(x,bins=None,normed=None,**kwargs)
plt.grid(True,linestyle=’–’,alpha=0.5):显示网格
对比:
直方图:适合x坐标是连接的数据,数据量大柱状图:适合类别少、数据量小
饼图:
应用场景:表示不同分类的占比情况API:plt.pie(x,explode=List,labels=None,autopct=’%1.2f%%’,colors=List,shadow=True,startangle=Num)绘制:注意显示整圆形 plt.axis(‘equal’)
K线图
API:
candlestick_ochl(axes,day,width=0.2,colorup=‘r’,colordown=‘g’)
折线图、柱状图、直方图、饼状图案例
折线图初体验
plt
.figure
(figsize
=(20,8))
x
= range(10)
y
= range(10,20)
plt
.plot
(x
,y
)
plt
.show
()
折线图初体验
plt
.figure
(figsize
=(20,8))
x
= range(60)
y_shanghai
= [random
.uniform
(15,18) for i
in x
]
plt
.plot
(x
,y_shanghai
)
plt
.show
()
两个城市的温度在一个坐标系显示
plt
.figure
(figsize
=(20,8))
x
= range(60)
y_shanghai
= [random
.uniform
(15,18) for i
in x
]
y_beijing
= [random
.uniform
(1,3) for i
in x
]
x_ch
= ["11点{}分".format(i
) for i
in x
]
y_ticks
= range(40)
plt
.plot
(x
,y_shanghai
,label
='上海')
plt
.plot
(x
,y_beijing
,color
='r',linestyle
='--',label
='北京')
plt
.xticks
(x
[::5],x_ch
[::5],fontsize
=20)
plt
.yticks
(y_ticks
[::5],fontsize
=20)
plt
.xlabel
('时间',fontsize
=20)
plt
.ylabel
('温度',fontsize
=20)
plt
.title
('一些城市从11点到12点之间的温度',fontsize
=20)
plt
.legend
(loc
='best',fontsize
=20)
plt
.show
()
两个城市的温度,在多个坐标系显示
fig
,ax
= plt
.subplots
(nrows
=1,ncols
=2,figsize
=(20,8))
x
= range(60)
y_shanghai
= [random
.uniform
(15,18) for i
in x
]
y_beijing
= [random
.uniform
(1,3) for i
in x
]
x_ch
= ["11点{}分".format(i
) for i
in x
]
y_ticks
= range(40)
ax
[0].plot
(x
,y_shanghai
,label
='上海')
ax
[1].plot
(x
,y_beijing
,color
='r',linestyle
='--',label
='北京')
ax
[0].set_xticks
(x
[::5],x_ch
[::5])
ax
[1].set_xticks
(x
[::5],x_ch
[::5])
ax
[0].set_yticks
(y_ticks
[::5])
ax
[1].set_yticks
(y_ticks
[::5])
ax
[0].set_xlabel
('时间',fontsize
=20)
ax
[0].set_ylabel
('温度',fontsize
=20)
ax
[0].set_title
('一些城市从11点到12点之间的温度',fontsize
=20)
ax
[1].set_xlabel
('时间',fontsize
=20)
ax
[1].set_ylabel
('温度',fontsize
=20)
ax
[1].set_title
('一些城市从11点到12点之间的温度',fontsize
=20)
ax
[0].legend
(loc
='best',fontsize
=20)
ax
[1].legend
(loc
='best',fontsize
=20)
plt
.show
()
电影票房数据的对比(柱状图)
plt
.figure
(figsize
=(20,8))
movie_name
= ['雷神3:诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴','降魔传','追捕','七十七天','密战','狂兽','其他']
y
= [73853,57767,22354,15969,14839,8725,8716,8318,7916,6764,52222]
x
= range(len(movie_name
))
color
= ['b','r','g','y','c','m','y','k','c','g','g']
plt
.bar
(x
,y
,width
=0.2,color
=color
)
plt
.xticks
(x
,movie_name
,fontsize
=16)
plt
.show
()
对比不同电影的首日、首周的电影票房
plt
.figure
(figsize
=(20,8))
movie_name
= ['雷声3:诸神黄昏','正义联盟','寻梦环游记']
first_day
= [10587.6,10062.5,1275.7]
first_weekend
= [36224.9,34479.6,11830]
x
= range(len(movie_name
))
plt
.bar
(x
,first_day
,width
=0.2,label
='首日票房')
plt
.bar
([i
+0.2 for i
in x
],first_weekend
,width
=0.2,label
='首周票房')
plt
.xticks
([i
+0.1 for i
in x
],movie_name
,fontsize
=16)
plt
.legend
(loc
='best',fontsize
=20)
plt
.show
()
电影时长分布直方图
plt
.figure
(figsize
=(20,8))
time
= [131,98,125,131,139,131,117,128,108,135,138,131,102,107,114,119,128,121,142,127,130,124,124,127,138,117,121,102,130,138,137,126,128,132,131,119,114,126,115]
bins
= 2
group
= int((max(time
)-min(time
))/bins
)
plt
.hist
(time
,group
)
plt
.xticks
(list(range(min(time
),max(time
)))[::2])
plt
.xlabel
('电影市场大小',fontsize
=20)
plt
.ylabel
('电影的数据量',fontsize
=20)
plt
.grid
(True,linestyle
='--',alpha
=0.5)
plt
.show
()
电影的拍片占比显示
plt
.figure
(figsize
=(20,8))
movie_name
= ['雷神3:诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴','降魔传','追捕','七十七天','密战','狂兽','其他']
place_count
= [60605,54546,45819,28243,13270,9945,7679,6799,6101,4621,20105]
color
= ['b','r','g','y','c','m','y','k','c','g','g']
plt
.pie
(place_count
,labels
=movie_name
,autopct
='%1.2f%%',colors
=color
)
plt
.axis
('equal')
plt
.legend
(loc
='best')
plt
.show
()
饼状图显示宠物偏爱比例 labels
= 'Frogs','Hogs','Dogs','Logs'
sizes
= [15,30,45,10]
explode
= (0,0.1,0,0)
fi1
,ax1
= plt
.subplots
()
ax1
.pie
(sizes
,explode
=explode
,labels
=labels
,autopct
='%1.1f%%',shadow
=True,startangle
=90)
ax1
.axis
('equal')
plt
.show
()