-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy path03_WXN_matplotlib.Rmd
More file actions
1305 lines (982 loc) · 25.2 KB
/
03_WXN_matplotlib.Rmd
File metadata and controls
1305 lines (982 loc) · 25.2 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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Python作图 {#Python_plot_WXN}
## 绘图基础
### Figure和Subplot
```python
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from numpy.random import randn
```
```python
x = [1, 3, 5, 7, 9, 10, 23, 45, 45, 56]
y = [2, 4, 6, 8, 11, 12, 23, 45, 56, 78]
fig = plt.figure() # 创建一个Figure
ax1 = fig.add_subplot(2, 2, 1) # 创建4个图的Figure对象,最后的1为选中第一个
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)
ax4 = fig.add_subplot(2, 2, 4)
ax1.hist(x, y) # 在第一图中绘制直方图
ax2.scatter(x, y) # 散点图
ax4.plot(x, y) # 线图
plt.show()
```
```{r}
knitr::include_graphics("03_WXN_matplotlib_files/03_WXN_matplotlib_4_0.png")
```
```python
fig, axes = plt.subplots(2, 3) # 创建一个Figure 绘制2 X3 图
fig
```
```{r}
knitr::include_graphics("03_WXN_matplotlib_files/03_WXN_matplotlib_5_0.png")
```
```python
axes
```
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x7f8436a71780>,
<matplotlib.axes._subplots.AxesSubplot object at 0x7f842f2438d0>,
<matplotlib.axes._subplots.AxesSubplot object at 0x7f842f27d9e8>],
[<matplotlib.axes._subplots.AxesSubplot object at 0x7f842f236978>,
<matplotlib.axes._subplots.AxesSubplot object at 0x7f842f1f19b0>,
<matplotlib.axes._subplots.AxesSubplot object at 0x7f842f199710>]], dtype=object)
```
创建2 X 3图像,可以相当于对二维数组进行索引
参数 说明
nrows subplot行数
ncols subplot列数
sharex 所有图使用相同的x轴
sharey 所有图使用相同的y轴
subplot_kw 用于创建各subplot的关键字字典
```|
### 调整subplot周围间距
```
subplots_ajust(left=None,bottom=None,right=None,top=None,wspace=None,hspace=None)
wspace和hspace控制宽度和高度
```
```python
fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)
for i in range(2):
for j in range(2):
axes[i, j].hist(randn(500), bins=50, color='k', alpha=0.5)
plt.subplots_adjust(wspace=0, hspace=0)
plt.show()
```
```{r}
knitr::include_graphics("03_WXN_matplotlib_files/03_WXN_matplotlib_9_0.png")
```
```{r}
knitr::include_graphics("03_WXN_matplotlib_files/03_WXN_matplotlib_9_1.png")
```
```python
fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)
for i in range(2):
for j in range(2):
axes[i, j].hist(randn(500), bins=50, color='k', alpha=0.5)
plt.subplots_adjust(wspace=2, hspace=0.5)
plt.show()
```
```{r}
knitr::include_graphics("03_WXN_matplotlib_files/03_WXN_matplotlib_10_0.png")
```
```python
fig
```
```{r}
knitr::include_graphics("03_WXN_matplotlib_files/03_WXN_matplotlib_11_0.png")
```
### 颜色 标记和线型
```python
"""
绘制绿色虚线
ax.plot(x,y,'g--')
另一种方式
ax.plot(x,y,linestyle='--',color='g')
标记点(maker)
"""
fig, axes = plt.subplots(1, 2)
axes[0].plot(randn(10), 'g--') # green ---
axes[1].plot(randn(10), 'ko--') # k:black o:圆点
plt.show()
```
```{r}
knitr::include_graphics("03_WXN_matplotlib_files/03_WXN_matplotlib_13_0.png")
```
```
character description
'-' solid line style
'--' dashed line style
'-.' dash-dot line style
':' dotted line style
'.' point marker
',' pixel marker
'o' circle marker
'v' triangle_down marker
'^' triangle_up marker
'<' triangle_left marker
'>' triangle_right marker
'1' tri_down marker
'2' tri_up marker
'3' tri_left marker
'4' tri_right marker
's' square marker
'p' pentagon marker
'*' star marker
'h' hexagon1 marker
'H' hexagon2 marker
'+' plus marker
'x' x marker
'D' diamond marker
'd' thin_diamond marker
'|' vline marker
'_' hline marker
The following color abbreviations are supported:
character color
‘b’ blue
‘g’ green
‘r’ red
‘c’ cyan(青色)
‘m’ magenta(紫红色)
‘y’ yellow
‘k’ black
‘w’ white
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.plot
```
### 刻度、标签和图例
```python
fig = plt.figure() # 创建一个Figure
ax = fig.add_subplot(1, 1, 1)
ax.plot(randn(100))
plt.show()
```
```{r}
knitr::include_graphics("03_WXN_matplotlib_files/03_WXN_matplotlib_16_0.png")
```
```python
"""修改上图的轴"""
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(randn(100))
ticks = ax.set_xticks([0, 25, 50, 75, 100]) # 设置刻度
labels = ax.set_xticklabels(
['first', 'second', 'third', 'forth', 'fifth'], rotation=30, fontsize='small') # 设置x轴标签
ax.set_title('my first matplot plot') # 设置图片标题
ax.set_xlabel('Stages') # 设置x轴名称
plt.show()
```
```{r}
knitr::include_graphics("03_WXN_matplotlib_files/03_WXN_matplotlib_17_0.png")
```
### 添加图例legend
```
https://matplotlib.org/api/legend_api.html?highlight=legend#module-matplotlib.legend
‘best’ 0
‘upper right’ 1
‘upper left’ 2
‘lower left’ 3
‘lower right’ 4
‘right’ 5
‘center left’ 6
‘center right’ 7
‘lower center’ 8
‘upper center’ 9
‘center’ 10
bbox_to_anchor=(0.5,0.8)
bbox_to_anchor被赋予的元组中,第一个数值用于控制legend的左右移动,值越大越向右边移动,
第二个数值用于控制legend的上下移动,值越大,越向上移动
```
```python
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(randn(10), 'k', label='one') # 画一条线,k黑色
ax.plot(randn(10), 'g--', label='two') # 画第二条线,g绿色 - -类型
ax.plot(randn(10), 'ro--', label='three') # 画第三条线红色 ,类型 ...
ax.legend(loc=0, bbox_to_anchor=(1, 1))
plt.show()
```
```{r}
knitr::include_graphics("03_WXN_matplotlib_files/03_WXN_matplotlib_19_0.png")
```
### 注解
```python
x = [2, 4, 6, 8, 10, 12]
y = [1, 3, 5, 7, 9, 11]
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(x, y, 'r--')
ax.text(2, 4, 'hello python')
plt.show()
```
```{r}
knitr::include_graphics("03_WXN_matplotlib_files/03_WXN_matplotlib_21_0.png")
```
```python
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='blue') # xy=(要注释的点),xytext=(注释在图中的位置)
)
plt.ylim(-2, 2)
plt.show()
```
```{r}
knitr::include_graphics("03_WXN_matplotlib_files/03_WXN_matplotlib_22_0.png")
```
### 图片保存
```python
x = [2, 4, 6, 8, 10, 12]
y = [1, 3, 5, 7, 9, 11]
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(x, y, 'r--')
ax.text(2, 4, 'hello python')
# bbox_inches减除当前图片周围空白部分
plt.savefig('figpath.jpg', dpi=300, bbox_inches='tight')
```
## 绘图实例
### 绘制散点图
```python
x = [1, 2, 3, 4, 5, 6]
y = [1, 4, 9, 16, 25, 36]
plt.scatter(x, # x轴数据
y, # y轴数据
s=20, # 设置点的大小
c='green', # 设置点的颜色
marker='s', # 设置点的形状
alpha=0.9, # 设置点的透明度
linewidths=0.8, # 设置散点边界的粗细
edgecolors='red' # 设置散点边界的颜色
)
plt.title('simple scatter plot')
plt.xlabel('X') # x轴名称
plt.ylabel('Y')
plt.show() # 展示绘图
```
```{r}
knitr::include_graphics("03_WXN_matplotlib_files/03_WXN_matplotlib_27_0.png")
```
### 折线图
```python
x = [1, 2, 3, 4, 5, 6]
y = [1, 4, 9, 16, 25, 36]
plt.plot(x, # x轴数据
y, # y轴数据
linestyle='-', # 折线类型
linewidth=2, # 折线宽度
color='blue', # 折线颜色
marker='o', # 点的形状
markersize=8, # 点的大小
markeredgecolor='black', # 点的边框色
markerfacecolor='red') # 点的填充色
# 添加标题和坐标轴标签
plt.title('line plot')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
```
```{r}
knitr::include_graphics("03_WXN_matplotlib_files/03_WXN_matplotlib_29_0.png")
```
```python
import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0., 5., 0.2)
t
```
array([ 0. , 0.2, 0.4, 0.6, 0.8, 1. , 1.2, 1.4, 1.6, 1.8, 2. ,
2.2, 2.4, 2.6, 2.8, 3. , 3.2, 3.4, 3.6, 3.8, 4. , 4.2,
4.4, 4.6, 4.8])
```python
# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g')
plt.show()
```
```{r}
knitr::include_graphics("03_WXN_matplotlib_files/03_WXN_matplotlib_31_0.png")
```
```python
# 同一效果
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(t, t, 'r--')
ax.plot(t, t**2, 'bs') # 'bs'表示blue square marker
ax.plot(t, t**3, 'g')
fig
```
```{r}
knitr::include_graphics("03_WXN_matplotlib_files/03_WXN_matplotlib_32_0.png")
```
### 直方图
```python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.hist(np.random.randn(50), # 绘图数据
bins=50, # 指定直方图的条形数
color='red', # 指定填充色
edgecolor='k') # 指定直方图的边界色k :black
plt.show()
```
```{r}
knitr::include_graphics("03_WXN_matplotlib_files/03_WXN_matplotlib_34_0.png")
```
### 直条图
```python
x = [1, 2, 3, 4, 5, 6]
y = [1, 4, 9, 16, 25, 36]
plt.bar(x, y,
color='steelblue',
alpha=0.8)
plt.title('bar plot')
plt.ylim([0, 40])
plt.show()
```
```{r}
knitr::include_graphics("03_WXN_matplotlib_files/03_WXN_matplotlib_36_0.png")
```
```python
x = [1, 2, 3, 4, 5, 6]
y = [1, 4, 9, 16, 25, 36]
plt.barh(x, y,
color='steelblue',
alpha=0.8)
plt.title('bar plot')
plt.show()
```
```{r}
knitr::include_graphics("03_WXN_matplotlib_files/03_WXN_matplotlib_37_0.png")
```
### 箱线图
```python
x = [1, 2, 3, 4, 5, 6]
plt.boxplot(x,
patch_artist=True, # 箱体添加颜色
labels=['boxplot'], # 添加具体的标签名称
showmeans=True)
# 显示图形
plt.show()
```
```{r}
knitr::include_graphics("03_WXN_matplotlib_files/03_WXN_matplotlib_39_0.png")
```
```python
np.random.seed(2) # 设置随机种子
df = pd.DataFrame(np.random.rand(5, 4),
columns=(['A', 'B', 'C', 'D']))
df
```
<!--html_preserve-->
<div>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0.435995</td>
<td>0.025926</td>
<td>0.549662</td>
<td>0.435322</td>
</tr>
<tr>
<th>1</th>
<td>0.420368</td>
<td>0.330335</td>
<td>0.204649</td>
<td>0.619271</td>
</tr>
<tr>
<th>2</th>
<td>0.299655</td>
<td>0.266827</td>
<td>0.621134</td>
<td>0.529142</td>
</tr>
<tr>
<th>3</th>
<td>0.134580</td>
<td>0.513578</td>
<td>0.184440</td>
<td>0.785335</td>
</tr>
<tr>
<th>4</th>
<td>0.853975</td>
<td>0.494237</td>
<td>0.846561</td>
<td>0.079645</td>
</tr>
</tbody>
</table>
</div>
<!--/html_preserve-->
```python
data = []
for i in range(4):
data.append(df.iloc[:, i])
data
```
[0 0.435995
1 0.420368
2 0.299655
3 0.134580
4 0.853975
Name: A, dtype: float64, 0 0.025926
1 0.330335
2 0.266827
3 0.513578
4 0.494237
Name: B, dtype: float64, 0 0.549662
1 0.204649
2 0.621134
3 0.184440
4 0.846561
Name: C, dtype: float64, 0 0.435322
1 0.619271
2 0.529142
3 0.785335
4 0.079645
Name: D, dtype: float64]
```python
plt.boxplot(data)
plt.show()
```
```{r}
knitr::include_graphics("03_WXN_matplotlib_files/03_WXN_matplotlib_42_0.png")
```
```
plt.boxplot(x, notch=None, sym=None, vert=None,
whis=None, positions=None, widths=None,
patch_artist=None, meanline=None, showmeans=None,
showcaps=None, showbox=None, showfliers=None,
boxprops=None, labels=None, flierprops=None,
medianprops=None, meanprops=None,
capprops=None, whiskerprops=None)
x:指定要绘制箱线图的数据;
notch:是否凹口的形式展现箱线图,默认非凹口;
sym:指定异常点的形状,默认为+号显示;
vert:是否需要将箱线图垂直摆放,默认垂直摆放;
whis:指定上下须与上下四分位的距离,默认为1.5倍的四分位差;
positions:指定箱线图的位置,默认为[0,1,2…];
widths:指定箱线图的宽度,默认为0.5;
patch_artist:是否填充箱体的颜色;
meanline:是否用线的形式表示均值,默认用点来表示;
showmeans:是否显示均值,默认不显示;
showcaps:是否显示箱线图顶端和末端的两条线,默认显示;
showbox:是否显示箱线图的箱体,默认显示;
showfliers:是否显示异常值,默认显示;
boxprops:设置箱体的属性,如边框色,填充色等;
labels:为箱线图添加标签,类似于图例的作用;
filerprops:设置异常值的属性,如异常点的形状、大小、填充色等;
medianprops:设置中位数的属性,如线的类型、粗细等;
meanprops:设置均值的属性,如点的大小、颜色等;
capprops:设置箱线图顶端和末端线条的属性,如颜色、粗细等;
whiskerprops:设置须的属性,如颜色、粗细、线的类型等;
```
### 饼图
```python
data = [0.2, 0.3, 0.4, 0.1]
plt.pie(data)
plt.show()
```
```{r}
knitr::include_graphics("03_WXN_matplotlib_files/03_WXN_matplotlib_45_0.png")
```
```
plt.pie(x, explode=None, labels=None, colors=None,
autopct=None, pctdistance=0.6, shadow=False,
labeldistance=1.1, startangle=None,
radius=None, counterclock=True, wedgeprops=None,
textprops=None, center=(0, 0), frame=False)
x:指定绘图的数据;
explode:指定饼图某些部分的突出显示,即呈现爆炸式;
labels:为饼图添加标签说明,类似于图例说明;
colors:指定饼图的填充色;
autopct:自动添加百分比显示,可以采用格式化的方法显示;
pctdistance:设置百分比标签与圆心的距离;
shadow:是否添加饼图的阴影效果;
labeldistance:设置各扇形标签(图例)与圆心的距离;
startangle:设置饼图的初始摆放角度;
radius:设置饼图的半径大小;
counterclock:是否让饼图按逆时针顺序呈现;
wedgeprops:设置饼图内外边界的属性,如边界线的粗细、颜色等;
textprops:设置饼图中文本的属性,如字体大小、颜色等;
center:指定饼图的中心点位置,默认为原点
frame:是否要显示饼图背后的图框,如果设置为True的话,需要同时控制图框x轴、y轴的范围和饼图的中心位置;
```
### 绘制基因矩阵的热图
```python
# 来源于百度经验
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import cm
from matplotlib import axes
def draw_heatmap(data, xlabels, ylabels): # 自定义函数,3个参数
cmap = cm.Blues
figure = plt.figure(facecolor='w')
ax = figure.add_subplot(1, 1, 1, position=[0.1, 0.15, 0.8, 0.8])
ax.set_yticks(range(len(ylabels)))
ax.set_yticklabels(ylabels)
ax.set_xticks(range(len(xlabels)))
ax.set_xticklabels(xlabels)
vmax = data[0][0]
vmin = data[0][0]
for i in data:
for j in i:
if j > vmax:
vmax = j
if j < vmin:
vmin = j
map = ax.imshow(data, interpolation='nearest', cmap=cmap, aspect='auto',
vmin=vmin, vmax=vmax) # interpolation插值,cmap:colormap
cb = plt.colorbar(mappable=map, cax=None, ax=None, shrink=0.5) # 绘制颜色条
plt.show()
a = np.random.rand(10, 10)
xlabels = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
ylabels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
draw_heatmap(a, xlabels, ylabels)
```
```{r}
knitr::include_graphics("03_WXN_matplotlib_files/03_WXN_matplotlib_48_0.png")
```
```python
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5, 6))
df
```
<!--html_preserve-->
<div>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>-0.596967</td>
<td>0.080979</td>
<td>-1.890122</td>
<td>-0.629949</td>
<td>-1.187113</td>
<td>-0.002018</td>
</tr>
<tr>
<th>1</th>
<td>1.264042</td>
<td>0.044216</td>
<td>-1.669342</td>
<td>0.805476</td>
<td>-0.173634</td>
<td>-0.051918</td>
</tr>
<tr>
<th>2</th>
<td>0.793684</td>
<td>1.746997</td>
<td>0.785712</td>
<td>-0.157126</td>
<td>0.072688</td>
<td>0.310271</td>
</tr>
<tr>
<th>3</th>
<td>0.988716</td>
<td>0.404472</td>
<td>0.967111</td>
<td>-0.612160</td>
<td>-0.259917</td>
<td>-0.067299</td>
</tr>
<tr>
<th>4</th>
<td>0.033911</td>
<td>1.177149</td>
<td>-0.914153</td>
<td>-0.579912</td>
<td>-0.100110</td>
<td>0.747296</td>
</tr>
</tbody>
</table>
</div>
<!--/html_preserve-->
```python
plt.imshow(df)
plt.show()
```
```{r}
knitr::include_graphics("03_WXN_matplotlib_files/03_WXN_matplotlib_50_0.png")
```
读入基因表达数据,绘制某基因的箱线图
```python
%cd F: \mega\hello\python\python课程包\merge
import os
os.getcwd()
```
F:\mega\hello\python\python课程包\merge
'F:\\mega\\hello\\python\\python课程包\\merge'
```python
import pandas as pd
df = pd.read_table("hello.xls", sep="\t", index_col=0, header=0)
df.head(5)
```
<!--html_preserve-->
<div>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>MCF10F</th>
<th>MCF12A</th>
<th>T47DKBluc</th>
<th>UACC812</th>
<th>UACC893</th>
<th>ZR751</th>
<th>ZR7530</th>
<th>ZR75B</th>
</tr>
<tr>
<th>EnsEMBL_Gene_ID</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>ENSG00000000003</th>
<td>91.574061</td>
<td>13.040870</td>
<td>73.387310</td>
<td>26.579280</td>
<td>50.676579</td>
<td>31.812999</td>
<td>121.459918</td>
<td>2.293898</td>
</tr>
<tr>
<th>ENSG00000001167</th>
<td>164.221408</td>
<td>170.101403</td>
<td>106.114416</td>
<td>138.140311</td>
<td>106.342032</td>
<td>68.458022</td>
<td>73.910964</td>
<td>61.625519</td>
</tr>
<tr>
<th>ENSG00000005471</th>
<td>0.000000</td>
<td>0.073680</td>
<td>0.536226</td>
<td>0.440649</td>
<td>0.066213</td>
<td>0.548865</td>
<td>0.611036</td>
<td>0.556531</td>
</tr>
<tr>
<th>ENSG00000066629</th>
<td>24.630773</td>
<td>18.627459</td>