import matplotlib.pyplot as plt
plt.figure(1) # the first figure
plt.subplot(211) # the first subplot in the first figure
plt.plot([1,2,3])
plt.subplot(212) # the second subplot in tthe first figure
plt.plot([4,5,6])
plt.figure(2) # a second figure
plt.plot([4,5,6])
plt.figure(1) # figure 1 current; ubplot(212) still current
plt.subplot(211) # make subplot(211) in figure1 current
plt.title("Easy as 1, 2, 3")# subplot 211 tittle
plt.show()
import numpy as np
import matplotlib.pyplot as plt
# Fixing random state for reproducibility
np.random.seed(19680801)
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
# the histogram of the data
n, bins, patches = plt.hist(x, bins=10, normed=1, facecolor='g', edgecolor='k', alpha=0.75, histtype='bar')
#hist的参数非常多,但常用的就这六个,只有第一个是必须的,后面六个可选
#x: 需要计算直方图的一维数组
#bins: 直方图的柱数,可选项,默认为10,可不写bins=10,直接在x后面写10
#normed: 是否将得到的直方图向量归一化。默认为0
#facecolor: 直方图颜色
#edgecolor: 直方图边框颜色
#alpha: 透明度
#histtype: 直方图类型,‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’
#返回值:
#n: 直方图向量,是否归一化由参数normed设定
#bins: 返回各个bin的区间范围
#patches: 返回每个bin里面包含的数据,是一个list
print(n)#直方图的y值向量,是否归一化由参数normed决定
#[ 1.28221796e-04 9.14648810e-04 5.06048687e-03 1.65919004e-02
# 2.51229239e-02 2.27209022e-02 1.13262586e-02 3.09441934e-03
# 4.87242824e-04 3.41924789e-05]
print(bins)#返回各个bin的区间范围
#[ 43.48262893 55.18110804 66.87958716 78.57806627 90.27654539
# 101.97502451 113.67350362 125.37198274 137.07046185 148.76894097
# 160.46742008]
print(patches)#返回每一个bin里包含的数据,是一个list
#<a list of 10 Patch objects>
print(type(patches))
#<class 'matplotlib.cbook.silent_list'>
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
plt.axis([40, 160, 0, 0.03])
plt.grid(True)
plt.show()
各种图形的绘制
柱形图
data = [3685588454, 1399975394, 670070036, 413226974, 298643648, 247590988, 136104912, 115126068, 61071716, 48001466, 38891784, 25692216, 6555850, 5839100, 4661722]
labels = ['0.00-0.01', "", '0.02-0.03', "", '0.04-0.05', "", '0.06-0.07', "", '0.08-0.09', "", '0.10-0.11', "", '0.12-0.13', "", '0.14-0.15']
data = [a/7177459576 for a in data]
print(sum(data))
plt.bar(range(len(data)), data, tick_label=labels, width=0.85)
plt.xticks(rotation=90)
plt.xlabel(u'区间') # 给x轴数据加上名称
plt.ylabel(u'数量占比') # 给y轴数据加上名称
plt.title(u'区间数量占比分布') # 给整个图表加上标题
x = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
for xx, yy in zip(range(len(data)),data):
plt.text(xx, yy+0.005, str(round(yy*100, 1)) + '%', ha='center')
plt.show()