2
2
.. _visualization :
3
3
4
4
.. ipython :: python
5
- :flake8- add- ignore: E702, E703
5
+ :flake8- add- ignore: E702, # multiple statements on one line. Needed for @savefig
6
6
:suppress:
7
7
8
8
import numpy as np
9
9
import pandas as pd
10
10
11
- import matplotlib # noqa: F401
12
- # matplotlib.style.use('default')
13
- import matplotlib.pyplot as plt
14
-
15
11
np.random.seed(123456 )
16
12
np.set_printoptions(precision = 4 , suppress = True )
17
13
pd.options.display.max_rows = 15
18
- plt.close(' all' )
19
14
20
15
21
16
*************
@@ -25,11 +20,14 @@ Visualization
25
20
We use the standard convention for referencing the matplotlib API:
26
21
27
22
.. ipython :: python
28
- :flake8- group: None
29
- :flake8- set - ignore: F401
30
23
31
24
import matplotlib.pyplot as plt
32
25
26
+ .. ipython :: python
27
+ :suppress:
28
+
29
+ plt.close(' all' )
30
+
33
31
We provide the basics in pandas to easily create decent looking plots.
34
32
See the :ref: `ecosystem <ecosystem.visualization >` section for visualization
35
33
libraries that go beyond the basics documented here.
@@ -81,7 +79,8 @@ On DataFrame, :meth:`~DataFrame.plot` is a convenience to plot all of the column
81
79
df = df.cumsum()
82
80
83
81
@savefig frame_plot_basic.png
84
- plt.figure(); df.plot();
82
+ plt.figure()
83
+ df.plot()
85
84
86
85
You can plot one column versus another using the `x ` and `y ` keywords in
87
86
:meth: `~DataFrame.plot `:
@@ -133,21 +132,22 @@ For example, a bar plot can be created the following way:
133
132
134
133
.. ipython :: python
135
134
136
- plt.figure();
135
+ plt.figure()
137
136
138
137
@savefig bar_plot_ex.png
139
- df.iloc[5 ].plot(kind = ' bar' );
138
+ df.iloc[5 ].plot(kind = ' bar' )
140
139
141
140
You can also create these other plots using the methods ``DataFrame.plot.<kind> `` instead of providing the ``kind `` keyword argument. This makes it easier to discover plot methods and the specific arguments they use:
142
141
143
142
.. ipython ::
144
143
:flake8-group: None
145
- :flake8-add-ignore: E999, E225, F821
144
+ :flake8-add-ignore: E999, E225, F821, # E999 breaks linting for complete block
146
145
:verbatim:
147
146
148
- In [14 ]: df = pd.DataFrame()
147
+ In [0 ]: df = pd.DataFrame()
149
148
150
- In [15]: df.plot.<TAB>
149
+ In [1]: df.plot.<TAB>
150
+ Out[1]:
151
151
df.plot.area df.plot.barh df.plot.density df.plot.hist df.plot.line df.plot.scatter
152
152
df.plot.bar df.plot.box df.plot.hexbin df.plot.kde df.plot.pie
153
153
@@ -178,7 +178,7 @@ For labeled, non-time series data, you may wish to produce a bar plot:
178
178
179
179
.. ipython :: python
180
180
181
- plt.figure();
181
+ plt.figure()
182
182
183
183
@savefig bar_plot_ex.png
184
184
df.iloc[5 ].plot.bar(); plt.axhline(0 , color = ' k' )
@@ -198,7 +198,7 @@ bar plot:
198
198
df2 = pd.DataFrame(np.random.rand(10 , 4 ), columns = [' a' , ' b' , ' c' , ' d' ])
199
199
200
200
@savefig bar_plot_multi_ex.png
201
- df2.plot.bar();
201
+ df2.plot.bar()
202
202
203
203
To produce a stacked bar plot, pass ``stacked=True ``:
204
204
@@ -211,7 +211,7 @@ To produce a stacked bar plot, pass ``stacked=True``:
211
211
.. ipython :: python
212
212
213
213
@savefig bar_plot_stacked_ex.png
214
- df2.plot.bar(stacked = True );
214
+ df2.plot.bar(stacked = True )
215
215
216
216
To get horizontal bar plots, use the ``barh `` method:
217
217
@@ -224,7 +224,7 @@ To get horizontal bar plots, use the ``barh`` method:
224
224
.. ipython :: python
225
225
226
226
@savefig barh_plot_stacked_ex.png
227
- df2.plot.barh(stacked = True );
227
+ df2.plot.barh(stacked = True )
228
228
229
229
.. _visualization.hist :
230
230
@@ -238,7 +238,7 @@ Histograms can be drawn by using the :meth:`DataFrame.plot.hist` and :meth:`Seri
238
238
df4 = pd.DataFrame({' a' : np.random.randn(1000 ) + 1 , ' b' : np.random.randn(1000 ),
239
239
' c' : np.random.randn(1000 ) - 1 }, columns = [' a' , ' b' , ' c' ])
240
240
241
- plt.figure();
241
+ plt.figure()
242
242
243
243
@savefig hist_new.png
244
244
df4.plot.hist(alpha = 0.5 )
@@ -254,7 +254,7 @@ using the ``bins`` keyword.
254
254
255
255
.. ipython :: python
256
256
257
- plt.figure();
257
+ plt.figure()
258
258
259
259
@savefig hist_new_stacked.png
260
260
df4.plot.hist(stacked = True , bins = 20 )
@@ -270,7 +270,7 @@ horizontal and cumulative histograms can be drawn by
270
270
271
271
.. ipython :: python
272
272
273
- plt.figure();
273
+ plt.figure()
274
274
275
275
@savefig hist_new_kwargs.png
276
276
df4[' a' ].plot.hist(orientation = ' horizontal' , cumulative = True )
@@ -288,7 +288,7 @@ The existing interface ``DataFrame.hist`` to plot histogram still can be used.
288
288
289
289
.. ipython :: python
290
290
291
- plt.figure();
291
+ plt.figure()
292
292
293
293
@savefig hist_plot_ex.png
294
294
df[' A' ].diff().hist()
@@ -402,7 +402,7 @@ The existing interface ``DataFrame.boxplot`` to plot boxplot still can be used.
402
402
:okwarning:
403
403
404
404
df = pd.DataFrame(np.random.rand(10 , 5 ))
405
- plt.figure();
405
+ plt.figure()
406
406
407
407
@savefig box_plot_ex.png
408
408
bp = df.boxplot()
@@ -422,7 +422,7 @@ groupings. For instance,
422
422
df = pd.DataFrame(np.random.rand(10 , 2 ), columns = [' Col1' , ' Col2' ])
423
423
df[' X' ] = pd.Series([' A' , ' A' , ' A' , ' A' , ' A' , ' B' , ' B' , ' B' , ' B' , ' B' ])
424
424
425
- plt.figure();
425
+ plt.figure()
426
426
427
427
@savefig box_plot_ex2.png
428
428
bp = df.boxplot(by = ' X' )
@@ -443,7 +443,7 @@ columns:
443
443
df[' X' ] = pd.Series([' A' , ' A' , ' A' , ' A' , ' A' , ' B' , ' B' , ' B' , ' B' , ' B' ])
444
444
df[' Y' ] = pd.Series([' A' , ' B' , ' A' , ' B' , ' A' , ' B' , ' A' , ' B' , ' A' , ' B' ])
445
445
446
- plt.figure();
446
+ plt.figure()
447
447
448
448
@savefig box_plot_ex3.png
449
449
bp = df.boxplot(column = [' Col1' , ' Col2' ], by = [' X' , ' Y' ])
@@ -531,7 +531,7 @@ When input data contains `NaN`, it will be automatically filled by 0. If you wan
531
531
df = pd.DataFrame(np.random.rand(10 , 4 ), columns = [' a' , ' b' , ' c' , ' d' ])
532
532
533
533
@savefig area_plot_stacked.png
534
- df.plot.area();
534
+ df.plot.area()
535
535
536
536
To produce an unstacked plot, pass ``stacked=False ``. Alpha value is set to 0.5 unless otherwise specified:
537
537
@@ -544,7 +544,7 @@ To produce an unstacked plot, pass ``stacked=False``. Alpha value is set to 0.5
544
544
.. ipython :: python
545
545
546
546
@savefig area_plot_unstacked.png
547
- df.plot.area(stacked = False );
547
+ df.plot.area(stacked = False )
548
548
549
549
.. _visualization.scatter :
550
550
@@ -567,16 +567,16 @@ These can be specified by the ``x`` and ``y`` keywords.
567
567
df = pd.DataFrame(np.random.rand(50 , 4 ), columns = [' a' , ' b' , ' c' , ' d' ])
568
568
569
569
@savefig scatter_plot.png
570
- df.plot.scatter(x = ' a' , y = ' b' );
570
+ df.plot.scatter(x = ' a' , y = ' b' )
571
571
572
572
To plot multiple column groups in a single axes, repeat ``plot `` method specifying target ``ax ``.
573
573
It is recommended to specify ``color `` and ``label `` keywords to distinguish each groups.
574
574
575
575
.. ipython :: python
576
576
577
- ax = df.plot.scatter(x = ' a' , y = ' b' , color = ' DarkBlue' , label = ' Group 1' );
577
+ ax = df.plot.scatter(x = ' a' , y = ' b' , color = ' DarkBlue' , label = ' Group 1' )
578
578
@savefig scatter_plot_repeated.png
579
- df.plot.scatter(x = ' c' , y = ' d' , color = ' DarkGreen' , label = ' Group 2' , ax = ax);
579
+ df.plot.scatter(x = ' c' , y = ' d' , color = ' DarkGreen' , label = ' Group 2' , ax = ax)
580
580
581
581
.. ipython :: python
582
582
:suppress:
@@ -589,7 +589,7 @@ each point:
589
589
.. ipython :: python
590
590
591
591
@savefig scatter_plot_colored.png
592
- df.plot.scatter(x = ' a' , y = ' b' , c = ' c' , s = 50 );
592
+ df.plot.scatter(x = ' a' , y = ' b' , c = ' c' , s = 50 )
593
593
594
594
595
595
.. ipython :: python
@@ -604,7 +604,7 @@ bubble chart using a column of the ``DataFrame`` as the bubble size.
604
604
.. ipython :: python
605
605
606
606
@savefig scatter_plot_bubble.png
607
- df.plot.scatter(x = ' a' , y = ' b' , s = df[' c' ] * 200 );
607
+ df.plot.scatter(x = ' a' , y = ' b' , s = df[' c' ] * 200 )
608
608
609
609
.. ipython :: python
610
610
:suppress:
@@ -1091,7 +1091,7 @@ layout and formatting of the returned plot:
1091
1091
.. ipython :: python
1092
1092
1093
1093
@savefig series_plot_basic2.png
1094
- plt.figure(); ts.plot(style = ' k--' , label = ' Series' );
1094
+ plt.figure(); ts.plot(style = ' k--' , label = ' Series' )
1095
1095
1096
1096
.. ipython :: python
1097
1097
:suppress:
@@ -1286,7 +1286,7 @@ with the ``subplots`` keyword:
1286
1286
.. ipython :: python
1287
1287
1288
1288
@savefig frame_plot_subplots.png
1289
- df.plot(subplots = True , figsize = (6 , 6 ));
1289
+ df.plot(subplots = True , figsize = (6 , 6 ))
1290
1290
1291
1291
.. ipython :: python
1292
1292
:suppress:
@@ -1309,7 +1309,7 @@ or columns needed, given the other.
1309
1309
.. ipython :: python
1310
1310
1311
1311
@savefig frame_plot_subplots_layout.png
1312
- df.plot(subplots = True , layout = (2 , 3 ), figsize = (6 , 6 ), sharex = False );
1312
+ df.plot(subplots = True , layout = (2 , 3 ), figsize = (6 , 6 ), sharex = False )
1313
1313
1314
1314
.. ipython :: python
1315
1315
:suppress:
@@ -1320,7 +1320,7 @@ The above example is identical to using:
1320
1320
1321
1321
.. ipython :: python
1322
1322
1323
- df.plot(subplots = True , layout = (2 , - 1 ), figsize = (6 , 6 ), sharex = False );
1323
+ df.plot(subplots = True , layout = (2 , - 1 ), figsize = (6 , 6 ), sharex = False )
1324
1324
1325
1325
.. ipython :: python
1326
1326
:suppress:
@@ -1340,15 +1340,15 @@ otherwise you will see a warning.
1340
1340
1341
1341
.. ipython :: python
1342
1342
1343
- fig, axes = plt.subplots(4 , 4 , figsize = (6 , 6 ));
1344
- plt.subplots_adjust(wspace = 0.5 , hspace = 0.5 );
1343
+ fig, axes = plt.subplots(4 , 4 , figsize = (6 , 6 ))
1344
+ plt.subplots_adjust(wspace = 0.5 , hspace = 0.5 )
1345
1345
target1 = [axes[0 ][0 ], axes[1 ][1 ], axes[2 ][2 ], axes[3 ][3 ]]
1346
1346
target2 = [axes[3 ][0 ], axes[2 ][1 ], axes[1 ][2 ], axes[0 ][3 ]]
1347
1347
1348
- df.plot(subplots = True , ax = target1, legend = False , sharex = False , sharey = False );
1348
+ df.plot(subplots = True , ax = target1, legend = False , sharex = False , sharey = False )
1349
1349
@savefig frame_plot_subplots_multi_ax.png
1350
1350
(- df).plot(subplots = True , ax = target2, legend = False ,
1351
- sharex = False , sharey = False );
1351
+ sharex = False , sharey = False )
1352
1352
1353
1353
.. ipython :: python
1354
1354
:suppress:
@@ -1377,12 +1377,12 @@ Another option is passing an ``ax`` argument to :meth:`Series.plot` to plot on a
1377
1377
.. ipython :: python
1378
1378
1379
1379
fig, axes = plt.subplots(nrows = 2 , ncols = 2 )
1380
- df[' A' ].plot(ax = axes[0 , 0 ]); axes[0 , 0 ].set_title(' A' );
1381
- df[' B' ].plot(ax = axes[0 , 1 ]); axes[0 , 1 ].set_title(' B' );
1382
- df[' C' ].plot(ax = axes[1 , 0 ]); axes[1 , 0 ].set_title(' C' );
1380
+ df[' A' ].plot(ax = axes[0 , 0 ]); axes[0 , 0 ].set_title(' A' )
1381
+ df[' B' ].plot(ax = axes[0 , 1 ]); axes[0 , 1 ].set_title(' B' )
1382
+ df[' C' ].plot(ax = axes[1 , 0 ]); axes[1 , 0 ].set_title(' C' )
1383
1383
1384
1384
@savefig series_plot_multi.png
1385
- df[' D' ].plot(ax = axes[1 , 1 ]); axes[1 , 1 ].set_title(' D' );
1385
+ df[' D' ].plot(ax = axes[1 , 1 ]); axes[1 , 1 ].set_title(' D' )
1386
1386
1387
1387
.. ipython :: python
1388
1388
:suppress:
0 commit comments