数据分析——matplotlib

2018-10-26 05:29:00来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

基础

 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()

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:万恶之源 - Python基础知识补充

下一篇:第七天-数据池/常量池 编码补充