5
5
"""
6
6
7
7
import numpy as np
8
+ import matplotlib
8
9
import matplotlib .mlab as mlab
9
10
import matplotlib .pyplot as plt
11
+ import matplotlib .font_manager as fm
10
12
from mpl_toolkits .mplot3d import Axes3D
11
13
14
+ # 解决中文乱码问题
15
+ myfont = fm .FontProperties (fname = "/Library/Fonts/Songti.ttc" , size = 14 )
16
+ matplotlib .rcParams ["axes.unicode_minus" ] = False
17
+
12
18
13
19
def simple_plot ():
14
20
"""
@@ -18,27 +24,27 @@ def simple_plot():
18
24
x = np .linspace (- np .pi , np .pi , 256 , endpoint = True )
19
25
y_cos , y_sin = np .cos (x ), np .sin (x )
20
26
21
- # 生成画布
27
+ # 生成画布,并设定标题
22
28
plt .figure (figsize = (8 , 6 ), dpi = 80 )
23
- plt .title ("plot title" )
29
+ plt .title ("plot title" , fontproperties = myfont )
24
30
plt .grid (True )
25
31
26
32
# 设置X轴
27
- plt .xlabel ("x label" )
33
+ plt .xlabel ("X轴" , fontproperties = myfont )
28
34
plt .xlim (- 4.0 , 4.0 )
29
35
plt .xticks (np .linspace (- 4 , 4 , 9 , endpoint = True ))
30
36
31
37
# 设置Y轴
32
- plt .ylabel ("y label" )
38
+ plt .ylabel ("Y轴" , fontproperties = myfont )
33
39
plt .ylim (- 1.0 , 1.0 )
34
40
plt .yticks (np .linspace (- 1 , 1 , 9 , endpoint = True ))
35
41
36
42
# 画两条曲线
37
- plt .plot (x , y_cos , "b--" , linewidth = 2.0 , label = "cos " )
38
- plt .plot (x , y_sin , "g-" , linewidth = 2.0 , label = "sin " )
43
+ plt .plot (x , y_cos , "b--" , linewidth = 2.0 , label = "cos事例 " )
44
+ plt .plot (x , y_sin , "g-" , linewidth = 2.0 , label = "sin事例 " )
39
45
40
46
# 设置图例位置,loc可以为[upper, lower, left, right, center]
41
- plt .legend (loc = "upper left" , shadow = True )
47
+ plt .legend (loc = "upper left" , prop = myfont , shadow = True )
42
48
43
49
# 图形显示
44
50
plt .show ()
@@ -54,7 +60,7 @@ def simple_advanced_plot():
54
60
x = np .linspace (- np .pi , np .pi , 256 , endpoint = True )
55
61
y_cos , y_sin = np .cos (x ), np .sin (x )
56
62
57
- # 生成画布
63
+ # 生成画布, 并设定标题
58
64
plt .figure (figsize = (8 , 6 ), dpi = 80 )
59
65
plt .title ("plot title" )
60
66
plt .grid (True )
@@ -80,9 +86,9 @@ def simple_advanced_plot():
80
86
ax_2 .set_yticks (np .linspace (- 2 , 2 , 9 , endpoint = True ))
81
87
82
88
# 设置X轴(共同)
83
- ax_2 .set_xlabel ("x label" )
84
- ax_2 .set_xlim (- 4.0 , 4.0 )
85
- ax_2 .set_xticks (np .linspace (- 4 , 4 , 9 , endpoint = True ))
89
+ ax_1 .set_xlabel ("x label" )
90
+ ax_1 .set_xlim (- 4.0 , 4.0 )
91
+ ax_1 .set_xticks (np .linspace (- 4 , 4 , 9 , endpoint = True ))
86
92
87
93
# 图形显示
88
94
plt .show ()
@@ -105,6 +111,7 @@ def subplot_plot():
105
111
106
112
# 子图的生成方式
107
113
plt .subplot (2 , 2 , num + 1 )
114
+ plt .title ("subplot %d" % (num + 1 ))
108
115
plt .plot (x , y , style_list [num ])
109
116
110
117
# 图形显示
@@ -122,6 +129,9 @@ def bar_plot():
122
129
means_men = (20 , 35 , 30 , 35 , 27 )
123
130
means_women = (25 , 32 , 34 , 20 , 25 )
124
131
132
+ # 设置标题
133
+ plt .title ("plot title" )
134
+
125
135
# 设置相关参数
126
136
index = np .arange (len (means_men ))
127
137
bar_width = 0.35
@@ -133,15 +143,15 @@ def bar_plot():
133
143
134
144
# 设置柱状图标示
135
145
for x , y in zip (index , means_men ):
136
- plt .text (x + ( bar_width / 2 ) , y + 0.3 , y , ha = "center" , va = "bottom" )
146
+ plt .text (x , y + 0.3 , y , ha = "center" , va = "bottom" )
137
147
for x , y in zip (index , means_women ):
138
- plt .text (x + bar_width + ( bar_width / 2 ) , y + 0.3 , y , ha = "center" , va = "bottom" )
148
+ plt .text (x + bar_width , y + 0.3 , y , ha = "center" , va = "bottom" )
139
149
140
150
# 设置刻度范围/坐标轴名称等
141
151
plt .ylim (0 , 45 )
142
152
plt .xlabel ("Group" )
143
153
plt .ylabel ("Scores" )
144
- plt .xticks (index + bar_width , ("A组 " , "B组 " , "C组 " , "D组 " , "E组 " ))
154
+ plt .xticks (index + ( bar_width / 2 ) , ("A " , "B " , "C " , "D " , "E " ))
145
155
146
156
# 图形显示
147
157
plt .show ()
@@ -157,6 +167,9 @@ def barh_plot():
157
167
means_men = (20 , 35 , 30 , 35 , 27 )
158
168
means_women = (25 , 32 , 34 , 20 , 25 )
159
169
170
+ # 设置标题
171
+ plt .title ("plot title" )
172
+
160
173
# 设置相关参数
161
174
index = np .arange (len (means_men ))
162
175
bar_height = 0.35
@@ -168,15 +181,15 @@ def barh_plot():
168
181
169
182
# 设置柱状图标示
170
183
for x , y in zip (index , means_men ):
171
- plt .text (y + 0.3 , x + ( bar_height / 2 ) , y , ha = "left" , va = "center" )
184
+ plt .text (y + 0.3 , x , y , ha = "left" , va = "center" )
172
185
for x , y in zip (index , means_women ):
173
- plt .text (y + 0.3 , x + bar_height + ( bar_height / 2 ) , y , ha = "left" , va = "center" )
186
+ plt .text (y + 0.3 , x + bar_height , y , ha = "left" , va = "center" )
174
187
175
188
# 设置刻度范围/坐标轴名称等
176
189
plt .xlim (0 , 45 )
177
190
plt .xlabel ("Scores" )
178
191
plt .ylabel ("Group" )
179
- plt .yticks (index + bar_height , ("A组 " , "B组 " , "C组 " , "D组 " , "E组 " ))
192
+ plt .yticks (index + ( bar_height / 2 ) , ("A " , "B " , "C " , "D " , "E " ))
180
193
181
194
# 图形显示
182
195
plt .show ()
@@ -192,6 +205,9 @@ def bar_advanced_plot():
192
205
means_men = np .array ((20 , 35 , 30 , 35 , 27 , 25 , 32 , 34 , 20 , 25 ))
193
206
means_women = np .array ((25 , 32 , 34 , 20 , 25 , 20 , 35 , 30 , 35 , 27 ))
194
207
208
+ # 设置标题
209
+ plt .title ("plot title" )
210
+
195
211
# 设置相关参数
196
212
index = np .arange (len (means_men ))
197
213
bar_width = 0.8
@@ -201,14 +217,14 @@ def bar_advanced_plot():
201
217
plt .bar (index , - means_women , width = bar_width , alpha = 0.4 , color = "r" , label = "Women" )
202
218
203
219
# 画折线图(两种,和柱状图对应)
204
- plt .plot (index + ( bar_width / 2 ) , means_men , marker = "o" , linestyle = "-" , color = "r" , label = "Men line" )
205
- plt .plot (index + ( bar_width / 2 ) , - means_women , marker = "." , linestyle = "--" , color = "b" , label = "Women line" )
220
+ plt .plot (index , means_men , marker = "o" , linestyle = "-" , color = "r" , label = "Men line" )
221
+ plt .plot (index , - means_women , marker = "." , linestyle = "--" , color = "b" , label = "Women line" )
206
222
207
223
# 设置图形标示(两种,和柱状图对应)
208
224
for x , y in zip (index , means_men ):
209
- plt .text (x + ( bar_width / 2 ) , y + 1 , y , ha = "center" , va = "bottom" )
225
+ plt .text (x , y + 1 , y , ha = "center" , va = "bottom" )
210
226
for x , y in zip (index , means_women ):
211
- plt .text (x + ( bar_width / 2 ) , - y - 1 , y , ha = "center" , va = "top" )
227
+ plt .text (x , - y - 1 , y , ha = "center" , va = "top" )
212
228
213
229
# 设置Y轴和图例位置
214
230
plt .ylim (- 45 , 80 )
@@ -231,6 +247,9 @@ def table_plot():
231
247
[5 , 3 , 6 , 4 , 1 ]
232
248
])
233
249
250
+ # 设置标题
251
+ plt .title ("plot title" )
252
+
234
253
# 设置相关参数
235
254
index = np .arange (len (data [0 ]))
236
255
color_index = ["r" , "g" , "b" ]
@@ -240,7 +259,7 @@ def table_plot():
240
259
241
260
# 依次画图,并更新底部位置
242
261
for i in range (len (data )):
243
- plt .bar (index + 0.25 , data [i ], width = 0.5 , color = color_index [i ], bottom = bottom , alpha = 0.7 , label = "label %d" % i )
262
+ plt .bar (index , data [i ], width = 0.5 , color = color_index [i ], bottom = bottom , alpha = 0.7 , label = "label %d" % i )
244
263
bottom += data [i ]
245
264
246
265
# 设置图例位置
@@ -260,13 +279,13 @@ def histograms_plot():
260
279
mu , sigma = 100 , 15
261
280
x = mu + sigma * np .random .randn (10000 )
262
281
263
- # 设置相关参数
264
- num_bins = 50
282
+ # 设置标题
283
+ plt . title ( "plot title" )
265
284
266
- # 画直方图,并返回相关结果
267
- n , bins , patches = plt .hist (x , bins = num_bins , normed = 1 , color = "green" , alpha = 0.6 , label = "hist" )
285
+ # 画直方图, 并返回相关结果
286
+ n , bins , patches = plt .hist (x , bins = 50 , normed = 1 , cumulative = False , color = "green" , alpha = 0.6 , label = "hist" )
268
287
269
- # 根据直方图返回的结果,画折线图
288
+ # 根据直方图返回的结果, 画折线图
270
289
y = mlab .normpdf (bins , mu , sigma )
271
290
plt .plot (bins , y , "r--" , label = "line" )
272
291
@@ -285,10 +304,15 @@ def pie_plot():
285
304
"""
286
305
# 生成测试数据
287
306
sizes = [15 , 30 , 45 , 10 ]
288
- explode = [0 , 0.05 , 0 , 0 ]
289
307
labels = ["Frogs" , "Hogs" , "Dogs" , "Logs" ]
290
308
colors = ["yellowgreen" , "gold" , "lightskyblue" , "lightcoral" ]
291
309
310
+ # 设置标题
311
+ plt .title ("plot title" )
312
+
313
+ # 设置突出参数
314
+ explode = [0 , 0.05 , 0 , 0 ]
315
+
292
316
# 画饼状图
293
317
plt .pie (sizes , explode = explode , labels = labels , colors = colors , autopct = "%1.1f%%" , shadow = True , startangle = 90 )
294
318
plt .axis ("equal" )
@@ -308,6 +332,9 @@ def scatter_plot():
308
332
x_index = np .random .random (point_count )
309
333
y_index = np .random .random (point_count )
310
334
335
+ # 设置标题
336
+ plt .title ("plot title" )
337
+
311
338
# 设置相关参数
312
339
color_list = np .random .random (point_count )
313
340
scale_list = np .random .random (point_count ) * 100
@@ -329,13 +356,15 @@ def fill_plot():
329
356
x = np .linspace (- 2 * np .pi , 2 * np .pi , 1000 , endpoint = True )
330
357
y = np .sin (x )
331
358
359
+ # 设置标题
360
+ plt .title ("plot title" )
361
+
332
362
# 画图
333
363
plt .plot (x , y , color = "blue" , alpha = 1.00 )
334
364
335
- # 填充图形
336
- # plt.fill_between(x, y1, y2, where=None, *kwargs)
337
- plt .fill_between (x , 0 , y , y > 0 , color = "blue" , alpha = .25 )
338
- plt .fill_between (x , 0 , y , y < 0 , color = "red" , alpha = .25 )
365
+ # 填充图形, plt.fill_between(x, y1, y2, where=None, *kwargs)
366
+ plt .fill_between (x , 0 , y , where = (y > 0 ), color = "blue" , alpha = 0.25 )
367
+ plt .fill_between (x , 0 , y , where = (y < 0 ), color = "red" , alpha = 0.25 )
339
368
340
369
# 图形显示
341
370
plt .show ()
@@ -349,7 +378,7 @@ def radar_plot():
349
378
"""
350
379
# 生成测试数据
351
380
labels = np .array (["A" , "B" , "C" , "D" , "E" , "F" ])
352
- data = np .array ([38 , 43 , 90 , 67 , 89 , 73 ])
381
+ data = np .array ([68 , 83 , 90 , 77 , 89 , 73 ])
353
382
theta = np .linspace (0 , 2 * np .pi , len (data ), endpoint = False )
354
383
355
384
# 数据预处理
@@ -358,10 +387,11 @@ def radar_plot():
358
387
359
388
# 画图方式
360
389
plt .subplot (111 , polar = True )
390
+ plt .title ("plot title" )
361
391
362
392
# 设置"theta grid"/"radar grid"
363
393
plt .thetagrids (theta * (180 / np .pi ), labels = labels )
364
- plt .rgrids (np .arange (20 , 101 , 20 ), labels = np .arange (20 , 101 , 20 ), angle = 0 )
394
+ plt .rgrids (np .arange (20 , 100 , 20 ), labels = np .arange (20 , 100 , 20 ), angle = 0 )
365
395
plt .ylim (0 , 100 )
366
396
367
397
# 画雷达图,并填充雷达图内部区域
@@ -379,17 +409,16 @@ def three_dimension_scatter():
379
409
3d scatter plot
380
410
"""
381
411
# 生成测试数据
382
- number = 1000
383
- x = np .random .random (number )
384
- y = np .random .random (number )
385
- z = np .random .random (number )
386
- color = np .random .random (number )
387
- scale = np .random .random (number ) * 100
412
+ x = np .random .random (100 )
413
+ y = np .random .random (100 )
414
+ z = np .random .random (100 )
415
+ color = np .random .random (100 )
416
+ scale = np .random .random (100 ) * 100
388
417
389
418
# 生成画布(两种形式)
390
419
fig = plt .figure ()
391
- # ax = fig.gca(projection="3d")
392
- ax = fig .add_subplot (111 , projection = "3d" )
420
+ # ax = fig.gca(projection="3d", title="plot title" )
421
+ ax = fig .add_subplot (111 , projection = "3d" , title = "plot title" )
393
422
394
423
# 画三维散点图
395
424
ax .scatter (x , y , z , s = scale , c = color , marker = "." )
@@ -415,15 +444,14 @@ def three_dimension_line():
415
444
3d line plot
416
445
"""
417
446
# 生成测试数据
418
- number = 1000
419
- x = np .linspace (0 , 1 , number )
420
- y = np .linspace (0 , 1 , number )
447
+ x = np .linspace (0 , 1 , 1000 )
448
+ y = np .linspace (0 , 1 , 1000 )
421
449
z = np .sin (x * 2 * np .pi ) / (y + 0.1 )
422
450
423
451
# 生成画布(两种形式)
424
452
fig = plt .figure ()
425
- ax = fig .gca (projection = "3d" )
426
- # ax = fig.add_subplot(111, projection="3d")
453
+ ax = fig .gca (projection = "3d" , title = "plot title" )
454
+ # ax = fig.add_subplot(111, projection="3d", title="plot title" )
427
455
428
456
# 画三维折线图
429
457
ax .plot (x , y , z , color = "red" , linestyle = "-" )
@@ -455,8 +483,7 @@ def three_dimension_bar():
455
483
456
484
# 生成画布(两种形式)
457
485
fig = plt .figure ()
458
- ax = fig .gca (projection = "3d" )
459
- # ax = fig.add_subplot(111, projection="3d")
486
+ ax = fig .gca (projection = "3d" , title = "plot title" )
460
487
461
488
# 画三维柱状图
462
489
ax .bar3d (xpos , ypos , zpos , dx , dy , dz , alpha = 0.5 )
0 commit comments