Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
512 lines (422 loc) · 11.3 KB
/
Copy pathindex.html
File metadata and controls
512 lines (422 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" >
<title>Python-matplotlib | 书生海海</title>
<meta name="description" content="温故而知新">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.0/animate.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<link rel="shortcut icon" href="https://aprilspring.github.io/favicon.ico?v=1667029754307">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.10.0/katex.min.css">
<link rel="stylesheet" href="https://aprilspring.github.io/styles/main.css">
<link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css" />
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="//cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.5.1/build/highlight.min.js"></script>
</head>
<body>
<div id="app" class="main">
<div class="site-header-container">
<div class="site-header">
<div class="left">
<a href="https://aprilspring.github.io">
<img class="avatar" src="https://aprilspring.github.io/images/avatar.png?v=1667029754307" alt="" width="32px" height="32px">
</a>
<a href="https://aprilspring.github.io">
<h1 class="site-title">书生海海</h1>
</a>
</div>
<div class="right">
<transition name="fade">
<i class="icon" :class="{ 'icon-close-outline': menuVisible, 'icon-menu-outline': !menuVisible }" @click="menuVisible = !menuVisible"></i>
</transition>
</div>
</div>
</div>
<transition name="fade">
<div class="menu-container" style="display: none;" v-show="menuVisible">
<div class="menu-list">
<a href="/" class="menu purple-link">
首页
</a>
<a href="/archives" class="menu purple-link">
归档
</a>
<a href="/tags" class="menu purple-link">
标签
</a>
<a href="/post/about" class="menu purple-link">
关于
</a>
</div>
</div>
</transition>
<div class="content-container">
<div class="post-detail">
<h2 class="post-title">Python-matplotlib</h2>
<div class="post-info post-detail-info">
<span><i class="icon-calendar-outline"></i> 2017-07-27</span>
<span>
<i class="icon-pricetags-outline"></i>
<a href="https://aprilspring.github.io/tag/jVciP1z0t/">
Python
</a>
</span>
</div>
<div class="post-content" v-pre>
<h1 id="python的matplotlib模块">Python的Matplotlib模块</h1>
<p>Matplotlib是Python的一个绘图库,包含了大量创建图形的工具,例如散点图,三角函数曲线,三维图形等。</p>
<!-- more -->
<h2 id="引用模块方式">引用模块方式</h2>
<p>由于from x import * 是一种糟糕的导入方式,同时不想在程序里重复书写过于冗长的matplotlib.pyplot和numpy,因此我们采用下面的写法:</p>
<pre><code>import matplotlib.pyplot as plt
import numpy as np
</code></pre>
<h2 id="绘图三大系统">绘图三大系统</h2>
<ol>
<li>pyplot<br>
使用plt.style.use('ggplot')命令,可以作出ggplot风格的图片。</li>
<li>pylab(不推荐)</li>
<li>面向对象</li>
</ol>
<h2 id="绘图">绘图</h2>
<ol>
<li>散点图</li>
</ol>
<pre><code>x = rand(50,30)
plt.scatter(x[:,1],x[:,0],marker = 'x', color = 'm', label='1', s = 30)
plt.show()
# or
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
plt.plot(x,y,'o')
plt.show()
</code></pre>
<ol start="2">
<li>折线图</li>
</ol>
<pre><code>x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
plt.plot(x,y)
plt.show()
</code></pre>
<ol start="3">
<li>条形图</li>
</ol>
<pre><code>mean_values = [1, 2, 3]
variance = [0.2, 0.4, 0.5]
bar_labels = ['bar 1', 'bar 2', 'bar 3']
x_pos = list(range(len(bar_labels)))
plt.bar(x_pos, mean_values, yerr=variance, align='center', alpha=0.5)
plt.ylabel('variable y')
plt.xticks(x_pos, bar_labels)
plt.title('Bar plot with error bars')
plt.show()
#参见:http://blog.topspeedsnail.com/archives/724
</code></pre>
<ol start="4">
<li>
<p>直方图<br>
函数:histogram() 或 hist()</p>
</li>
<li>
<p>饼状图<br>
函数:pie()</p>
</li>
<li>
<p>箱形图<br>
函数:boxplot() 或 plot.box()</p>
</li>
</ol>
<h2 id="图形美化">图形美化</h2>
<ol>
<li>确定坐标范围</li>
</ol>
<pre><code>plt.axis([xmin, xmax, ymin, ymax]) #给定了坐标范围
xlim(xmin, xmax) #调整x坐标范围
ylim(ymin, ymax) #调整y坐标范围
</code></pre>
<ol start="2">
<li>添加文字说明<br>
在图中的任意位置添加文字:text() 支持LaTex语法<br>
添加x轴和y轴标签:xlable(), ylable()<br>
添加图的题目:title()</li>
</ol>
<pre><code>mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
#数据的直方图
n, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75)
plt.xlabel('Smarts')
plt.ylabel('Probability')
#添加标题
plt.title('Histogram of IQ')
#添加文字
plt.text(60, .025, r'$mu=100, sigma=15$')
plt.axis([40, 160, 0, 0.03])
plt.grid(True)
plt.show()
</code></pre>
<ol start="3">
<li>添加图片注释<br>
函数:annotate()</li>
</ol>
<pre><code>ax = plt.subplot(111)
t = np.arange(0.0, 5.0, 0.01)
s = np.cos(2*np.pi*t)
line = plt.plot(t, s, lw=2)
plt.annotate('local max', xy=(2,1), xytext=(3,1.5),
arrowprops=dict(facecolor='black', shrink=0.05),)
plt.ylim(-2,2)
plt.show()
</code></pre>
<ol start="4">
<li>
<p>设置坐标轴刻度<br>
函数:xticks()/ yticks(),为x,y轴的刻度设置颜色、大小、方向,以及标签大小。</p>
</li>
<li>
<p>设置坐标轴体系-Axes对象<br>
函数:axes(rect=[左,下,宽,高],axisbg='y')<br>
设置坐标轴边界和表面的颜色、坐标刻度值大小和网格的显示。</p>
</li>
<li>
<p>添加图例<br>
函数:legend()</p>
</li>
</ol>
<pre><code>plot(X, C, color="blue", linewidth=2.5, linestyle="-", label="cosine")
plot(X, S, color="red", linewidth=2.5, linestyle="-", label="sine")
legend(loc='upper left')
</code></pre>
<ol start="7">
<li>叠加图</li>
</ol>
<pre><code>t = np.arange(0., 5., 0.2)
#red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g<')
plt.show()
</code></pre>
<ol start="8">
<li>多张图<br>
创建新图:figure()<br>
创建子图:subplot()。其中,subplot(2,3,1)表示把图标分割成2*3的网格,也可以简写plt.subplot(231)。第一个参数是行数,第二个参数是列数,第三个参数表示图形的标号。</li>
</ol>
<pre><code>plt.figure(1) # 第一张图
plt.subplot(211) # 第一张图中的第一张子图
plt.plot([1,2,3])
plt.subplot(212) # 第一张图中的第二张子图
plt.plot([4,5,6])
plt.figure(2) # 第二张图
plt.plot([4,5,6]) # 默认创建子图subplot(111)
plt.figure(1) # 切换到figure(1)
plt.subplot(211) # 切换到figure(1)的子图subplot(211)
plt.title('Easy as 1,2,3') # 添加subplot(211)的标题
plt.show()
</code></pre>
<ol start="9">
<li>图形正常显示中文<br>
函数:rcParams()</li>
</ol>
<pre><code>plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
</code></pre>
<ol start="10">
<li>保存图片<br>
函数:savefig()</li>
</ol>
<pre><code>plt.plot([1,2,3],'r--')
plt.savefig('D:\\test.pdf',dpi=600, format="pdf") #png, pdf, ps, eps and svg
plt.show()
</code></pre>
<h2 id="图形标记">图形标记</h2>
<p>*1. 线条类型</p>
<table>
<thead>
<tr>
<th>标记</th>
<th>描述</th>
<th>标记</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>'-'</td>
<td>实线</td>
<td>':'</td>
<td>虚线</td>
</tr>
<tr>
<td>'--'</td>
<td>破折线</td>
<td>'-.'</td>
<td>点划线</td>
</tr>
<tr>
<td>'None' / ' ' / ''</td>
<td>什么都不画</td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<p>*2. 线条标记</p>
<table>
<thead>
<tr>
<th>标记</th>
<th>描述</th>
<th>标记</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>'o'</td>
<td>实心圆圈</td>
<td>'.'</td>
<td>点</td>
</tr>
<tr>
<td>'D'</td>
<td>实心菱形</td>
<td>'s'</td>
<td>实心正方形</td>
</tr>
<tr>
<td>'h'</td>
<td>竖六边形</td>
<td>'*'</td>
<td>星号</td>
</tr>
<tr>
<td>'H'</td>
<td>横六边形</td>
<td>'d'</td>
<td>瘦菱形</td>
</tr>
<tr>
<td>'_'</td>
<td>水平线</td>
<td>'+'</td>
<td>加号</td>
</tr>
<tr>
<td>'8'</td>
<td>实心八边形</td>
<td></td>
<td>竖线</td>
</tr>
<tr>
<td>'p'</td>
<td>实心五边形</td>
<td>','</td>
<td>像素标记(极小点)</td>
</tr>
<tr>
<td>'v'</td>
<td>下三角标记</td>
<td>'<'</td>
<td>左三角标记</td>
</tr>
<tr>
<td>'>'</td>
<td>右三角标记</td>
<td>'^'</td>
<td>上三角标记</td>
</tr>
<tr>
<td>'1'</td>
<td>下花三角标记</td>
<td>'2'</td>
<td>上花三角标记</td>
</tr>
<tr>
<td>'3'</td>
<td>左花三角标记</td>
<td>'4'</td>
<td>右花三角标记</td>
</tr>
</tbody>
</table>
<p>*3. 颜色标记</p>
<table>
<thead>
<tr>
<th>标记</th>
<th>描述</th>
<th>标记</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>'b'</td>
<td>蓝色</td>
<td>'g'</td>
<td>绿色</td>
</tr>
<tr>
<td>'r'</td>
<td>红色</td>
<td>'y'</td>
<td>黄色</td>
</tr>
<tr>
<td>'c'</td>
<td>青色</td>
<td>'k'</td>
<td>黑色</td>
</tr>
<tr>
<td>'m'</td>
<td>洋红色</td>
<td>'w'</td>
<td>白色</td>
</tr>
</tbody>
</table>
<h2 id="参考">参考</h2>
<p>https://matplotlib.org/contents.html</p>
</div>
</div>
<div class="next-post">
<a class="purple-link" href="https://aprilspring.github.io/post/python-numpy/">
<h3 class="post-title">
下一篇:Python-numpy
</h3>
</a>
</div>
</div>
<div id="gitalk-container"></div>
<div class="site-footer">
<div class="slogan">温故而知新</div>
<div class="social-container">
</div>
Powered by <a href="https://github.com/getgridea/gridea" target="_blank">Gridea</a> | <a class="rss" href="https://aprilspring.github.io/atom.xml" target="_blank">RSS</a>
</div>
</div>
<script type="application/javascript">
hljs.initHighlightingOnLoad()
var app = new Vue({
el: '#app',
data: {
menuVisible: false,
},
})
</script>
<script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>
<script>
var gitalk = new Gitalk({
clientID: '337e2992c146020878cb',
clientSecret: '883fa6db7cdfafda50356191ec9938e4d661aca1',
repo: 'AprilSpring.github.io',
owner: 'AprilSpring',
admin: ['AprilSpring'],
id: (location.pathname).substring(0, 49), // Ensure uniqueness and length less than 50
distractionFreeMode: false // Facebook-like distraction free mode
})
gitalk.render('gitalk-container')
</script>
</body>
</html>