直方图
直方图实际上是用长方形的面积表示频数
参数只有一个
hist()的参数:
- bins 条数
- normed 归一化处理 normed=True
- color
- orientation 方向 默认为vertical竖直,horizontal为水平
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
x = np.random.randint(0,10,10)
plt.hist(x,20,normed=True,orientation='horizontal')
条形图
条形图是用条形的高度表示频数的大小
有两个参数x,y
- bar()
- barh() 水平条形图
x = np.linspace(0,5,5)
y = np.random.randint(0,25,size=5) #(0,25,5)
plt.bar(x,y)
plt.barh(x,y)
饼图
只有一个参数x pie()适合展示各部分的比例,条形图表示各部分的大小
p = np.array([0.7,0.2,0.1])
plt.pie(p,labels=['dog','cat','other'],autopct='%1.2f%%')
#plt.axis('equal')
饼图未占满
p = np.array([0.6,0.2,0.1])
plt.pie(p,labels=['dog','cat','other'],autopct='%1.2f%%')
p = np.array([0.6,0.2,0.1])
plt.pie(p,
labels=['dog','cat','other'], #标签
labeldistance=1.2, # 标签到圆心距离
pctdistance = 0.5, # --% 距离圆心距离
explode= [0.2,0,0.6], #(0.2,0,0.6) 脱离圆心距离
shadow=True, #阴影
startangle=90, #角度
autopct='%1.2f%%') #百分比
散点图
scatter() x,y x为每个点的横坐标
x = np.random.randn(1000)
y = np.random.randn(1000)
plt.figure(figsize=(15,9))
#产生随机颜色
color = np.random.random(3000).reshape((1000,3))
size = np.random.randint(0,80,1000)
plt.scatter(x,y,color=color,s=size,marker='D')
#关闭坐标轴
plt.axis('off')
plt.savefig(r'C:\Users\‘\Desktop\python\数据分析\matplotlib使用\风格和样式\caise.png',dpi=1000)
网友评论