55"""
66
77import numpy as np
8+ import matplotlib
89import matplotlib .mlab as mlab
910import matplotlib .pyplot as plt
11+ import matplotlib .font_manager as fm
1012from mpl_toolkits .mplot3d import Axes3D
1113
14+ # 解决中文乱码问题
15+ myfont = fm .FontProperties (fname = "/Library/Fonts/Songti.ttc" , size = 14 )
16+ matplotlib .rcParams ["axes.unicode_minus" ] = False
17+
1218
1319def simple_plot ():
1420 """
@@ -18,27 +24,27 @@ def simple_plot():
1824 x = np .linspace (- np .pi , np .pi , 256 , endpoint = True )
1925 y_cos , y_sin = np .cos (x ), np .sin (x )
2026
21- # 生成画布
27+ # 生成画布,并设定标题
2228 plt .figure (figsize = (8 , 6 ), dpi = 80 )
23- plt .title ("plot title" )
29+ plt .title ("plot title" , fontproperties = myfont )
2430 plt .grid (True )
2531
2632 # 设置X轴
27- plt .xlabel ("x label" )
33+ plt .xlabel ("X轴" , fontproperties = myfont )
2834 plt .xlim (- 4.0 , 4.0 )
2935 plt .xticks (np .linspace (- 4 , 4 , 9 , endpoint = True ))
3036
3137 # 设置Y轴
32- plt .ylabel ("y label" )
38+ plt .ylabel ("Y轴" , fontproperties = myfont )
3339 plt .ylim (- 1.0 , 1.0 )
3440 plt .yticks (np .linspace (- 1 , 1 , 9 , endpoint = True ))
3541
3642 # 画两条曲线
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事例 " )
3945
4046 # 设置图例位置,loc可以为[upper, lower, left, right, center]
41- plt .legend (loc = "upper left" , shadow = True )
47+ plt .legend (loc = "upper left" , prop = myfont , shadow = True )
4248
4349 # 图形显示
4450 plt .show ()
@@ -54,7 +60,7 @@ def simple_advanced_plot():
5460 x = np .linspace (- np .pi , np .pi , 256 , endpoint = True )
5561 y_cos , y_sin = np .cos (x ), np .sin (x )
5662
57- # 生成画布
63+ # 生成画布, 并设定标题
5864 plt .figure (figsize = (8 , 6 ), dpi = 80 )
5965 plt .title ("plot title" )
6066 plt .grid (True )
@@ -80,9 +86,9 @@ def simple_advanced_plot():
8086 ax_2 .set_yticks (np .linspace (- 2 , 2 , 9 , endpoint = True ))
8187
8288 # 设置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 ))
8692
8793 # 图形显示
8894 plt .show ()
@@ -105,6 +111,7 @@ def subplot_plot():
105111
106112 # 子图的生成方式
107113 plt .subplot (2 , 2 , num + 1 )
114+ plt .title ("subplot %d" % (num + 1 ))
108115 plt .plot (x , y , style_list [num ])
109116
110117 # 图形显示
@@ -122,6 +129,9 @@ def bar_plot():
122129 means_men = (20 , 35 , 30 , 35 , 27 )
123130 means_women = (25 , 32 , 34 , 20 , 25 )
124131
132+ # 设置标题
133+ plt .title ("plot title" )
134+
125135 # 设置相关参数
126136 index = np .arange (len (means_men ))
127137 bar_width = 0.35
@@ -133,15 +143,15 @@ def bar_plot():
133143
134144 # 设置柱状图标示
135145 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" )
137147 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" )
139149
140150 # 设置刻度范围/坐标轴名称等
141151 plt .ylim (0 , 45 )
142152 plt .xlabel ("Group" )
143153 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 " ))
145155
146156 # 图形显示
147157 plt .show ()
@@ -157,6 +167,9 @@ def barh_plot():
157167 means_men = (20 , 35 , 30 , 35 , 27 )
158168 means_women = (25 , 32 , 34 , 20 , 25 )
159169
170+ # 设置标题
171+ plt .title ("plot title" )
172+
160173 # 设置相关参数
161174 index = np .arange (len (means_men ))
162175 bar_height = 0.35
@@ -168,15 +181,15 @@ def barh_plot():
168181
169182 # 设置柱状图标示
170183 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" )
172185 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" )
174187
175188 # 设置刻度范围/坐标轴名称等
176189 plt .xlim (0 , 45 )
177190 plt .xlabel ("Scores" )
178191 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 " ))
180193
181194 # 图形显示
182195 plt .show ()
@@ -192,6 +205,9 @@ def bar_advanced_plot():
192205 means_men = np .array ((20 , 35 , 30 , 35 , 27 , 25 , 32 , 34 , 20 , 25 ))
193206 means_women = np .array ((25 , 32 , 34 , 20 , 25 , 20 , 35 , 30 , 35 , 27 ))
194207
208+ # 设置标题
209+ plt .title ("plot title" )
210+
195211 # 设置相关参数
196212 index = np .arange (len (means_men ))
197213 bar_width = 0.8
@@ -201,14 +217,14 @@ def bar_advanced_plot():
201217 plt .bar (index , - means_women , width = bar_width , alpha = 0.4 , color = "r" , label = "Women" )
202218
203219 # 画折线图(两种,和柱状图对应)
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" )
206222
207223 # 设置图形标示(两种,和柱状图对应)
208224 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" )
210226 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" )
212228
213229 # 设置Y轴和图例位置
214230 plt .ylim (- 45 , 80 )
@@ -231,6 +247,9 @@ def table_plot():
231247 [5 , 3 , 6 , 4 , 1 ]
232248 ])
233249
250+ # 设置标题
251+ plt .title ("plot title" )
252+
234253 # 设置相关参数
235254 index = np .arange (len (data [0 ]))
236255 color_index = ["r" , "g" , "b" ]
@@ -240,7 +259,7 @@ def table_plot():
240259
241260 # 依次画图,并更新底部位置
242261 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 )
244263 bottom += data [i ]
245264
246265 # 设置图例位置
@@ -260,13 +279,13 @@ def histograms_plot():
260279 mu , sigma = 100 , 15
261280 x = mu + sigma * np .random .randn (10000 )
262281
263- # 设置相关参数
264- num_bins = 50
282+ # 设置标题
283+ plt . title ( "plot title" )
265284
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" )
268287
269- # 根据直方图返回的结果,画折线图
288+ # 根据直方图返回的结果, 画折线图
270289 y = mlab .normpdf (bins , mu , sigma )
271290 plt .plot (bins , y , "r--" , label = "line" )
272291
@@ -285,10 +304,15 @@ def pie_plot():
285304 """
286305 # 生成测试数据
287306 sizes = [15 , 30 , 45 , 10 ]
288- explode = [0 , 0.05 , 0 , 0 ]
289307 labels = ["Frogs" , "Hogs" , "Dogs" , "Logs" ]
290308 colors = ["yellowgreen" , "gold" , "lightskyblue" , "lightcoral" ]
291309
310+ # 设置标题
311+ plt .title ("plot title" )
312+
313+ # 设置突出参数
314+ explode = [0 , 0.05 , 0 , 0 ]
315+
292316 # 画饼状图
293317 plt .pie (sizes , explode = explode , labels = labels , colors = colors , autopct = "%1.1f%%" , shadow = True , startangle = 90 )
294318 plt .axis ("equal" )
@@ -308,6 +332,9 @@ def scatter_plot():
308332 x_index = np .random .random (point_count )
309333 y_index = np .random .random (point_count )
310334
335+ # 设置标题
336+ plt .title ("plot title" )
337+
311338 # 设置相关参数
312339 color_list = np .random .random (point_count )
313340 scale_list = np .random .random (point_count ) * 100
@@ -329,13 +356,15 @@ def fill_plot():
329356 x = np .linspace (- 2 * np .pi , 2 * np .pi , 1000 , endpoint = True )
330357 y = np .sin (x )
331358
359+ # 设置标题
360+ plt .title ("plot title" )
361+
332362 # 画图
333363 plt .plot (x , y , color = "blue" , alpha = 1.00 )
334364
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 )
339368
340369 # 图形显示
341370 plt .show ()
@@ -349,7 +378,7 @@ def radar_plot():
349378 """
350379 # 生成测试数据
351380 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 ])
353382 theta = np .linspace (0 , 2 * np .pi , len (data ), endpoint = False )
354383
355384 # 数据预处理
@@ -358,10 +387,11 @@ def radar_plot():
358387
359388 # 画图方式
360389 plt .subplot (111 , polar = True )
390+ plt .title ("plot title" )
361391
362392 # 设置"theta grid"/"radar grid"
363393 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 )
365395 plt .ylim (0 , 100 )
366396
367397 # 画雷达图,并填充雷达图内部区域
@@ -379,17 +409,16 @@ def three_dimension_scatter():
379409 3d scatter plot
380410 """
381411 # 生成测试数据
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
388417
389418 # 生成画布(两种形式)
390419 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" )
393422
394423 # 画三维散点图
395424 ax .scatter (x , y , z , s = scale , c = color , marker = "." )
@@ -415,15 +444,14 @@ def three_dimension_line():
415444 3d line plot
416445 """
417446 # 生成测试数据
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 )
421449 z = np .sin (x * 2 * np .pi ) / (y + 0.1 )
422450
423451 # 生成画布(两种形式)
424452 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" )
427455
428456 # 画三维折线图
429457 ax .plot (x , y , z , color = "red" , linestyle = "-" )
@@ -455,8 +483,7 @@ def three_dimension_bar():
455483
456484 # 生成画布(两种形式)
457485 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" )
460487
461488 # 画三维柱状图
462489 ax .bar3d (xpos , ypos , zpos , dx , dy , dz , alpha = 0.5 )
0 commit comments