Skip to content

Commit c8e88d4

Browse files
author
Tom Augspurger
committed
Merge pull request #8353 from TomAugspurger/trellis-doc
DOC: move trellis under visualization.rst
2 parents 709d0d6 + 998b8c9 commit c8e88d4

File tree

2 files changed

+180
-179
lines changed

2 files changed

+180
-179
lines changed

doc/source/rplot.rst

-179
This file was deleted.

doc/source/visualization.rst

+180
Original file line numberDiff line numberDiff line change
@@ -1378,3 +1378,183 @@ when plotting a large number of points.
13781378
:suppress:
13791379
13801380
plt.close('all')
1381+
1382+
1383+
.. _rplot:
1384+
1385+
1386+
Trellis plotting interface
1387+
--------------------------
1388+
1389+
.. ipython:: python
1390+
:suppress:
1391+
1392+
import numpy as np
1393+
np.random.seed(123456)
1394+
from pandas import *
1395+
options.display.max_rows=15
1396+
import pandas.util.testing as tm
1397+
randn = np.random.randn
1398+
np.set_printoptions(precision=4, suppress=True)
1399+
import matplotlib.pyplot as plt
1400+
tips_data = read_csv('data/tips.csv')
1401+
iris_data = read_csv('data/iris.data')
1402+
from pandas import read_csv
1403+
from pandas.tools.plotting import radviz
1404+
import pandas.tools.rplot as rplot
1405+
plt.close('all')
1406+
1407+
1408+
.. note::
1409+
1410+
The tips data set can be downloaded `here
1411+
<http://wesmckinney.com/files/tips.csv>`__. Once you download it execute
1412+
1413+
.. code-block:: python
1414+
1415+
from pandas import read_csv
1416+
tips_data = read_csv('tips.csv')
1417+
1418+
from the directory where you downloaded the file.
1419+
1420+
We import the rplot API:
1421+
1422+
.. ipython:: python
1423+
1424+
import pandas.tools.rplot as rplot
1425+
1426+
Examples
1427+
~~~~~~~~
1428+
1429+
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.
1430+
1431+
.. ipython:: python
1432+
1433+
plt.figure()
1434+
1435+
plot = rplot.RPlot(tips_data, x='total_bill', y='tip')
1436+
plot.add(rplot.TrellisGrid(['sex', 'smoker']))
1437+
plot.add(rplot.GeomHistogram())
1438+
1439+
@savefig rplot1_tips.png
1440+
plot.render(plt.gcf())
1441+
1442+
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.
1443+
1444+
.. ipython:: python
1445+
1446+
plt.figure()
1447+
1448+
plot = rplot.RPlot(tips_data, x='total_bill', y='tip')
1449+
plot.add(rplot.TrellisGrid(['sex', 'smoker']))
1450+
plot.add(rplot.GeomDensity())
1451+
1452+
@savefig rplot2_tips.png
1453+
plot.render(plt.gcf())
1454+
1455+
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.
1456+
1457+
.. ipython:: python
1458+
1459+
plt.figure()
1460+
1461+
plot = rplot.RPlot(tips_data, x='total_bill', y='tip')
1462+
plot.add(rplot.TrellisGrid(['sex', 'smoker']))
1463+
plot.add(rplot.GeomScatter())
1464+
plot.add(rplot.GeomPolyFit(degree=2))
1465+
1466+
@savefig rplot3_tips.png
1467+
plot.render(plt.gcf())
1468+
1469+
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.
1470+
1471+
.. ipython:: python
1472+
1473+
plt.figure()
1474+
1475+
plot = rplot.RPlot(tips_data, x='total_bill', y='tip')
1476+
plot.add(rplot.TrellisGrid(['sex', 'smoker']))
1477+
plot.add(rplot.GeomScatter())
1478+
plot.add(rplot.GeomDensity2D())
1479+
1480+
@savefig rplot4_tips.png
1481+
plot.render(plt.gcf())
1482+
1483+
Above is a similar plot but with 2D kernel density estimation plot superimposed.
1484+
1485+
.. ipython:: python
1486+
1487+
plt.figure()
1488+
1489+
plot = rplot.RPlot(tips_data, x='total_bill', y='tip')
1490+
plot.add(rplot.TrellisGrid(['sex', '.']))
1491+
plot.add(rplot.GeomHistogram())
1492+
1493+
@savefig rplot5_tips.png
1494+
plot.render(plt.gcf())
1495+
1496+
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.
1497+
1498+
.. ipython:: python
1499+
1500+
plt.figure()
1501+
1502+
plot = rplot.RPlot(tips_data, x='total_bill', y='tip')
1503+
plot.add(rplot.TrellisGrid(['.', 'smoker']))
1504+
plot.add(rplot.GeomHistogram())
1505+
1506+
@savefig rplot6_tips.png
1507+
plot.render(plt.gcf())
1508+
1509+
If the first grouping attribute is not specified the plots will be arranged in a row.
1510+
1511+
.. ipython:: python
1512+
1513+
plt.figure()
1514+
1515+
plot = rplot.RPlot(tips_data, x='total_bill', y='tip')
1516+
plot.add(rplot.TrellisGrid(['.', 'smoker']))
1517+
plot.add(rplot.GeomHistogram())
1518+
1519+
plot = rplot.RPlot(tips_data, x='tip', y='total_bill')
1520+
plot.add(rplot.TrellisGrid(['sex', 'smoker']))
1521+
plot.add(rplot.GeomPoint(size=80.0, colour=rplot.ScaleRandomColour('day'), shape=rplot.ScaleShape('size'), alpha=1.0))
1522+
1523+
@savefig rplot7_tips.png
1524+
plot.render(plt.gcf())
1525+
1526+
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.
1527+
1528+
1529+
Scales
1530+
~~~~~~
1531+
1532+
::
1533+
1534+
ScaleGradient(column, colour1, colour2)
1535+
1536+
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.
1537+
1538+
::
1539+
1540+
ScaleGradient2(column, colour1, colour2, colour3)
1541+
1542+
The same as ScaleGradient but interpolates linearly between three colours instead of two.
1543+
1544+
::
1545+
1546+
ScaleSize(column, min_size, max_size, transform)
1547+
1548+
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).
1549+
1550+
::
1551+
1552+
ScaleShape(column)
1553+
1554+
Map the shape of the object to attribute value. The attribute has to be categorical.
1555+
1556+
::
1557+
1558+
ScaleRandomColour(column)
1559+
1560+
Assign a random colour to a value of categorical attribute specified by column.

0 commit comments

Comments
 (0)