数据分析——matplotlib
程序员文章站
2022-06-30 14:02:40
基础 柱状图 小案例 学生成绩 导入类库 数据准备 数据提取 作图 ......
基础
1 # coding=utf-8 2 import matplotlib.pyplot as pt 3 import numpy as np 4 from matplotlib import font_manager # 字体管理 5 6 # pt.figure(num='ljb',facecolor='y',figsize=(8,6)) #num画框名,facecolor画框颜色,figsize画框大小(宽,高) 7 # pt.subplot(1,2,1) #子画框,1行1列1号位置 8 # x = np.linspace(-np.pi,np.pi,100) 9 # y = np.sin(x) 10 # b = np.cos(x) 11 # pt.plot(x,y,c='b',lw=4,ls='--') #c图形颜色,lw图形粗细,ls图形样式 12 # pt.subplot(1,2,2) 13 # pt.plot(x,b,c='g',lw=4,ls='--') 14 # pt.savefig('./sinx.png') #保存图形 15 16 # pt.figure(num='haha',facecolor='g') 17 # x = np.linspace(0,np.pi,100) 18 # y = np.cos(x)/np.sin(x) 19 # y = np.sin(x)/np.cos(x) 20 # pt.plot(x,y,'b+') 21 22 # 行,列,位置 23 # 画哪个就把哪个作为主体 24 # pt.figure(facecolor='r') 25 # x = np.linspace(-100, 100, 20) 26 # y = x ** 2 27 # pt.xlabel(u'x数值') #x轴名字 28 # pt.ylabel(u'y数值') #y轴名字 29 # pt.subplot(3,1,1,facecolor='y') #子图形颜色 30 # pt.plot(x,y,'bo') #b蓝色blue ,形状:o圆点 31 # 32 # a = np.linspace(-np.pi,np.pi,10) 33 # b = np.sin(a) 34 # c = np.cos(a) 35 # pt.subplot(3,3,4) 36 # pt.plot(a,b,'g+') #g绿色green,形状:+ 37 # pt.subplot(3,3,5) 38 # pt.plot(a,c,'r*') #r红色red,形状:* 39 # 40 # m = np.linspace(2, 10, 20) 41 # n = np.log(m) 42 # pt.subplot(3,3,6,facecolor='g') 43 # pt.plot(m,n,'md') #m紫色,形状d:钻石 44 # 45 # a = np.linspace(-100, 100, 20) 46 # b = a ** 2 47 # pt.subplot(3,2,5,facecolor='m') 48 # pt.plot(a,b,'bo') 49 # x = np.linspace(-100, 100, 20) 50 # y = x ** 2 51 # pt.subplot(3,2,6,facecolor='y') 52 # pt.plot(x,y,'b>') 53 # pt.show() 54 # subplot(3,2,6)可以简写为subplot(326) 55 56 pt.figure() 57 # pt.rcparams['font.sans-serif'] = ['字体名'] 58 myfont = font_manager.fontproperties(fname=u'c:\windows\fonts\时尚中黑简体.ttf') 59 x = np.linspace(-np.pi, np.pi, 50) 60 y = x ** 2 61 y1 = x ** 3 62 y2 = np.sin(x) 63 # 设置xy轴上下限 64 pt.xlim(-np.pi, np.pi) 65 pt.ylim(-1, 1) 66 # 设置x轴每个显示刻度 67 pt.xticks([-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi]) 68 # 设置某点坐标的文字:x坐标,y坐标以数据刻度为基准,显示的内容,字体配置 69 pt.text(0.0, 0.5, 'ljb', fontdict={'size': '16', 'color': 'm'}) 70 # 添加标注:xy:标注箭头想要指示的点,xytext:描述信息所在的坐标,arrowprops:设置箭头样式,shrink:箭头长度 71 pt.annotate('note!', xy=(np.pi / 2, 1), xytext=(np.pi / 2, 0.25), fontsize=16, 72 arrowprops=dict(facecolor='y', shrink=0.01)) 73 74 pt.xlabel(u'x数值', fontproperties=myfont, fontsize=12) # x轴名字及字体设置 75 pt.ylabel(u'y数值', fontproperties=myfont, fontsize=12) # y轴名字及字体设置 76 pt.title(u'函数图像', fontproperties=myfont, fontsize=16) # 图形标题及字体设置 77 # pt.plot(x,y,label=u'x2函数') 78 # pt.plot(x,y1,label=u'x3函数') 79 pt.plot(x, y2, 'bo', label=u'sinx函数') # label设置图例 80 pt.legend(prop=myfont) # 显示图例并设置字体 81 pt.show()
柱状图
1 # coding=utf-8 2 import numpy as np 3 import matplotlib.pyplot as pt 4 5 # 柱状图-------------------------------------- 6 k = 10 7 x = np.arange(k) #生成序列数组 8 y = np.random.rand(k) #生成对应的随机数 9 for t in x: #text设置某点坐标的文字,ha,va设置文字位置, 10 pt.text(t,y[t]+0.01,'%.2f' % y[t],ha='center',va='bottom',fontdict={'size':'10','color':'r'}) 11 pt.annotate('%.2f' % y[4],xy=(4,y[4]+0.05),xytext=(4.3,y[4]+0.15),fontsize=16,arrowprops=dict(facecolor='y',arrowstyle='fancy'),bbox = dict(boxstyle="sawtooth",fc="0.8")) 12 pt.bar(x,y) 13 # pt.barh(x,y) #横向柱状图 14 pt.show() 15 16 # 子图配置----------------------------------------- 17 # pt.figure(figsize=(10, 6)) 18 # x = np.linspace(-np.pi, np.pi, 30) 19 # y_sin = np.sin(x) 20 # y_cos = np.cos(x) 21 # 22 # # 获取子图的配置 23 # ax_sin = pt.subplot(121) #需要把子图存起来 24 # ax_sin.set_title('sin(x)') #单独设置某个子图标题 25 # ax_sin.set_xlabel('x') #单独设置某个子图x轴名字 26 # ax_sin.set_ylabel('y') #单独设置某个子图y轴名字 27 # pt.plot(y_sin, 'bo') 28 # 29 # ax_cos = pt.subplot(122) 30 # ax_cos.set_title('cos(x)') 31 # ax_cos.set_xlabel('x') 32 # ax_cos.set_ylabel('y') 33 # pt.plot(y_cos, 'r-') 34 # 35 # pt.show() 36 37 # 分类柱状图----------------------------------------------- 38 # from matplotlib import font_manager 39 # 40 # myfont = font_manager.fontproperties(fname=u'c:\windows\fonts\时尚中黑简体.ttf') 41 # 42 # b_16 = [15746, 312, 4497, 319] 43 # b_15 = [12357, 156, 2045, 168] 44 # b_14 = [2358, 399, 2358, 362] 45 # a = [u'猩球崛起3', u'敦刻尔克', u'蜘蛛侠', u'战狼2'] 46 # 47 # bar_width = 0.2 #设置一个条状图的宽度 48 # 49 # x_14 = list(range(len(a))) 50 # x_15 = [i + bar_width for i in x_14] 51 # x_16 = [i + bar_width * 2 for i in x_14] 52 # 53 # # 设置图形大小,分辨率 54 # pt.figure(figsize=(12, 6), dpi=80) 55 # 56 # ax = pt.subplot(211) 57 # 58 # ax.set_title('counter') 59 # ax.set_xlabel('x') 60 #参数1:位置列表,参数2:数据列表,参数3:柱宽,参数4:图例 61 # pt.bar(range(len(a)), b_14, width=bar_width, label=u'9月14日') 62 # pt.bar(x_15, b_15, width=bar_width, label=u'9月15日') 63 # pt.bar(x_16, b_16, width=bar_width, label=u'9月16日') 64 # 65 # # 设置图例 66 # pt.legend(prop=myfont) 67 # 68 # # 设置x轴的刻度 69 # pt.xticks(x_15, a, fontproperties=myfont) 70 # pt.show()
小案例
学生成绩
导入类库
1 from matplotlib import font_manager 2 import numpy as np 3 import matplotlib.pyplot as pt
数据准备
1 score_array = np.genfromtxt('score.csv', delimiter=',', dtype=int) 2 courses = [u'数学', u'语文', u'化学', u'地理', u'音乐', u'体育'] 3 students = [u'小数', u'小语', u'小化', u'小地', u'小音', u'小体'] 4 5 myfont = font_manager.fontproperties(fname=u'c:\windows\fonts\时尚中黑简体.ttf')
数据提取
1 student_0 = score_array[:,0] 2 student_1 = score_array[:,1] 3 student_2 = score_array[:,2] 4 student_3 = score_array[:,3] 5 student_4 = score_array[:,4] 6 student_5 = score_array[:,5] 7 8 bar_width = 0.1 9 x_0 = list(range(len(students))) 10 x_1 = [i + bar_width for i in x_0] 11 x_2 = [i + bar_width * 2 for i in x_0] 12 x_3 = [i + bar_width * 3 for i in x_0] 13 x_4 = [i + bar_width * 4 for i in x_0] 14 x_5 = [i + bar_width * 5 for i in x_0]
作图
1 pt.figure(figsize=(12, 5),dpi=80) 2 ax = pt.subplot(111) 3 4 ax.set_title('score') 5 ax.set_xlabel('students') 6 ax.set_ylabel('y') 7 8 pt.bar(range(len(students)), student_0, width=bar_width, label=courses[0]) 9 pt.bar(x_1, student_1, width=bar_width, label=courses[1]) 10 pt.bar(x_2, student_2, width=bar_width, label=courses[2]) 11 pt.bar(x_3, student_3, width=bar_width, label=courses[3]) 12 pt.bar(x_4, student_4, width=bar_width, label=courses[4]) 13 pt.bar(x_5, student_5, width=bar_width, label=courses[5]) 14 15 pt.legend(prop=myfont) 16 pt.xticks([i + bar_width * 2.5 for i in x_0], students, fontproperties=myfont) 17 pt.show()
下一篇: (django)11类视图