Skip to content

Commit

Permalink
Merge pull request #468 from mwaskom/fix_facetgrid_axes_indices
Browse files Browse the repository at this point in the history
Fix two FacetGrid problems
  • Loading branch information
mwaskom committed Mar 13, 2015
2 parents 331d8a9 + 095aeb4 commit 9a1db37
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions doc/releases/v0.6.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@ Bug fixes
~~~~~~~~~

- 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.

- Fixed a bug in :meth:`FacetGrid.set_xticklabels` or :meth:`FacetGrid.set_yticklabels` when ``col_wrap`` is being used.
7 changes: 5 additions & 2 deletions seaborn/axisgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ def __init__(self, data, row=None, col=None, hue=None, col_wrap=None,

self._col_wrap = col_wrap
if col_wrap is not None:
if row is not None:
err = "Cannot use `row` and `col_wrap` together."
raise ValueError(err)
ncol = col_wrap
nrow = int(np.ceil(len(data[col].unique()) / col_wrap))
self._ncol = ncol
Expand Down Expand Up @@ -639,7 +642,7 @@ def set_ylabels(self, label=None, **kwargs):

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

def set_yticklabels(self, labels=None, **kwargs):
"""Set y axis tick labels on the left column of the grid."""
for ax in self.axes[-1, :]:
for ax in self._left_axes:
if labels is None:
labels = [l.get_text() for l in ax.get_yticklabels()]
ax.set_yticklabels(labels, **kwargs)
Expand Down
16 changes: 16 additions & 0 deletions seaborn/tests/test_axisgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ def test_col_wrap(self):
nt.assert_equal(g_wrap._ncol, 4)
nt.assert_equal(g_wrap._nrow, 3)

with nt.assert_raises(ValueError):
g = ag.FacetGrid(self.df, row="b", col="d", col_wrap=4)

plt.close("all")

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

g = ag.FacetGrid(self.df, col="d", col_wrap=5)
g.map(plt.plot, "x", "y")
g.set_xticklabels(rotation=45)
g.set_yticklabels(rotation=75)
for ax in g._bottom_axes:
for l in ax.get_xticklabels():
nt.assert_equal(l.get_rotation(), 45)
for ax in g._left_axes:
for l in ax.get_yticklabels():
nt.assert_equal(l.get_rotation(), 75)
plt.close("all")

def test_set_axis_labels(self):

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

def test_axis_lims(self):

Expand Down

0 comments on commit 9a1db37

Please sign in to comment.