6
6
7
7
import numpy as np
8
8
import pandas as pd
9
+
9
10
np.random.seed(123456 )
10
11
np.set_printoptions(precision = 4 , suppress = True )
11
12
pd.options.display.max_rows = 15
12
- import matplotlib
13
- # matplotlib.style.use('default')
14
- import matplotlib.pyplot as plt
15
- plt.close(' all' )
13
+
16
14
17
15
*************
18
16
Visualization
@@ -50,7 +48,8 @@ The ``plot`` method on Series and DataFrame is just a simple wrapper around
50
48
51
49
.. ipython :: python
52
50
53
- ts = pd.Series(np.random.randn(1000 ), index = pd.date_range(' 1/1/2000' , periods = 1000 ))
51
+ ts = pd.Series(np.random.randn(1000 ),
52
+ index = pd.date_range(' 1/1/2000' , periods = 1000 ))
54
53
ts = ts.cumsum()
55
54
56
55
@savefig series_plot_basic.png
@@ -69,11 +68,13 @@ On DataFrame, :meth:`~DataFrame.plot` is a convenience to plot all of the column
69
68
70
69
.. ipython :: python
71
70
72
- df = pd.DataFrame(np.random.randn(1000 , 4 ), index = ts.index, columns = list (' ABCD' ))
71
+ df = pd.DataFrame(np.random.randn(1000 , 4 ),
72
+ index = ts.index, columns = list (' ABCD' ))
73
73
df = df.cumsum()
74
74
75
+ plt.figure();
75
76
@savefig frame_plot_basic.png
76
- plt.figure(); df.plot();
77
+ df.plot();
77
78
78
79
You can plot one column versus another using the `x ` and `y ` keywords in
79
80
:meth: `~DataFrame.plot `:
@@ -355,8 +356,8 @@ more complicated colorization, you can get each drawn artists by passing
355
356
356
357
.. ipython :: python
357
358
358
- color = dict ( boxes = ' DarkGreen' , whiskers = ' DarkOrange' ,
359
- medians = ' DarkBlue' , caps = ' Gray' )
359
+ color = { ' boxes' : ' DarkGreen' , ' whiskers' : ' DarkOrange' ,
360
+ ' medians' : ' DarkBlue' , ' caps' : ' Gray' }
360
361
361
362
@savefig box_new_colorize.png
362
363
df.plot.box(color = color, sym = ' r+' )
@@ -391,7 +392,7 @@ The existing interface ``DataFrame.boxplot`` to plot boxplot still can be used.
391
392
.. ipython :: python
392
393
:okwarning:
393
394
394
- df = pd.DataFrame(np.random.rand(10 ,5 ))
395
+ df = pd.DataFrame(np.random.rand(10 , 5 ))
395
396
plt.figure();
396
397
397
398
@savefig box_plot_ex.png
@@ -409,8 +410,8 @@ groupings. For instance,
409
410
.. ipython :: python
410
411
:okwarning:
411
412
412
- df = pd.DataFrame(np.random.rand(10 ,2 ), columns = [' Col1' , ' Col2' ] )
413
- df[' X' ] = pd.Series([' A' ,' A' ,' A' ,' A' ,' A' ,' B' ,' B' ,' B' ,' B' ,' B' ])
413
+ df = pd.DataFrame(np.random.rand(10 , 2 ), columns = [' Col1' , ' Col2' ])
414
+ df[' X' ] = pd.Series([' A' , ' A' , ' A' , ' A' , ' A' , ' B' , ' B' , ' B' , ' B' , ' B' ])
414
415
415
416
plt.figure();
416
417
@@ -429,14 +430,14 @@ columns:
429
430
.. ipython :: python
430
431
:okwarning:
431
432
432
- df = pd.DataFrame(np.random.rand(10 ,3 ), columns = [' Col1' , ' Col2' , ' Col3' ])
433
- df[' X' ] = pd.Series([' A' ,' A' ,' A' ,' A' ,' A' ,' B' ,' B' ,' B' ,' B' ,' B' ])
434
- df[' Y' ] = pd.Series([' A' ,' B' ,' A' ,' B' ,' A' ,' B' ,' A' ,' B' ,' A' ,' B' ])
433
+ df = pd.DataFrame(np.random.rand(10 , 3 ), columns = [' Col1' , ' Col2' , ' Col3' ])
434
+ df[' X' ] = pd.Series([' A' , ' A' , ' A' , ' A' , ' A' , ' B' , ' B' , ' B' , ' B' , ' B' ])
435
+ df[' Y' ] = pd.Series([' A' , ' B' , ' A' , ' B' , ' A' , ' B' , ' A' , ' B' , ' A' , ' B' ])
435
436
436
437
plt.figure();
437
438
438
439
@savefig box_plot_ex3.png
439
- bp = df.boxplot(column = [' Col1' ,' Col2' ], by = [' X' ,' Y' ])
440
+ bp = df.boxplot(column = [' Col1' , ' Col2' ], by = [' X' , ' Y' ])
440
441
441
442
.. ipython :: python
442
443
:suppress:
@@ -594,7 +595,7 @@ bubble chart using a column of the ``DataFrame`` as the bubble size.
594
595
.. ipython :: python
595
596
596
597
@savefig scatter_plot_bubble.png
597
- df.plot.scatter(x = ' a' , y = ' b' , s = df[' c' ]* 200 );
598
+ df.plot.scatter(x = ' a' , y = ' b' , s = df[' c' ] * 200 );
598
599
599
600
.. ipython :: python
600
601
:suppress:
@@ -654,8 +655,7 @@ given by column ``z``. The bins are aggregated with NumPy's ``max`` function.
654
655
df[' z' ] = np.random.uniform(0 , 3 , 1000 )
655
656
656
657
@savefig hexbin_plot_agg.png
657
- df.plot.hexbin(x = ' a' , y = ' b' , C = ' z' , reduce_C_function = np.max,
658
- gridsize = 25 )
658
+ df.plot.hexbin(x = ' a' , y = ' b' , C = ' z' , reduce_C_function = np.max, gridsize = 25 )
659
659
660
660
.. ipython :: python
661
661
:suppress:
@@ -682,7 +682,8 @@ A ``ValueError`` will be raised if there are any negative values in your data.
682
682
683
683
.. ipython :: python
684
684
685
- series = pd.Series(3 * np.random.rand(4 ), index = [' a' , ' b' , ' c' , ' d' ], name = ' series' )
685
+ series = pd.Series(3 * np.random.rand(4 ),
686
+ index = [' a' , ' b' , ' c' , ' d' ], name = ' series' )
686
687
687
688
@savefig series_pie_plot.png
688
689
series.plot.pie(figsize = (6 , 6 ))
@@ -711,7 +712,8 @@ drawn in each pie plots by default; specify ``legend=False`` to hide it.
711
712
712
713
.. ipython :: python
713
714
714
- df = pd.DataFrame(3 * np.random.rand(4 , 2 ), index = [' a' , ' b' , ' c' , ' d' ], columns = [' x' , ' y' ])
715
+ df = pd.DataFrame(3 * np.random.rand(4 , 2 ),
716
+ index = [' a' , ' b' , ' c' , ' d' ], columns = [' x' , ' y' ])
715
717
716
718
@savefig df_pie_plot.png
717
719
df.plot.pie(subplots = True , figsize = (8 , 4 ))
@@ -939,8 +941,8 @@ be passed, and when ``lag=1`` the plot is essentially ``data[:-1]`` vs.
939
941
940
942
plt.figure()
941
943
942
- data = pd.Series( 0.1 * np.random.rand( 1000 ) +
943
- 0.9 * np.sin(np.linspace( - 99 * np.pi, 99 * np.pi, num = 1000 ) ))
944
+ spacing = np.linspace( - 99 * np.pi, 99 * np.pi, num = 1000 )
945
+ data = pd.Series( 0.1 * np.random.rand( 1000 ) + 0.9 * np.sin(spacing ))
944
946
945
947
@savefig lag_plot.png
946
948
lag_plot(data)
@@ -976,8 +978,8 @@ autocorrelation plots.
976
978
977
979
plt.figure()
978
980
979
- data = pd.Series( 0.7 * np.random.rand( 1000 ) +
980
- 0.3 * np.sin(np.linspace( - 9 * np.pi, 9 * np.pi, num = 1000 ) ))
981
+ spacing = np.linspace( - 9 * np.pi, 9 * np.pi, num = 1000 )
982
+ data = pd.Series( 0.7 * np.random.rand( 1000 ) + 0.3 * np.sin(spacing ))
981
983
982
984
@savefig autocorrelation_plot.png
983
985
autocorrelation_plot(data)
@@ -1078,8 +1080,9 @@ layout and formatting of the returned plot:
1078
1080
1079
1081
.. ipython :: python
1080
1082
1083
+ plt.figure();
1081
1084
@savefig series_plot_basic2.png
1082
- plt.figure(); ts.plot(style = ' k--' , label = ' Series' );
1085
+ ts.plot(style = ' k--' , label = ' Series' );
1083
1086
1084
1087
.. ipython :: python
1085
1088
:suppress:
@@ -1106,7 +1109,8 @@ shown by default.
1106
1109
1107
1110
.. ipython :: python
1108
1111
1109
- df = pd.DataFrame(np.random.randn(1000 , 4 ), index = ts.index, columns = list (' ABCD' ))
1112
+ df = pd.DataFrame(np.random.randn(1000 , 4 ),
1113
+ index = ts.index, columns = list (' ABCD' ))
1110
1114
df = df.cumsum()
1111
1115
1112
1116
@savefig frame_plot_basic_noleg.png
@@ -1130,7 +1134,8 @@ You may pass ``logy`` to get a log-scale Y axis.
1130
1134
1131
1135
.. ipython :: python
1132
1136
1133
- ts = pd.Series(np.random.randn(1000 ), index = pd.date_range(' 1/1/2000' , periods = 1000 ))
1137
+ ts = pd.Series(np.random.randn(1000 ),
1138
+ index = pd.date_range(' 1/1/2000' , periods = 1000 ))
1134
1139
ts = np.exp(ts.cumsum())
1135
1140
1136
1141
@savefig series_plot_logy.png
@@ -1326,14 +1331,15 @@ otherwise you will see a warning.
1326
1331
1327
1332
.. ipython :: python
1328
1333
1329
- fig, axes = plt.subplots(4 , 4 , figsize = (6 , 6 ));
1330
- plt.subplots_adjust(wspace = 0.5 , hspace = 0.5 );
1334
+ fig, axes = plt.subplots(4 , 4 , figsize = (6 , 6 ))
1335
+ plt.subplots_adjust(wspace = 0.5 , hspace = 0.5 )
1331
1336
target1 = [axes[0 ][0 ], axes[1 ][1 ], axes[2 ][2 ], axes[3 ][3 ]]
1332
1337
target2 = [axes[3 ][0 ], axes[2 ][1 ], axes[1 ][2 ], axes[0 ][3 ]]
1333
1338
1334
1339
df.plot(subplots = True , ax = target1, legend = False , sharex = False , sharey = False );
1335
1340
@savefig frame_plot_subplots_multi_ax.png
1336
- (- df).plot(subplots = True , ax = target2, legend = False , sharex = False , sharey = False );
1341
+ (- df).plot(subplots = True , ax = target2, legend = False ,
1342
+ sharex = False , sharey = False );
1337
1343
1338
1344
.. ipython :: python
1339
1345
:suppress:
@@ -1346,10 +1352,12 @@ Another option is passing an ``ax`` argument to :meth:`Series.plot` to plot on a
1346
1352
:suppress:
1347
1353
1348
1354
np.random.seed(123456 )
1349
- ts = pd.Series(np.random.randn(1000 ), index = pd.date_range(' 1/1/2000' , periods = 1000 ))
1355
+ ts = pd.Series(np.random.randn(1000 ),
1356
+ index = pd.date_range(' 1/1/2000' , periods = 1000 ))
1350
1357
ts = ts.cumsum()
1351
1358
1352
- df = pd.DataFrame(np.random.randn(1000 , 4 ), index = ts.index, columns = list (' ABCD' ))
1359
+ df = pd.DataFrame(np.random.randn(1000 , 4 ), index = ts.index,
1360
+ columns = list (' ABCD' ))
1353
1361
df = df.cumsum()
1354
1362
1355
1363
.. ipython :: python
@@ -1360,12 +1368,15 @@ Another option is passing an ``ax`` argument to :meth:`Series.plot` to plot on a
1360
1368
.. ipython :: python
1361
1369
1362
1370
fig, axes = plt.subplots(nrows = 2 , ncols = 2 )
1363
- df[' A' ].plot(ax = axes[0 ,0 ]); axes[0 ,0 ].set_title(' A' );
1364
- df[' B' ].plot(ax = axes[0 ,1 ]); axes[0 ,1 ].set_title(' B' );
1365
- df[' C' ].plot(ax = axes[1 ,0 ]); axes[1 ,0 ].set_title(' C' );
1366
-
1371
+ df[' A' ].plot(ax = axes[0 , 0 ]);
1372
+ axes[0 , 0 ].set_title(' A' );
1373
+ df[' B' ].plot(ax = axes[0 , 1 ]);
1374
+ axes[0 , 1 ].set_title(' B' );
1375
+ df[' C' ].plot(ax = axes[1 , 0 ]);
1376
+ axes[1 , 0 ].set_title(' C' );
1377
+ df[' D' ].plot(ax = axes[1 , 1 ]);
1367
1378
@savefig series_plot_multi.png
1368
- df[ ' D ' ].plot( ax = axes[1 ,1 ]); axes[ 1 , 1 ].set_title(' D' );
1379
+ axes[1 , 1 ].set_title(' D' );
1369
1380
1370
1381
.. ipython :: python
1371
1382
:suppress:
@@ -1392,10 +1403,16 @@ Here is an example of one way to easily plot group means with standard deviation
1392
1403
.. ipython :: python
1393
1404
1394
1405
# Generate the data
1395
- ix3 = pd.MultiIndex.from_arrays([[' a' , ' a' , ' a' , ' a' , ' b' , ' b' , ' b' , ' b' ], [' foo' , ' foo' , ' bar' , ' bar' , ' foo' , ' foo' , ' bar' , ' bar' ]], names = [' letter' , ' word' ])
1396
- df3 = pd.DataFrame({' data1' : [3 , 2 , 4 , 3 , 2 , 4 , 3 , 2 ], ' data2' : [6 , 5 , 7 , 5 , 4 , 5 , 6 , 5 ]}, index = ix3)
1406
+ ix3 = pd.MultiIndex.from_arrays([
1407
+ [' a' , ' a' , ' a' , ' a' , ' b' , ' b' , ' b' , ' b' ],
1408
+ [' foo' , ' foo' , ' bar' , ' bar' , ' foo' , ' foo' , ' bar' , ' bar' ]],
1409
+ names = [' letter' , ' word' ])
1410
+
1411
+ df3 = pd.DataFrame({' data1' : [3 , 2 , 4 , 3 , 2 , 4 , 3 , 2 ],
1412
+ ' data2' : [6 , 5 , 7 , 5 , 4 , 5 , 6 , 5 ]}, index = ix3)
1397
1413
1398
- # Group by index labels and take the means and standard deviations for each group
1414
+ # Group by index labels and take the means and standard deviations
1415
+ # for each group
1399
1416
gp3 = df3.groupby(level = (' letter' , ' word' ))
1400
1417
means = gp3.mean()
1401
1418
errors = gp3.std()
@@ -1616,7 +1633,8 @@ when plotting a large number of points.
1616
1633
plt.plot(price.index, price, ' k' )
1617
1634
plt.plot(ma.index, ma, ' b' )
1618
1635
@savefig bollinger.png
1619
- plt.fill_between(mstd.index, ma- 2 * mstd, ma+ 2 * mstd, color = ' b' , alpha = 0.2 )
1636
+ plt.fill_between(mstd.index, ma - 2 * mstd, ma + 2 * mstd,
1637
+ color = ' b' , alpha = 0.2 )
1620
1638
1621
1639
.. ipython :: python
1622
1640
:suppress:
0 commit comments