Skip to content

Commit 9e84387

Browse files
committed
mwaskom#185 make it possible to override gridsize with hex jointplot
1 parent c2b278f commit 9e84387

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ dist/
66
seaborn.egg-info/
77
.coverage
88
cover/
9+
.idea

seaborn/distributions.py

+34-12
Original file line numberDiff line numberDiff line change
@@ -884,37 +884,59 @@ def jointplot(x, y, data=None, kind="scatter", stat_func=stats.pearsonr,
884884
# Plot the data using the grid
885885
if kind == "scatter":
886886

887-
grid.plot_joint(plt.scatter, color=color, **joint_kws)
888-
grid.plot_marginals(distplot, kde=False, color=color, **marginal_kws)
887+
joint_kws.setdefault("color", color)
888+
grid.plot_joint(plt.scatter, **joint_kws)
889+
890+
marginal_kws.setdefault("kde", False)
891+
marginal_kws.setdefault("color", color)
892+
grid.plot_marginals(distplot, **marginal_kws)
889893

890894
elif kind.startswith("hex"):
891895

892896
x_bins = _freedman_diaconis_bins(grid.x)
893897
y_bins = _freedman_diaconis_bins(grid.y)
894898
gridsize = int(np.mean([x_bins, y_bins]))
895899

896-
grid.plot_joint(plt.hexbin, gridsize=gridsize, cmap=cmap, **joint_kws)
897-
grid.plot_marginals(distplot, kde=False, color=color, **marginal_kws)
900+
joint_kws.setdefault("gridsize", gridsize)
901+
joint_kws.setdefault("cmap", cmap)
902+
grid.plot_joint(plt.hexbin, **joint_kws)
903+
904+
marginal_kws.setdefault("kde", False)
905+
marginal_kws.setdefault("color", color)
906+
grid.plot_marginals(distplot, **marginal_kws)
898907

899908
elif kind.startswith("kde"):
900909

901-
grid.plot_joint(kdeplot, shade=True, cmap=cmap, **joint_kws)
902-
grid.plot_marginals(kdeplot, shade=True, color=color, **marginal_kws)
910+
joint_kws.setdefault("shade", True)
911+
joint_kws.setdefault("cmap", cmap)
912+
grid.plot_joint(kdeplot, **joint_kws)
913+
914+
marginal_kws.setdefault("shade", True)
915+
marginal_kws.setdefault("color", color)
916+
grid.plot_marginals(kdeplot, **marginal_kws)
903917

904918
elif kind.startswith("reg"):
905919

906920
from .linearmodels import regplot
907-
grid.plot_marginals(distplot, color=color, **marginal_kws)
908-
grid.plot_joint(regplot, color=color, **joint_kws)
921+
922+
marginal_kws.setdefault("color", color)
923+
grid.plot_marginals(distplot, **marginal_kws)
924+
925+
joint_kws.setdefault("color", color)
926+
grid.plot_joint(regplot, **joint_kws)
909927

910928
elif kind.startswith("resid"):
911929

912930
from .linearmodels import residplot
913-
grid.plot_joint(residplot, color=color, **joint_kws)
931+
932+
joint_kws.setdefault("color", color)
933+
grid.plot_joint(residplot, **joint_kws)
934+
914935
x, y = grid.ax_joint.collections[0].get_offsets().T
915-
distplot(x, color=color, kde=False, ax=grid.ax_marg_x)
916-
distplot(y, color=color, kde=False, vertical=True,
917-
fit=stats.norm, ax=grid.ax_marg_y)
936+
marginal_kws.setdefault("color", color)
937+
marginal_kws.setdefault("kde", False)
938+
distplot(x, ax=grid.ax_marg_x, **marginal_kws)
939+
distplot(y, vertical=True, fit=stats.norm, ax=grid.ax_marg_y, **marginal_kws)
918940
stat_func = None
919941

920942
if stat_func is not None:

seaborn/tests/test_distributions.py

+13
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,16 @@ def test_annotation(self):
348348
nt.assert_is(g.ax_joint.legend_, None)
349349

350350
plt.close("all")
351+
352+
def test_hex_customise(self):
353+
354+
# test that default gridsize can be overridden
355+
g = dist.jointplot("x", "y", self.data, kind="hex", joint_kws=dict(gridsize=5))
356+
nt.assert_equal(len(g.ax_joint.collections), 1)
357+
a = g.ax_joint.collections[0].get_array()
358+
nt.assert_equal(28, a.shape[0]) # 28 hexagons expected for gridsize 5
359+
360+
plt.close("all")
361+
362+
363+

0 commit comments

Comments
 (0)