You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
RPlot is a flexible API for producing Trellis plots. These plots allow you to arrange data in a rectangular grid by values of certain attributes.
1660
+
RPlot was an API for producing Trellis plots. These plots allow you toµ
1661
+
arrange data in a rectangular grid by values of certain attributes.
1662
+
In the example below, data from the tips data set is arranged by the attributes
1663
+
'sex' and 'smoker'. Since both of those attributes can take on one of two
1664
+
values, the resulting grid has two columns and two rows. A histogram is
1665
+
displayed for each cell of the grid.
1666
+
1651
1667
1652
1668
.. ipython:: python
1653
1669
@@ -1665,7 +1681,20 @@ RPlot is a flexible API for producing Trellis plots. These plots allow you to ar
1665
1681
1666
1682
plt.close('all')
1667
1683
1668
-
In the example above, data from the tips data set is arranged by the attributes 'sex' and 'smoker'. Since both of those attributes can take on one of two values, the resulting grid has two columns and two rows. A histogram is displayed for each cell of the grid.
1684
+
A similar plot can be made with ``seaborn`` using the ``FacetGrid`` object,
1685
+
resulting in the following image:
1686
+
1687
+
.. code-block:: python
1688
+
1689
+
import seaborn as sns
1690
+
g = sns.FacetGrid(tips_data, row="sex", col="smoker")
1691
+
g.map(plt.hist, "total_bill")
1692
+
1693
+
.. image:: _static/rplot-seaborn-example1.png
1694
+
1695
+
1696
+
Example below is the same as previous except the plot is set to kernel density
1697
+
estimation. A ``seaborn`` example is included beneath.
1669
1698
1670
1699
.. ipython:: python
1671
1700
@@ -1683,7 +1712,15 @@ In the example above, data from the tips data set is arranged by the attributes
1683
1712
1684
1713
plt.close('all')
1685
1714
1686
-
Example above is the same as previous except the plot is set to kernel density estimation. This shows how easy it is to have different plots for the same Trellis structure.
1715
+
.. code-block:: python
1716
+
1717
+
g = sns.FacetGrid(tips_data, row="sex", col="smoker")
1718
+
g.map(sns.kdeplot, "total_bill")
1719
+
1720
+
.. image:: _static/rplot-seaborn-example2.png
1721
+
1722
+
The plot below shows that it is possible to have two or more plots for the same
1723
+
data displayed on the same Trellis grid cell.
1687
1724
1688
1725
.. ipython:: python
1689
1726
@@ -1702,7 +1739,27 @@ Example above is the same as previous except the plot is set to kernel density e
1702
1739
1703
1740
plt.close('all')
1704
1741
1705
-
The plot above shows that it is possible to have two or more plots for the same data displayed on the same Trellis grid cell.
1742
+
A seaborn equivalent for a simple scatter plot:
1743
+
1744
+
.. code-block:: python
1745
+
1746
+
g = sns.FacetGrid(tips_data, row="sex", col="smoker")
1747
+
g.map(plt.scatter, "total_bill", "tip")
1748
+
1749
+
.. image:: _static/rplot-seaborn-example3.png
1750
+
1751
+
and with a regression line, using the dedicated ``seaborn`` ``regplot`` function:
1752
+
1753
+
.. code-block:: python
1754
+
1755
+
g = sns.FacetGrid(tips_data, row="sex", col="smoker", margin_titles=True)
1756
+
g.map(sns.regplot, "total_bill", "tip", order=2)
1757
+
1758
+
.. image:: _static/rplot-seaborn-example3b.png
1759
+
1760
+
1761
+
Below is a similar plot but with 2D kernel density estimation plot superimposed,
1762
+
followed by a ``seaborn`` equivalent:
1706
1763
1707
1764
.. ipython:: python
1708
1765
@@ -1721,7 +1778,17 @@ The plot above shows that it is possible to have two or more plots for the same
1721
1778
1722
1779
plt.close('all')
1723
1780
1724
-
Above is a similar plot but with 2D kernel density estimation plot superimposed.
1781
+
.. code-block:: python
1782
+
1783
+
g = sns.FacetGrid(tips_data, row="sex", col="smoker")
1784
+
g.map(plt.scatter, "total_bill", "tip")
1785
+
g.map(sns.kdeplot, "total_bill", "tip")
1786
+
1787
+
.. image:: _static/rplot-seaborn-example4.png
1788
+
1789
+
It is possible to only use one attribute for grouping data. The example above
1790
+
only uses 'sex' attribute. If the second grouping attribute is not specified,
1791
+
the plots will be arranged in a column.
1725
1792
1726
1793
.. ipython:: python
1727
1794
@@ -1739,7 +1806,7 @@ Above is a similar plot but with 2D kernel density estimation plot superimposed.
1739
1806
1740
1807
plt.close('all')
1741
1808
1742
-
It is possible to only use one attribute for grouping data. The example above only uses 'sex' attribute. If the second grouping attribute is not specified, the plots will be arranged in a column.
1809
+
If the first grouping attribute is not specified the plots will be arranged in a row.
1743
1810
1744
1811
.. ipython:: python
1745
1812
@@ -1757,16 +1824,18 @@ It is possible to only use one attribute for grouping data. The example above on
1757
1824
1758
1825
plt.close('all')
1759
1826
1760
-
If the first grouping attribute is not specified the plots will be arranged in a row.
1827
+
In ``seaborn``, this can also be done by only specifying one of the ``row``
1828
+
and ``col`` arguments.
1829
+
1830
+
In the example below the colour and shape of the scatter plot graphical
1831
+
objects is mapped to 'day' and 'size' attributes respectively. You use
1832
+
scale objects to specify these mappings. The list of scale classes is
1833
+
given below with initialization arguments for quick reference.
@@ -1779,38 +1848,12 @@ If the first grouping attribute is not specified the plots will be arranged in a
1779
1848
1780
1849
plt.close('all')
1781
1850
1782
-
As shown above, scatter plots are also possible. Scatter plots allow you to map various data attributes to graphical properties of the plot. In the example above the colour and shape of the scatter plot graphical objects is mapped to 'day' and 'size' attributes respectively. You use scale objects to specify these mappings. The list of scale classes is given below with initialization arguments for quick reference.
1783
-
1784
-
1785
-
Scales
1786
-
~~~~~~
1787
-
1788
-
::
1789
-
1790
-
ScaleGradient(column, colour1, colour2)
1791
-
1792
-
This one allows you to map an attribute (specified by parameter column) value to the colour of a graphical object. The larger the value of the attribute the closer the colour will be to colour2, the smaller the value, the closer it will be to colour1.
1793
-
1794
-
::
1795
-
1796
-
ScaleGradient2(column, colour1, colour2, colour3)
1797
-
1798
-
The same as ScaleGradient but interpolates linearly between three colours instead of two.
1799
-
1800
-
::
1801
-
1802
-
ScaleSize(column, min_size, max_size, transform)
1803
-
1804
-
Map attribute value to size of the graphical object. Parameter min_size (default 5.0) is the minimum size of the graphical object, max_size (default 100.0) is the maximum size and transform is a one argument function that will be used to transform the attribute value (defaults to lambda x: x).
1805
-
1806
-
::
1807
-
1808
-
ScaleShape(column)
1809
-
1810
-
Map the shape of the object to attribute value. The attribute has to be categorical.
1851
+
This can also be done in ``seaborn``, at least for 3 variables:
1811
1852
1812
-
::
1853
+
.. code-block:: python
1813
1854
1814
-
ScaleRandomColour(column)
1855
+
g = sns.FacetGrid(tips_data, row="sex", col="smoker", hue="day")
1856
+
g.map(plt.scatter, "tip", "total_bill")
1857
+
g.add_legend()
1815
1858
1816
-
Assign a random colour to a value of categorical attribute specified by column.
0 commit comments