python繪制圖形


    python能快速解決日常工作中的小任務,比如數據展示。

  python做數據展示,主要用到matplotlib庫,使用簡單的代碼,就可以很方便的繪制折線圖、柱狀圖等。使用Java等,可能還需要配合html來進行展示,十分繁瑣。

繪制的圖形舉例如下:

     

 

 

 各種平面圖的繪制代碼:

 1 '''
 2 File Name:   draw
 3 Author:      tim
 4 Date:        2018/8/15 16:47
 5 Description: 圖形繪制。十分有用,對於工作中實驗性的項目,可以快速展示效果。如果使用java,還需要配合前端展示。
 6 '''
 7 
 8 import matplotlib.pyplot as plt
 9 import numpy as np  # 模塊取別名
10 
11 
12 # 直方圖
13 def draw_hist():
14     mu = 100
15     sigma = 20
16 
17     x = mu + sigma * np.random.randn(20000)  # 樣本數量
18     plt.hist(x, bins=100, color='green', normed=True)  # bins:顯示有幾個直方,normed是否對數據進行標准化
19 
20     plt._show()
21 
22 
23 # 條形圖
24 def draw_bar():
25     y = [20, 10, 30, 25, 15]  # Y軸數據
26     index = np.arange(5)  # X軸數據,也可以是index = [0,5]
27 
28     plt.bar(left=index, height=y, color='blue', width=0.5)
29     plt.show()
30 
31 
32 # 折線圖
33 def draw_plot():
34     x = np.linspace(-10, 10, 100)  # -10到10,100個點
35     y = x ** 3  # x的3次冪
36 
37     plt.plot(x, y, linestyle='--', color='orange', marker='<')
38     plt.xlabel('X')
39     plt.ylabel('Y')
40 
41     plt.show()
42 
43 
44 # 散點圖
45 def draw_scatter():
46     x = np.random.randn(1000)
47     y = x + np.random.randn(1000) * 0.5
48 
49     plt.scatter(x, y, s=5, marker='<')  # s表示面積,marker表示圖形
50     plt.show()
51 
52 
53 # 餅狀圖
54 def draw_pie():
55     labels = 'A', 'B', 'C', 'D'  # 4個模塊
56     fracs = [15, 30, 45, 10]  # 每個模塊占比例
57 
58     plt.axes(aspect=1)  # 使x、y軸比例相同
59     explode = [0, 0.5, 0, 0]  # 突出某一部分區域
60 
61     plt.pie(x=fracs, labels=labels, autopct='%.0f%%', explode=explode)  # autopct顯示百分比
62     plt.show()
63 
64 
65 # 帶圖例
66 def draw_with_legend():
67     x = np.arange(1, 11, 1)  # x軸坐標,1開始,11結束,步長為1
68 
69     plt.plot(x, x * 2)  # 第一條線,x,y坐標
70     plt.plot(x, x * 3)
71     plt.plot(x, x * 4)
72 
73     plt.legend(['Normal', 'Fast', 'Faster'])  # 設置圖例,與上面的線對應
74     plt.grid(True, color='green', linestyle='--', linewidth=1)  # 繪制網格
75 
76     plt.show()
77 
78 
79 # start
80 if __name__ == '__main__':
81     # draw_hist()
82     # draw_bar()
83     draw_plot()
84     # draw_scatter()
85     # draw_pie()
86     # draw_with_legend()

 

3D圖的繪制代碼:

 1 '''
 2 File Name:   draw_3d
 3 Author:      tim
 4 Date:        2018/8/15 18:42
 5 Description: 3D繪圖
 6 '''
 7 
 8 import numpy as np
 9 import matplotlib.pyplot as plt
10 from mpl_toolkits.mplot3d import Axes3D
11 
12 
13 # 3D 繪制
14 def draw_3D():
15     fig = plt.figure()  # 定義一個窗口
16     ax = Axes3D(fig)  # 繪制3D坐標
17 
18     # 設置x、y、z的值
19     x = np.arange(-4, 4, 0.25)
20     y = np.arange(-4, 4, 0.25)
21     x, y = np.meshgrid(x, y)  # x-y 平面的網格
22 
23     r = np.sqrt(x ** 2 + y ** 2)
24     z = np.sin(r)  # z值
25 
26     # 做出一個三維曲面,並將一個 colormap rainbow 填充顏色,之后將三維圖像投影到 XY 平面上做一個等高線圖
27     # rstride 和 cstride 分別代表 row 和 column 的跨度。
28     ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'))
29 
30     # 添加 XY 平面的等高線
31     ax.contourf(x, y, z, zdir='z', offset=-2, cmap=plt.get_cmap('rainbow'))
32 
33     ax.set_zlim(-2, 2)
34     plt.show()  # 展示
35 
36 
37 # start
38 if __name__ == '__main__':
39     draw_3D()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM