Skip to content

Commit 9a1db37

Browse files
committed
Merge pull request mwaskom#468 from mwaskom/fix_facetgrid_axes_indices
Fix two FacetGrid problems
2 parents 331d8a9 + 095aeb4 commit 9a1db37

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

doc/releases/v0.6.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,5 @@ Bug fixes
5757
~~~~~~~~~
5858

5959
- Fixed a bug in :class:`FacetGrid` and :class:`PairGrid` that lead to incorrect legend labels when levels of the ``hue`` variable appeared in ``hue_order`` but not in the data.
60+
61+
- Fixed a bug in :meth:`FacetGrid.set_xticklabels` or :meth:`FacetGrid.set_yticklabels` when ``col_wrap`` is being used.

seaborn/axisgrid.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ def __init__(self, data, row=None, col=None, hue=None, col_wrap=None,
249249

250250
self._col_wrap = col_wrap
251251
if col_wrap is not None:
252+
if row is not None:
253+
err = "Cannot use `row` and `col_wrap` together."
254+
raise ValueError(err)
252255
ncol = col_wrap
253256
nrow = int(np.ceil(len(data[col].unique()) / col_wrap))
254257
self._ncol = ncol
@@ -639,7 +642,7 @@ def set_ylabels(self, label=None, **kwargs):
639642

640643
def set_xticklabels(self, labels=None, step=None, **kwargs):
641644
"""Set x axis tick labels on the bottom row of the grid."""
642-
for ax in self.axes[-1, :]:
645+
for ax in self._bottom_axes:
643646
if labels is None:
644647
labels = [l.get_text() for l in ax.get_xticklabels()]
645648
if step is not None:
@@ -651,7 +654,7 @@ def set_xticklabels(self, labels=None, step=None, **kwargs):
651654

652655
def set_yticklabels(self, labels=None, **kwargs):
653656
"""Set y axis tick labels on the left column of the grid."""
654-
for ax in self.axes[-1, :]:
657+
for ax in self._left_axes:
655658
if labels is None:
656659
labels = [l.get_text() for l in ax.get_yticklabels()]
657660
ax.set_yticklabels(labels, **kwargs)

seaborn/tests/test_axisgrid.py

+16
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ def test_col_wrap(self):
104104
nt.assert_equal(g_wrap._ncol, 4)
105105
nt.assert_equal(g_wrap._nrow, 3)
106106

107+
with nt.assert_raises(ValueError):
108+
g = ag.FacetGrid(self.df, row="b", col="d", col_wrap=4)
109+
107110
plt.close("all")
108111

109112
def test_normal_axes(self):
@@ -489,6 +492,18 @@ def test_set_ticklabels(self):
489492
got_x = [int(l.get_text()) for l in g.axes[0, 0].get_xticklabels()]
490493
npt.assert_array_equal(x[::2], got_x)
491494

495+
g = ag.FacetGrid(self.df, col="d", col_wrap=5)
496+
g.map(plt.plot, "x", "y")
497+
g.set_xticklabels(rotation=45)
498+
g.set_yticklabels(rotation=75)
499+
for ax in g._bottom_axes:
500+
for l in ax.get_xticklabels():
501+
nt.assert_equal(l.get_rotation(), 45)
502+
for ax in g._left_axes:
503+
for l in ax.get_yticklabels():
504+
nt.assert_equal(l.get_rotation(), 75)
505+
plt.close("all")
506+
492507
def test_set_axis_labels(self):
493508

494509
g = ag.FacetGrid(self.df, row="a", col="b")
@@ -502,6 +517,7 @@ def test_set_axis_labels(self):
502517
got_y = [ax.get_ylabel() for ax in g.axes[:, 0]]
503518
npt.assert_array_equal(got_x, xlab)
504519
npt.assert_array_equal(got_y, ylab)
520+
plt.close("all")
505521

506522
def test_axis_lims(self):
507523

0 commit comments

Comments
 (0)