From 923224272f3e3ac81f128977560d62bc871b42fb Mon Sep 17 00:00:00 2001 From: Jacques Kvam Date: Sun, 18 Mar 2012 00:12:52 -0700 Subject: [PATCH 1/2] add scatter plot matrix method --- pandas/core/frame.py | 60 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 4d6e3176108fd..21ce2bfe3fc47 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4018,6 +4018,66 @@ def hist(self, grid=True, **kwds): ax.grid(grid) return axes + + def scatter_matrix(self, **kwds): + """ + Draw a matrix of scatter plots. + + Parameters + ---------- + kwds : other plotting keyword arguments + To be passed to scatter function + + Examples + -------- + >>> df = DataFrame(np.random.randn(1000, 4), columns=['A','B','C','D']) + >>> df.scatter_matrix(alpha=0.2) + """ + import pandas.tools.plotting as gfx + + df = self._get_numeric_data() + n = df.columns.size + fig, axes = gfx.subplots(nrows=n, ncols=n) + + # no gaps between subplots + fig.subplots_adjust(wspace=0, hspace=0) + + for i, a in zip(range(n), df.columns): + for j, b in zip(range(n), df.columns): + axes[i, j].scatter(df[b], df[a], **kwds) + axes[i, j].yaxis.set_visible(False) + axes[i, j].xaxis.set_visible(False) + + # setup labels + if i == 0 and j % 2 == 1: + axes[i, j].set_xlabel(b, visible=True) + axes[i, j].xaxis.set_visible(True) + axes[i, j].xaxis.set_ticks_position('top') + axes[i, j].xaxis.set_label_position('top') + if i == n - 1 and j % 2 == 0: + axes[i, j].set_xlabel(b, visible=True) + axes[i, j].xaxis.set_visible(True) + axes[i, j].xaxis.set_ticks_position('bottom') + axes[i, j].xaxis.set_label_position('bottom') + if j == 0 and i % 2 == 0: + axes[i, j].set_ylabel(a, visible=True) + axes[i, j].yaxis.set_visible(True) + axes[i, j].yaxis.set_ticks_position('left') + axes[i, j].yaxis.set_label_position('left') + if j == n - 1 and i % 2 == 1: + axes[i, j].set_ylabel(a, visible=True) + axes[i, j].yaxis.set_visible(True) + axes[i, j].yaxis.set_ticks_position('right') + axes[i, j].yaxis.set_label_position('right') + + # ensure {x,y}lim off diagonal are the same as diagonal + for i in range(n): + for j in range(n): + if i != j: + axes[i, j].set_xlim(axes[j, j].get_xlim()) + axes[i, j].set_ylim(axes[i, i].get_xlim()) + + return axes #---------------------------------------------------------------------- # Deprecated stuff From 913af023427a75a44b108b489c2153a80d68fc20 Mon Sep 17 00:00:00 2001 From: Jacques Kvam Date: Mon, 19 Mar 2012 19:22:28 -0700 Subject: [PATCH 2/2] typo --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 21ce2bfe3fc47..ae737b187a8c6 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4075,7 +4075,7 @@ def scatter_matrix(self, **kwds): for j in range(n): if i != j: axes[i, j].set_xlim(axes[j, j].get_xlim()) - axes[i, j].set_ylim(axes[i, i].get_xlim()) + axes[i, j].set_ylim(axes[i, i].get_ylim()) return axes #----------------------------------------------------------------------