8
8
9
9
from pandas ._config import get_option
10
10
11
-
12
11
from pandas .core .dtypes .common import (
13
12
is_integer ,
14
13
is_list_like ,
@@ -593,43 +592,38 @@ def boxplot(
593
592
:context: close-figs
594
593
595
594
>>> np.random.seed(1234)
596
- >>> df = pd.DataFrame(np.random.randn(10, 4),
597
- ... columns=['Col1', 'Col2', 'Col3', 'Col4'])
598
- >>> boxplot = df.boxplot(column=['Col1', 'Col2', 'Col3']) # doctest: +SKIP
595
+ >>> df = pd.DataFrame(
596
+ ... np.random.randn(10, 4), columns=["Col1", "Col2", "Col3", "Col4"]
597
+ ... )
598
+ >>> boxplot = df.boxplot(column=["Col1", "Col2", "Col3"]) # doctest: +SKIP
599
599
600
600
Boxplots of variables distributions grouped by the values of a third
601
601
variable can be created using the option ``by``. For instance:
602
602
603
603
.. plot::
604
604
:context: close-figs
605
605
606
- >>> df = pd.DataFrame(np.random.randn(10, 2),
607
- ... columns=['Col1', 'Col2'])
608
- >>> df['X'] = pd.Series(['A', 'A', 'A', 'A', 'A',
609
- ... 'B', 'B', 'B', 'B', 'B'])
610
- >>> boxplot = df.boxplot(by='X')
606
+ >>> df = pd.DataFrame(np.random.randn(10, 2), columns=["Col1", "Col2"])
607
+ >>> df["X"] = pd.Series(["A", "A", "A", "A", "A", "B", "B", "B", "B", "B"])
608
+ >>> boxplot = df.boxplot(by="X")
611
609
612
610
A list of strings (i.e. ``['X', 'Y']``) can be passed to boxplot
613
611
in order to group the data by combination of the variables in the x-axis:
614
612
615
613
.. plot::
616
614
:context: close-figs
617
615
618
- >>> df = pd.DataFrame(np.random.randn(10, 3),
619
- ... columns=['Col1', 'Col2', 'Col3'])
620
- >>> df['X'] = pd.Series(['A', 'A', 'A', 'A', 'A',
621
- ... 'B', 'B', 'B', 'B', 'B'])
622
- >>> df['Y'] = pd.Series(['A', 'B', 'A', 'B', 'A',
623
- ... 'B', 'A', 'B', 'A', 'B'])
624
- >>> boxplot = df.boxplot(column=['Col1', 'Col2'], by=['X', 'Y'])
616
+ >>> df = pd.DataFrame(np.random.randn(10, 3), columns=["Col1", "Col2", "Col3"])
617
+ >>> df["X"] = pd.Series(["A", "A", "A", "A", "A", "B", "B", "B", "B", "B"])
618
+ >>> df["Y"] = pd.Series(["A", "B", "A", "B", "A", "B", "A", "B", "A", "B"])
619
+ >>> boxplot = df.boxplot(column=["Col1", "Col2"], by=["X", "Y"])
625
620
626
621
The layout of boxplot can be adjusted giving a tuple to ``layout``:
627
622
628
623
.. plot::
629
624
:context: close-figs
630
625
631
- >>> boxplot = df.boxplot(column=['Col1', 'Col2'], by='X',
632
- ... layout=(2, 1))
626
+ >>> boxplot = df.boxplot(column=["Col1", "Col2"], by="X", layout=(2, 1))
633
627
634
628
Additional formatting can be done to the boxplot, like suppressing the grid
635
629
(``grid=False``), rotating the labels in the x-axis (i.e. ``rot=45``)
@@ -644,23 +638,21 @@ def boxplot(
644
638
returned by `boxplot`. When ``return_type='axes'`` is selected,
645
639
the matplotlib axes on which the boxplot is drawn are returned:
646
640
647
- >>> boxplot = df.boxplot(column=[' Col1', ' Col2' ], return_type=' axes' )
641
+ >>> boxplot = df.boxplot(column=[" Col1", " Col2" ], return_type=" axes" )
648
642
>>> type(boxplot)
649
643
<class 'matplotlib.axes._axes.Axes'>
650
644
651
645
When grouping with ``by``, a Series mapping columns to ``return_type``
652
646
is returned:
653
647
654
- >>> boxplot = df.boxplot(column=['Col1', 'Col2'], by='X',
655
- ... return_type='axes')
648
+ >>> boxplot = df.boxplot(column=["Col1", "Col2"], by="X", return_type="axes")
656
649
>>> type(boxplot)
657
650
<class 'pandas.Series'>
658
651
659
652
If ``return_type`` is `None`, a NumPy array of axes with the same shape
660
653
as ``layout`` is returned:
661
654
662
- >>> boxplot = df.boxplot(column=['Col1', 'Col2'], by='X',
663
- ... return_type=None)
655
+ >>> boxplot = df.boxplot(column=["Col1", "Col2"], by="X", return_type=None)
664
656
>>> type(boxplot)
665
657
<class 'numpy.ndarray'>
666
658
"""
@@ -791,43 +783,38 @@ def boxplot_frame(
791
783
:context: close-figs
792
784
793
785
>>> np.random.seed(1234)
794
- >>> df = pd.DataFrame(np.random.randn(10, 4),
795
- ... columns=['Col1', 'Col2', 'Col3', 'Col4'])
796
- >>> boxplot = df.boxplot(column=['Col1', 'Col2', 'Col3']) # doctest: +SKIP
786
+ >>> df = pd.DataFrame(
787
+ ... np.random.randn(10, 4), columns=["Col1", "Col2", "Col3", "Col4"]
788
+ ... )
789
+ >>> boxplot = df.boxplot(column=["Col1", "Col2", "Col3"]) # doctest: +SKIP
797
790
798
791
Boxplots of variables distributions grouped by the values of a third
799
792
variable can be created using the option ``by``. For instance:
800
793
801
794
.. plot::
802
795
:context: close-figs
803
796
804
- >>> df = pd.DataFrame(np.random.randn(10, 2),
805
- ... columns=['Col1', 'Col2'])
806
- >>> df['X'] = pd.Series(['A', 'A', 'A', 'A', 'A',
807
- ... 'B', 'B', 'B', 'B', 'B'])
808
- >>> boxplot = df.boxplot(by='X')
797
+ >>> df = pd.DataFrame(np.random.randn(10, 2), columns=["Col1", "Col2"])
798
+ >>> df["X"] = pd.Series(["A", "A", "A", "A", "A", "B", "B", "B", "B", "B"])
799
+ >>> boxplot = df.boxplot(by="X")
809
800
810
801
A list of strings (i.e. ``['X', 'Y']``) can be passed to boxplot
811
802
in order to group the data by combination of the variables in the x-axis:
812
803
813
804
.. plot::
814
805
:context: close-figs
815
806
816
- >>> df = pd.DataFrame(np.random.randn(10, 3),
817
- ... columns=['Col1', 'Col2', 'Col3'])
818
- >>> df['X'] = pd.Series(['A', 'A', 'A', 'A', 'A',
819
- ... 'B', 'B', 'B', 'B', 'B'])
820
- >>> df['Y'] = pd.Series(['A', 'B', 'A', 'B', 'A',
821
- ... 'B', 'A', 'B', 'A', 'B'])
822
- >>> boxplot = df.boxplot(column=['Col1', 'Col2'], by=['X', 'Y'])
807
+ >>> df = pd.DataFrame(np.random.randn(10, 3), columns=["Col1", "Col2", "Col3"])
808
+ >>> df["X"] = pd.Series(["A", "A", "A", "A", "A", "B", "B", "B", "B", "B"])
809
+ >>> df["Y"] = pd.Series(["A", "B", "A", "B", "A", "B", "A", "B", "A", "B"])
810
+ >>> boxplot = df.boxplot(column=["Col1", "Col2"], by=["X", "Y"])
823
811
824
812
The layout of boxplot can be adjusted giving a tuple to ``layout``:
825
813
826
814
.. plot::
827
815
:context: close-figs
828
816
829
- >>> boxplot = df.boxplot(column=['Col1', 'Col2'], by='X',
830
- ... layout=(2, 1))
817
+ >>> boxplot = df.boxplot(column=["Col1", "Col2"], by="X", layout=(2, 1))
831
818
832
819
Additional formatting can be done to the boxplot, like suppressing the grid
833
820
(``grid=False``), rotating the labels in the x-axis (i.e. ``rot=45``)
@@ -842,23 +829,21 @@ def boxplot_frame(
842
829
returned by `boxplot`. When ``return_type='axes'`` is selected,
843
830
the matplotlib axes on which the boxplot is drawn are returned:
844
831
845
- >>> boxplot = df.boxplot(column=[' Col1', ' Col2' ], return_type=' axes' )
832
+ >>> boxplot = df.boxplot(column=[" Col1", " Col2" ], return_type=" axes" )
846
833
>>> type(boxplot)
847
834
<class 'matplotlib.axes._axes.Axes'>
848
835
849
836
When grouping with ``by``, a Series mapping columns to ``return_type``
850
837
is returned:
851
838
852
- >>> boxplot = df.boxplot(column=['Col1', 'Col2'], by='X',
853
- ... return_type='axes')
839
+ >>> boxplot = df.boxplot(column=["Col1", "Col2"], by="X", return_type="axes")
854
840
>>> type(boxplot)
855
841
<class 'pandas.Series'>
856
842
857
843
If ``return_type`` is `None`, a NumPy array of axes with the same shape
858
844
as ``layout`` is returned:
859
845
860
- >>> boxplot = df.boxplot(column=['Col1', 'Col2'], by='X',
861
- ... return_type=None)
846
+ >>> boxplot = df.boxplot(column=["Col1", "Col2"], by="X", return_type=None)
862
847
>>> type(boxplot)
863
848
<class 'numpy.ndarray'>
864
849
"""
@@ -1468,10 +1453,13 @@ def line(
1468
1453
The following example shows the populations for some animals
1469
1454
over the years.
1470
1455
1471
- >>> df = pd.DataFrame({
1472
- ... 'pig': [20, 18, 489, 675, 1776],
1473
- ... 'horse': [4, 25, 281, 600, 1900]
1474
- ... }, index=[1990, 1997, 2003, 2009, 2014])
1456
+ >>> df = pd.DataFrame(
1457
+ ... {
1458
+ ... "pig": [20, 18, 489, 675, 1776],
1459
+ ... "horse": [4, 25, 281, 600, 1900],
1460
+ ... },
1461
+ ... index=[1990, 1997, 2003, 2009, 2014],
1462
+ ... )
1475
1463
>>> lines = df.plot.line()
1476
1464
1477
1465
.. plot::
@@ -1499,7 +1487,7 @@ def line(
1499
1487
The following example shows the relationship between both
1500
1488
populations.
1501
1489
1502
- >>> lines = df.plot.line(x=' pig' , y=' horse' )
1490
+ >>> lines = df.plot.line(x=" pig" , y=" horse" )
1503
1491
"""
1504
1492
if color is not None :
1505
1493
kwargs ["color" ] = color
@@ -1570,8 +1558,8 @@ def bar(
1570
1558
.. plot::
1571
1559
:context: close-figs
1572
1560
1573
- >>> df = pd.DataFrame({' lab' : ['A', 'B', 'C' ], ' val' : [10, 30, 20]})
1574
- >>> ax = df.plot.bar(x=' lab' , y=' val' , rot=0)
1561
+ >>> df = pd.DataFrame({" lab" : ["A", "B", "C" ], " val" : [10, 30, 20]})
1562
+ >>> ax = df.plot.bar(x=" lab" , y=" val" , rot=0)
1575
1563
1576
1564
Plot a whole dataframe to a bar plot. Each column is assigned a
1577
1565
distinct color, and each row is nested in a group along the
@@ -1582,10 +1570,18 @@ def bar(
1582
1570
1583
1571
>>> speed = [0.1, 17.5, 40, 48, 52, 69, 88]
1584
1572
>>> lifespan = [2, 8, 70, 1.5, 25, 12, 28]
1585
- >>> index = ['snail', 'pig', 'elephant',
1586
- ... 'rabbit', 'giraffe', 'coyote', 'horse']
1587
- >>> df = pd.DataFrame({'speed': speed,
1588
- ... 'lifespan': lifespan}, index=index)
1573
+ >>> index = [
1574
+ ... "snail",
1575
+ ... "pig",
1576
+ ... "elephant",
1577
+ ... "rabbit",
1578
+ ... "giraffe",
1579
+ ... "coyote",
1580
+ ... "horse",
1581
+ ... ]
1582
+ >>> df = pd.DataFrame(
1583
+ ... {"speed": speed, "lifespan": lifespan}, index=index
1584
+ ... )
1589
1585
>>> ax = df.plot.bar(rot=0)
1590
1586
1591
1587
Plot stacked bar charts for the DataFrame
@@ -1612,8 +1608,9 @@ def bar(
1612
1608
:context: close-figs
1613
1609
1614
1610
>>> axes = df.plot.bar(
1615
- ... rot=0, subplots=True,
1616
- ... color={"speed": "red", "lifespan": "green"}
1611
+ ... rot=0,
1612
+ ... subplots=True,
1613
+ ... color={"speed": "red", "lifespan": "green"},
1617
1614
... )
1618
1615
>>> axes[1].legend(loc=2) # doctest: +SKIP
1619
1616
@@ -1622,14 +1619,14 @@ def bar(
1622
1619
.. plot::
1623
1620
:context: close-figs
1624
1621
1625
- >>> ax = df.plot.bar(y=' speed' , rot=0)
1622
+ >>> ax = df.plot.bar(y=" speed" , rot=0)
1626
1623
1627
1624
Plot only selected categories for the DataFrame.
1628
1625
1629
1626
.. plot::
1630
1627
:context: close-figs
1631
1628
1632
- >>> ax = df.plot.bar(x=' lifespan' , rot=0)
1629
+ >>> ax = df.plot.bar(x=" lifespan" , rot=0)
1633
1630
"""
1634
1631
if color is not None :
1635
1632
kwargs ["color" ] = color
@@ -1700,8 +1697,8 @@ def barh(
1700
1697
.. plot::
1701
1698
:context: close-figs
1702
1699
1703
- >>> df = pd.DataFrame({' lab' : ['A', 'B', 'C' ], ' val' : [10, 30, 20]})
1704
- >>> ax = df.plot.barh(x=' lab' , y=' val' )
1700
+ >>> df = pd.DataFrame({" lab" : ["A", "B", "C" ], " val" : [10, 30, 20]})
1701
+ >>> ax = df.plot.barh(x=" lab" , y=" val" )
1705
1702
1706
1703
Plot a whole DataFrame to a horizontal bar plot
1707
1704
@@ -1710,10 +1707,18 @@ def barh(
1710
1707
1711
1708
>>> speed = [0.1, 17.5, 40, 48, 52, 69, 88]
1712
1709
>>> lifespan = [2, 8, 70, 1.5, 25, 12, 28]
1713
- >>> index = ['snail', 'pig', 'elephant',
1714
- ... 'rabbit', 'giraffe', 'coyote', 'horse']
1715
- >>> df = pd.DataFrame({'speed': speed,
1716
- ... 'lifespan': lifespan}, index=index)
1710
+ >>> index = [
1711
+ ... "snail",
1712
+ ... "pig",
1713
+ ... "elephant",
1714
+ ... "rabbit",
1715
+ ... "giraffe",
1716
+ ... "coyote",
1717
+ ... "horse",
1718
+ ... ]
1719
+ >>> df = pd.DataFrame(
1720
+ ... {"speed": speed, "lifespan": lifespan}, index=index
1721
+ ... )
1717
1722
>>> ax = df.plot.barh()
1718
1723
1719
1724
Plot stacked barh charts for the DataFrame
@@ -1737,11 +1742,19 @@ def barh(
1737
1742
1738
1743
>>> speed = [0.1, 17.5, 40, 48, 52, 69, 88]
1739
1744
>>> lifespan = [2, 8, 70, 1.5, 25, 12, 28]
1740
- >>> index = ['snail', 'pig', 'elephant',
1741
- ... 'rabbit', 'giraffe', 'coyote', 'horse']
1742
- >>> df = pd.DataFrame({'speed': speed,
1743
- ... 'lifespan': lifespan}, index=index)
1744
- >>> ax = df.plot.barh(y='speed')
1745
+ >>> index = [
1746
+ ... "snail",
1747
+ ... "pig",
1748
+ ... "elephant",
1749
+ ... "rabbit",
1750
+ ... "giraffe",
1751
+ ... "coyote",
1752
+ ... "horse",
1753
+ ... ]
1754
+ >>> df = pd.DataFrame(
1755
+ ... {"speed": speed, "lifespan": lifespan}, index=index
1756
+ ... )
1757
+ >>> ax = df.plot.barh(y="speed")
1745
1758
1746
1759
Plot DataFrame versus the desired column
1747
1760
@@ -1750,11 +1763,19 @@ def barh(
1750
1763
1751
1764
>>> speed = [0.1, 17.5, 40, 48, 52, 69, 88]
1752
1765
>>> lifespan = [2, 8, 70, 1.5, 25, 12, 28]
1753
- >>> index = ['snail', 'pig', 'elephant',
1754
- ... 'rabbit', 'giraffe', 'coyote', 'horse']
1755
- >>> df = pd.DataFrame({'speed': speed,
1756
- ... 'lifespan': lifespan}, index=index)
1757
- >>> ax = df.plot.barh(x='lifespan')
1766
+ >>> index = [
1767
+ ... "snail",
1768
+ ... "pig",
1769
+ ... "elephant",
1770
+ ... "rabbit",
1771
+ ... "giraffe",
1772
+ ... "coyote",
1773
+ ... "horse",
1774
+ ... ]
1775
+ >>> df = pd.DataFrame(
1776
+ ... {"speed": speed, "lifespan": lifespan}, index=index
1777
+ ... )
1778
+ >>> ax = df.plot.barh(x="lifespan")
1758
1779
"""
1759
1780
if color is not None :
1760
1781
kwargs ["color" ] = color
0 commit comments