import matplotlib.pyplot as pltimport numpy as npdeffunc(t):return np.exp(-t)*np.cos(2*np.pi*t)t1 = np.arange(0,5,0.2)t2 = np.arange(0,5,0.05)plt.figure(1)plt.subplot(211)plt.plot(t1,func(t1),'bo',t2,func(t2),'k')plt.subplot(212)plt.plot(t2,np.cos(2*np.pi*t2),'r--')plt.show()
import matplotlib.pyplot as pltplt.figure(1)# the first figureplt.subplot(211)# the first subplot in the first figureplt.plot([1,2,3])plt.subplot(212)# the second subplot in tthe first figureplt.plot([4,5,6])plt.figure(2)# a second figureplt.plot([4,5,6])plt.figure(1)# figure 1 current; ubplot(212) still currentplt.subplot(211)# make subplot(211) in figure1 currentplt.title("Easy as 1, 2, 3")# subplot 211 tittleplt.show()
import numpy as npimport matplotlib.pyplot as plt# Fixing random state for reproducibilitynp.random.seed(19680801)mu, sigma =100,15x = mu + sigma * np.random.randn(10000)# the histogram of the datan, 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里面包含的数据,是一个listprint(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()