import matplotlib.pyplot as plt
labels=[' 搜索引擎','视频广告','联盟广告','邮件营销','直接访问']
sizes =[60.42,5.27,9.13,12.1,13.08]
fig, ax = plt.subplots()
colors=['lightgreen','orange','lightblue','blue','red']
ax.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=100,colors=colors)
circle = plt.Circle((0, 0), 0.7, fc='white')
fig.gca().add_artist(circle)
plt.legend(labels, loc='best')
plt.rcParams['font.sans-serif']=['SimHei']
plt.axis('equal')
plt.show()
import matplotlib.pyplot as plt
labels=['上海','福建','南京','广东','安徽','浙江','江苏','北京']
sizes = [29.19,25.59,4.44,4.07,2.25,1.73,1.35,31.37]
fig, ax = plt.subplots()
colors=['orange','green','red','purple','brown','pink','gray','lightblue']
ax.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=120,colors=colors)
circle = plt.Circle((0,0), 0.6, fc='white')
fig.gca().add_artist(circle)
plt.legend(labels, loc='best')
plt.rcParams['font.sans-serif']=['SimHei']
plt.axis('equal')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
def draw_histogram():
data = np.concatenate([np.random.normal(165, 5, 600), np.random.normal(175, 5, 1000),
np.random.normal(155, 5, 100)])
bin_edges = np.array([150, 155, 160, 165, 170, 175, 180, 185, 190,195])
hist, bin_edges = np.histogram(data, bins=bin_edges, density=True)
plt.bar(bin_edges[:-1], hist, width=np.diff(bin_edges), align='edge',
color='cyan', alpha=0.7)
plt.xlabel('身高')
plt.ylabel('概率密度')
plt.xticks(bin_edges[:-1])
plt.show()
draw_histogram()