數據分析——matplotlib

基礎

 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()
相關文章
相關標籤/搜索