Skip to content

Commit fc2d167

Browse files
acorbey-p
authored and
y-p
committed
BUG: scatter_plot ranges are unaligned among subframes GH5497
1 parent cabe9b1 commit fc2d167

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

doc/source/release.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ Bug Fixes
126126
of pandas in QTConsole, now fixed. If you're using an older version and
127127
need to supress the warnings, see (:issue:`5922`).
128128
- Bug in merging ``timedelta`` dtypes (:issue:`5695`)
129+
- Bug in plotting.scatter_matrix function. Wrong alignment among diagonal
130+
and off-diagonal plots, see (:issue:`5497`).
129131

130132
pandas 0.13.0
131133
-------------

pandas/tests/test_graphics.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,7 @@ def scat(**kwds):
755755
_check_plot_works(scat, diagonal='kde')
756756
_check_plot_works(scat, diagonal='density')
757757
_check_plot_works(scat, diagonal='hist')
758+
_check_plot_works(scat, range_padding=.1)
758759

759760
def scat2(x, y, by=None, ax=None, figsize=None):
760761
return plt.scatter_plot(df, x, y, by, ax, figsize=None)

pandas/tools/plotting.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,25 +202,34 @@ def use(self, key, value):
202202

203203
def scatter_matrix(frame, alpha=0.5, figsize=None, ax=None, grid=False,
204204
diagonal='hist', marker='.', density_kwds=None,
205-
hist_kwds=None, **kwds):
205+
hist_kwds=None, range_padding=0.05, **kwds):
206206
"""
207207
Draw a matrix of scatter plots.
208208
209209
Parameters
210210
----------
211211
frame : DataFrame
212-
alpha : amount of transparency applied
213-
figsize : a tuple (width, height) in inches
214-
ax : Matplotlib axis object
215-
grid : setting this to True will show the grid
216-
diagonal : pick between 'kde' and 'hist' for
212+
alpha : float, optional
213+
amount of transparency applied
214+
figsize : (float,float), optional
215+
a tuple (width, height) in inches
216+
ax : Matplotlib axis object, optional
217+
grid : bool, optional
218+
setting this to True will show the grid
219+
diagonal : {'hist', 'kde'}
220+
pick between 'kde' and 'hist' for
217221
either Kernel Density Estimation or Histogram
218222
plot in the diagonal
219-
marker : Matplotlib marker type, default '.'
223+
marker : str, optional
224+
Matplotlib marker type, default '.'
220225
hist_kwds : other plotting keyword arguments
221226
To be passed to hist function
222227
density_kwds : other plotting keyword arguments
223228
To be passed to kernel density estimate plot
229+
range_padding : float, optional
230+
relative extension of axis range in x and y
231+
with respect to (x_max - x_min) or (y_max - y_min),
232+
default 0.05
224233
kwds : other plotting keyword arguments
225234
To be passed to scatter function
226235
@@ -250,6 +259,13 @@ def scatter_matrix(frame, alpha=0.5, figsize=None, ax=None, grid=False,
250259
# workaround because `c='b'` is hardcoded in matplotlibs scatter method
251260
kwds.setdefault('c', plt.rcParams['patch.facecolor'])
252261

262+
boundaries_list = []
263+
for a in df.columns:
264+
values = df[a].values[mask[a].values]
265+
rmin_, rmax_ = np.min(values), np.max(values)
266+
rdelta_ext = (rmax_ - rmin_) * range_padding / 2.
267+
boundaries_list.append((rmin_ - rdelta_ext, rmax_+ rdelta_ext))
268+
253269
for i, a in zip(lrange(n), df.columns):
254270
for j, b in zip(lrange(n), df.columns):
255271
ax = axes[i, j]
@@ -260,18 +276,25 @@ def scatter_matrix(frame, alpha=0.5, figsize=None, ax=None, grid=False,
260276
# Deal with the diagonal by drawing a histogram there.
261277
if diagonal == 'hist':
262278
ax.hist(values, **hist_kwds)
279+
263280
elif diagonal in ('kde', 'density'):
264281
from scipy.stats import gaussian_kde
265282
y = values
266283
gkde = gaussian_kde(y)
267284
ind = np.linspace(y.min(), y.max(), 1000)
268285
ax.plot(ind, gkde.evaluate(ind), **density_kwds)
286+
287+
ax.set_xlim(boundaries_list[i])
288+
269289
else:
270290
common = (mask[a] & mask[b]).values
271291

272292
ax.scatter(df[b][common], df[a][common],
273293
marker=marker, alpha=alpha, **kwds)
274294

295+
ax.set_xlim(boundaries_list[j])
296+
ax.set_ylim(boundaries_list[i])
297+
275298
ax.set_xlabel('')
276299
ax.set_ylabel('')
277300

0 commit comments

Comments
 (0)