Skip to content

Commit fb12f01

Browse files
DOC: add seaborn equivalent examples in the rplot trellis docs (GH3445)
1 parent f3bea53 commit fb12f01

7 files changed

+79
-46
lines changed
12.9 KB
Loading
30.6 KB
Loading
19 KB
Loading
30.4 KB
Loading
54.3 KB
Loading
26 KB
Loading

doc/source/visualization.rst

+79-46
Original file line numberDiff line numberDiff line change
@@ -1609,13 +1609,13 @@ Trellis plotting interface
16091609

16101610
.. warning::
16111611

1612-
The ``rplot`` trellis plotting interface is deprecated and will be removed
1613-
in a future version. We refer to external packages like
1612+
The ``rplot`` trellis plotting interface is **deprecated and will be removed
1613+
in a future version**. We refer to external packages like
16141614
`seaborn <https://github.com/mwaskom/seaborn>`_ for similar but more
16151615
refined functionality.
16161616

16171617
The docs below include some example on how to convert your existing code to
1618-
these packages.
1618+
``seaborn``.
16191619

16201620
.. ipython:: python
16211621
:suppress:
@@ -1657,7 +1657,13 @@ We import the rplot API:
16571657
Examples
16581658
~~~~~~~~
16591659

1660-
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+
16611667

16621668
.. ipython:: python
16631669
@@ -1675,7 +1681,20 @@ RPlot is a flexible API for producing Trellis plots. These plots allow you to ar
16751681
16761682
plt.close('all')
16771683
1678-
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.
16791698

16801699
.. ipython:: python
16811700
@@ -1693,7 +1712,15 @@ In the example above, data from the tips data set is arranged by the attributes
16931712
16941713
plt.close('all')
16951714
1696-
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.
16971724

16981725
.. ipython:: python
16991726
@@ -1712,7 +1739,27 @@ Example above is the same as previous except the plot is set to kernel density e
17121739
17131740
plt.close('all')
17141741
1715-
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:
17161763

17171764
.. ipython:: python
17181765
@@ -1731,7 +1778,17 @@ The plot above shows that it is possible to have two or more plots for the same
17311778
17321779
plt.close('all')
17331780
1734-
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.
17351792

17361793
.. ipython:: python
17371794
@@ -1749,7 +1806,7 @@ Above is a similar plot but with 2D kernel density estimation plot superimposed.
17491806
17501807
plt.close('all')
17511808
1752-
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.
17531810

17541811
.. ipython:: python
17551812
@@ -1767,16 +1824,18 @@ It is possible to only use one attribute for grouping data. The example above on
17671824
17681825
plt.close('all')
17691826
1770-
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.
17711834

17721835
.. ipython:: python
17731836
17741837
plt.figure()
17751838
1776-
plot = rplot.RPlot(tips_data, x='total_bill', y='tip')
1777-
plot.add(rplot.TrellisGrid(['.', 'smoker']))
1778-
plot.add(rplot.GeomHistogram())
1779-
17801839
plot = rplot.RPlot(tips_data, x='tip', y='total_bill')
17811840
plot.add(rplot.TrellisGrid(['sex', 'smoker']))
17821841
plot.add(rplot.GeomPoint(size=80.0, colour=rplot.ScaleRandomColour('day'), shape=rplot.ScaleShape('size'), alpha=1.0))
@@ -1789,38 +1848,12 @@ If the first grouping attribute is not specified the plots will be arranged in a
17891848
17901849
plt.close('all')
17911850
1792-
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.
1793-
1794-
1795-
Scales
1796-
~~~~~~
1797-
1798-
::
1799-
1800-
ScaleGradient(column, colour1, colour2)
1801-
1802-
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.
1803-
1804-
::
1805-
1806-
ScaleGradient2(column, colour1, colour2, colour3)
1807-
1808-
The same as ScaleGradient but interpolates linearly between three colours instead of two.
1809-
1810-
::
1811-
1812-
ScaleSize(column, min_size, max_size, transform)
1813-
1814-
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).
1815-
1816-
::
1817-
1818-
ScaleShape(column)
1819-
1820-
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:
18211852

1822-
::
1853+
.. code-block:: python
18231854
1824-
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()
18251858
1826-
Assign a random colour to a value of categorical attribute specified by column.
1859+
.. image:: _static/rplot-seaborn-example6.png

0 commit comments

Comments
 (0)