# matplotlib--python的数据可视化二

#### 饼状图

``````import matplotlib.pyplot as plt
labels = ['a','b','c']
weights = [100,60,30]
colors = ['yellowgreen','gold','lightskyblue']
#就是突出出来
explodes = [0.05, 0, 0]
#第一个参数：数据
#explodes突出出来的距离
#labeldistance label距离圆心的距离/半径
#autopct 设定显示格式
#startangle 开始角度  这是xy轴的360度 自己试试就懂了
#pctdistance 数据距离圆心的距离/半径
patchs, l_texts, p_texts = plt.pie(weights, explode=explodes, labels=labels, labeldistance=1.1, autopct='%2.1f%%', shadow=False, startangle=0,
pctdistance=0.6)
#plt.axis('equal') #xy轴相等  也就是圆
plt.legend() #图例
#这样设置字的大小
# for l in l_texts:
#     l.set_size(20)
# for p in p_texts:
#     p.set_size(20)
plt.show()
``````
image.png

#### 柱状图

``````import numpy as np
from matplotlib import pyplot as plt

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

n = 12
X = np.arange(n)+1
# numpy.random.uniform(low=0.0, high=1.0, size=None), normal
Y1 = (1-X/float(n+1)) * np.random.uniform(0.5,1.0,n)
Y2 = (1-X/float(n+1)) * np.random.uniform(0.5,1.0,n)

# bar and barh
width = 0.5#间距
#heigh=width则为水平柱状图
#facecolor柱体颜色
#edgecolor柱边颜色

plt.bar(X, Y1, width=width, facecolor='#9999ff', edgecolor='white')
plt.bar(X, -Y2, width=width, facecolor='#ff9999', edgecolor='white')
#plt.bar(X+width, -Y2, width=width, facecolor='#ff9999', edgecolor='white')#并列柱状图

for x,y in zip(X,Y1):
plt.text(x+0.4, y+0.05, '%.2f' % y, ha='center', va= 'bottom')

for x,y in zip(X,-Y2):
plt.text(x+0.4, y-0.15, '%.2f' % y, ha='center', va= 'bottom')

#plt.ylim(-1.25,+1.25)
plt.show()
``````
image.png

#### 概率分布

``````from matplotlib import pyplot as plt
import numpy as np

mu = 0
sigma = 1
#rand是均匀分布  randn是正态分布
x = mu + sigma*np.random.randn(10000)

fig,(ax0,ax1)=plt.subplots(ncols=2, figsize=(9,6))
#histype是选择用什么表示，这里用的是柱状图
#normed=1 归1化
ax0.hist(x, 20, normed=1, histtype='bar', facecolor='g', alpha=0.75)
ax0.set_title('pdf')
#cumulative=Ture是累加模式
ax1.hist(x, 20, normed=1, histtype='bar', rwidth=0.8, cumulative=True)
ax1.set_title('cdf')

plt.show()
``````
image.png

image
python