本文共 3303 字,大约阅读时间需要 11 分钟。
1、LinePlot
import matplotlib.pyplot as pltimport numpy as npt = np.arange(0.0,2.0,0.01)s = 1 + np.sin(2*np.pi*t)# fig,ax = plt.subplots() 等价于 先生成一个figure 然后再 添加子图(add_subplot(111))fig = plt.figure()ax = fig.add_subplot(111)ax.plot(t,s)ax.set(xlabel='time(s)',ylabel='voltage(mV)',title='About as simple as it gets,folks')ax.grid()plt.show()
2、MultiPlot
import numpy as npimport matplotlib.pyplot as pltx1 = np.linspace(0.0, 5.0) #linspace 默认 数据点为50个x2 = np.linspace(0.0, 2.0)y1 = np.cos(2*np.pi*x1)*np.exp(-x1)y2 = np.cos(2*np.pi*x2)''' 可以直接用plt.subplot()'''# plt.subplot(2,1,1)# plt.plot(x1,y1,'o-')# plt.title('A tale of 2 subplots')# plt.ylabel('Damped oscillation')# plt.subplot(2,1,2)# plt.plot(x2,y2,'.-')# plt.xlabel('time (s)')# plt.ylabel('Undamped')# plt.show()''' 可以生成figure 后 逐个添加 sub_plot'''fig = plt.figure()ax = fig.add_subplot(211)ax.plot(x1,y1,'*-')ax.set_title('A tale of 2 subplots')ax2 = fig.add_subplot(212)ax2.plot(x2,y2,'o-')ax2.set_title('Damped oscillation')plt.show()
3、Histogram(直方图)
import matplotlibimport numpy as npimport matplotlib.pyplot as plt''' hist 函数就是根据输入的数据x 画出分布直方图(不同数据多少)'''np.random.seed(19680801) # 随机种子mu = 100 # 均值sigma = 15 # 标准差#生成 400 个随机数列表x = np.random.randn(400)x = mu + sigma * np.random.randn(400)num_bins = 50fig, ax = plt.subplots()'''input: x: 可以是一组或多组输入值 num_bins:直方图数 用于计算bins 边界 density: 保证所有直方图 和为1 :return n : 每个直方图的值 bins :长度为nbin + 1 每个直方图的左边缘值 + 最后一个右边缘值 patches: 这个返回值可以不用管 '''n, bins, patches = ax.hist(x, num_bins, density=1)#添加一个拟合直方图的曲线y = ((1 / (np.sqrt(2 * np.pi) * sigma)) * np.exp(-0.5 * (1 / sigma * (bins - mu))**2))ax.plot(bins, y, '--')ax.set_xlabel('Smarts')ax.set_ylabel('Probability density')ax.set_title(r'Histogram of IQ: $\mu=100$, $\sigma=15$')fig.tight_layout()plt.show()
4、BarCharts(条形图)
import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.ticker import MaxNLocatorfrom collections import namedtuplen_groups = 5means_men = (20, 35, 30, 35, 27)std_men = (2, 3, 4, 1, 2)means_women = (25, 32, 34, 20, 25)std_women = (3, 5, 2, 3, 3)fig, ax = plt.subplots()index = np.arange(n_groups)bar_width = 0.35#透明度opacity = 0.4error_config = {'ecolor': '0.3'}rects1 = ax.bar(index, means_men, bar_width, alpha=opacity, color='b', yerr=std_men, error_kw=error_config, label='Men')# 注意起始位置 index+bar_width 控制 其与 rects1的间距rects2 = ax.bar(index + bar_width, means_women, bar_width, alpha=opacity, color='r', yerr=std_women, error_kw=error_config, label='Women')ax.set_xlabel('Group')ax.set_ylabel('Scores')ax.set_title('Scores by group and gender')# tick 设置标记的位置ax.set_xticks(index + bar_width/2)#设置标记的标签ax.set_xticklabels(('A', 'B', 'C', 'D', 'E'))ax.legend()fig.tight_layout()plt.show()
5、PieChart
import matplotlib.pyplot as plt# counter-clockwise 标签按照逆时针画出labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'sizes = [15, 30, 45, 10]# 对第二个 'Hogs' 单独处理 往外偏移0.1explode = (0, 0.1, 0, 0)fig1, ax1 = plt.subplots()''' size: array-like 数据值的数组 explode:描述每一部分偏移量 长度必须和size相等 autopct:指定 value值的格式 %% 表示% startangle:指定起始角度 ,默认为x轴 这里指定为90 即 逆时针旋转90度'''ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90)# 保证各部分占比相等 为圆形ax1.axis('equal')plt.show()