pandas的基本用法(八)——数据的绘制

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

本文主要是关于pandas的一些数据的绘制。

Demo 1

  • Code
1
2
3
4
5
6
7
8
9
10
11
12
13
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# 定义Series
data = pd.Series(np.random.randn(1000), index = np.arange(1000))
print data
# 累加数据
data = data.cumsum()
# 绘制数据
data.plot()
# 显示数据
plt.show()
  • Data
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
0      1.548346
1 0.572742
2 1.104705
3 1.548704
4 0.287301
5 -0.656314
6 -0.202801
7 -0.196791
8 0.096911
9 -0.113942
10 -0.883673
11 1.501955
12 -0.711122
13 0.142911
14 -0.941701
15 1.522272
16 0.610483
17 -0.219638
18 1.745611
19 0.031911
20 0.889913
21 0.771158
22 -0.586174
23 0.873769
24 -2.186166
25 1.643832
26 0.522218
27 -0.301171
28 -1.128542
29 0.085811
...
970 -0.823560
971 0.828570
972 0.344901
973 -1.700792
974 -0.458375
975 0.846068
976 1.054396
977 -0.338136
978 1.039985
979 0.132224
980 -0.152097
981 1.034157
982 -0.950993
983 1.934781
984 0.301666
985 -0.910372
986 0.606312
987 1.562350
988 0.979057
989 0.262618
990 0.105402
991 0.352259
992 0.462557
993 -0.686371
994 1.125795
995 -1.202305
996 -0.879454
997 0.479948
998 -0.058433
999 1.150558
dtype: float64
  • Image

Image

Demo 2

  • Code
1
2
3
4
5
6
7
8
9
10
# 定义DataFrame
data = pd.DataFrame(np.random.randn(1000, 4), index = np.arange(1000), columns = list('ABCD'))
print data
# 累加数据
data = data.cumsum()
# print data
# 绘制数据
data.plot()
# 显示数据
plt.show()
  • Data
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
            A         B         C         D
0 0.188169 -0.410177 -0.035167 -0.632530
1 1.902968 -0.253942 0.116262 -0.409900
2 -0.477557 -0.544720 0.475352 -0.763175
3 -0.131545 0.276950 -0.309663 -1.704675
4 -0.497051 -0.786458 0.142589 -1.658723
5 -1.219892 -2.844160 0.923590 1.463719
6 -0.729045 -1.040011 -0.453982 -0.589323
7 1.235946 -0.616109 -0.160319 -1.101710
8 0.064108 -0.880624 0.291627 -0.481524
9 1.178941 -0.812158 -0.440956 0.017456
10 0.246466 1.173672 -1.010398 0.493644
11 0.228121 -0.715523 0.287755 -0.227716
12 0.435218 -1.112818 1.938080 -0.348133
13 -1.154960 0.090186 -0.365532 -0.513318
14 1.061165 -0.040768 -0.994464 1.183172
15 0.138335 0.690717 0.485866 -0.014977
16 0.938048 0.251487 0.009421 -0.809593
17 -1.480628 -0.270541 0.882366 -1.808014
18 -1.122170 0.791330 -1.122514 -1.248467
19 0.736545 1.094979 -0.926841 -0.223580
20 0.439745 0.505928 -0.425728 0.306738
21 0.117386 -3.699946 0.050963 -1.166935
22 -1.433574 0.311665 2.226888 -1.139630
23 1.641118 -0.198970 -0.240798 0.720337
24 0.722513 1.714796 -0.542274 0.443971
25 0.154177 0.701450 0.832888 -1.898574
26 -0.713805 -1.184821 -0.531134 0.068217
27 0.694963 -0.318380 1.437368 0.213080
28 0.331043 1.892780 -0.256899 -1.189912
29 -0.247650 1.601953 -1.695998 -1.001989
.. ... ... ... ...
970 1.096683 0.796003 0.258615 -1.275517
971 -1.302741 1.864113 -0.753244 -0.035786
972 -0.259019 0.760312 -1.273606 0.896497
973 -0.060886 1.100344 2.051858 -0.898953
974 0.058918 0.123978 -0.534120 1.256028
975 -0.813877 -0.344310 -1.149161 0.768660
976 -0.234716 -1.039258 0.592899 0.662823
977 0.353870 -0.536609 -1.078631 1.716869
978 -2.455930 -0.022458 1.159104 1.597242
979 -1.318595 -0.716460 1.254460 -2.477972
980 -0.655070 -1.299694 0.442306 0.685829
981 -0.242390 0.495463 -0.746983 1.224797
982 -0.452496 -0.961725 -0.795946 1.296465
983 -0.118532 0.136311 -0.311137 -0.205128
984 -0.395279 0.646056 1.757899 0.089445
985 1.459979 0.024268 -0.294394 1.992585
986 0.915223 -0.313486 0.873132 -1.046711
987 -1.483945 0.520361 0.728229 1.279807
988 1.496952 0.793115 -0.717488 -0.367732
989 -0.913652 -1.891394 -0.692108 -0.478300
990 -1.384200 0.167642 0.077620 0.741487
991 -0.895972 -0.393196 -0.694417 -1.110403
992 1.045946 -0.618238 1.229456 0.467488
993 -0.199291 -0.199487 1.714675 0.371975
994 0.653998 0.548682 0.598073 -0.668729
995 -0.522661 1.547213 0.684786 0.991293
996 -0.682826 1.844690 -0.577090 0.440919
997 -0.935643 -0.264333 1.067578 0.677179
998 0.957670 -1.024795 0.607110 -0.475680
999 -0.854264 -0.680246 -0.166721 -0.394088

[1000 rows x 4 columns]
  • Image

Image

Demo 3

  • Code
1
2
3
4
# 绘制散点图
ax = data.plot.scatter(x = 'A', y = 'B', color = 'DarkBlue')
data.plot.scatter(x = 'A', y = 'C', color = 'DarkGreen', ax = ax)
plt.show()
  • Image

Image

参考资料

  1. https://www.youtube.com/user/MorvanZhou
如果有收获,可以请我喝杯咖啡!