Skip to content

TST: Add layout tests for boxplot #7159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 43 additions & 39 deletions pandas/tests/test_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def _check_axes_shape(self, axes, axes_num=None, layout=None, figsize=(8.0, 6.0)
axes_num : number
expected number of axes. Unnecessary axes should be set to invisible.
layout : tuple
expected layout
expected layout, (expected number of rows , columns)
figsize : tuple
expected figsize. default is matplotlib default
"""
Expand All @@ -299,17 +299,22 @@ def _check_axes_shape(self, axes, axes_num=None, layout=None, figsize=(8.0, 6.0)
self.assertTrue(len(ax.get_children()) > 0)

if layout is not None:
if isinstance(axes, list):
self.assertEqual((len(axes), ), layout)
elif isinstance(axes, np.ndarray):
self.assertEqual(axes.shape, layout)
else:
# in case of AxesSubplot
self.assertEqual((1, ), layout)
result = self._get_axes_layout(plotting._flatten(axes))
self.assertEqual(result, layout)

self.assert_numpy_array_equal(np.round(visible_axes[0].figure.get_size_inches()),
np.array(figsize))

def _get_axes_layout(self, axes):
x_set = set()
y_set = set()
for ax in axes:
# check axes coordinates to estimate layout
points = ax.get_position().get_points()
x_set.add(points[0][0])
y_set.add(points[0][1])
return (len(y_set), len(x_set))

def _flatten_visible(self, axes):
"""
Flatten axes, and filter only visible
Expand Down Expand Up @@ -401,14 +406,14 @@ def test_plot(self):

# GH 6951
ax = _check_plot_works(self.ts.plot, subplots=True)
self._check_axes_shape(ax, axes_num=1, layout=(1, ))
self._check_axes_shape(ax, axes_num=1, layout=(1, 1))

@slow
def test_plot_figsize_and_title(self):
# figsize and title
ax = self.series.plot(title='Test', figsize=(16, 8))
self._check_text_labels(ax.title, 'Test')
self._check_axes_shape(ax, axes_num=1, layout=(1, ), figsize=(16, 8))
self._check_axes_shape(ax, axes_num=1, layout=(1, 1), figsize=(16, 8))

def test_ts_area_lim(self):
ax = self.ts.plot(kind='area', stacked=False)
Expand Down Expand Up @@ -556,10 +561,10 @@ def test_hist_layout_with_by(self):
df = self.hist_df

axes = _check_plot_works(df.height.hist, by=df.gender, layout=(2, 1))
self._check_axes_shape(axes, axes_num=2, layout=(2, ), figsize=(10, 5))
self._check_axes_shape(axes, axes_num=2, layout=(2, 1), figsize=(10, 5))

axes = _check_plot_works(df.height.hist, by=df.category, layout=(4, 1))
self._check_axes_shape(axes, axes_num=4, layout=(4, ), figsize=(10, 5))
self._check_axes_shape(axes, axes_num=4, layout=(4, 1), figsize=(10, 5))

axes = _check_plot_works(df.height.hist, by=df.classroom, layout=(2, 2))
self._check_axes_shape(axes, axes_num=3, layout=(2, 2), figsize=(10, 5))
Expand Down Expand Up @@ -757,9 +762,9 @@ def test_plot(self):
df = self.tdf
_check_plot_works(df.plot, grid=False)
axes = _check_plot_works(df.plot, subplots=True)
self._check_axes_shape(axes, axes_num=4, layout=(4, ))
self._check_axes_shape(axes, axes_num=4, layout=(4, 1))
_check_plot_works(df.plot, subplots=True, use_index=False)
self._check_axes_shape(axes, axes_num=4, layout=(4, ))
self._check_axes_shape(axes, axes_num=4, layout=(4, 1))

df = DataFrame({'x': [1, 2], 'y': [3, 4]})
with tm.assertRaises(TypeError):
Expand All @@ -774,7 +779,7 @@ def test_plot(self):
_check_plot_works(df.plot, ylim=(-100, 100), xlim=(-100, 100))

axes = _check_plot_works(df.plot, subplots=True, title='blah')
self._check_axes_shape(axes, axes_num=3, layout=(3, ))
self._check_axes_shape(axes, axes_num=3, layout=(3, 1))

_check_plot_works(df.plot, title='blah')

Expand Down Expand Up @@ -804,7 +809,7 @@ def test_plot(self):
# Test with single column
df = DataFrame({'x': np.random.rand(10)})
axes = _check_plot_works(df.plot, kind='bar', subplots=True)
self._check_axes_shape(axes, axes_num=1, layout=(1, ))
self._check_axes_shape(axes, axes_num=1, layout=(1, 1))

def test_nonnumeric_exclude(self):
df = DataFrame({'A': ["x", "y", "z"], 'B': [1, 2, 3]})
Expand Down Expand Up @@ -846,7 +851,7 @@ def test_plot_xy(self):
# figsize and title
ax = df.plot(x=1, y=2, title='Test', figsize=(16, 8))
self._check_text_labels(ax.title, 'Test')
self._check_axes_shape(ax, axes_num=1, layout=(1, ), figsize=(16., 8.))
self._check_axes_shape(ax, axes_num=1, layout=(1, 1), figsize=(16., 8.))

# columns.inferred_type == 'mixed'
# TODO add MultiIndex test
Expand Down Expand Up @@ -913,7 +918,7 @@ def test_subplots(self):

for kind in ['bar', 'barh', 'line', 'area']:
axes = df.plot(kind=kind, subplots=True, sharex=True, legend=True)
self._check_axes_shape(axes, axes_num=3, layout=(3, ))
self._check_axes_shape(axes, axes_num=3, layout=(3, 1))

for ax, column in zip(axes, df.columns):
self._check_legend_labels(ax, labels=[com.pprint_thing(column)])
Expand Down Expand Up @@ -1081,7 +1086,7 @@ def test_bar_linewidth(self):

# subplots
axes = df.plot(kind='bar', linewidth=2, subplots=True)
self._check_axes_shape(axes, axes_num=5, layout=(5, ))
self._check_axes_shape(axes, axes_num=5, layout=(5, 1))
for ax in axes:
for r in ax.patches:
self.assertEqual(r.get_linewidth(), 2)
Expand Down Expand Up @@ -1179,7 +1184,7 @@ def test_plot_scatter(self):

# GH 6951
axes = df.plot(x='x', y='y', kind='scatter', subplots=True)
self._check_axes_shape(axes, axes_num=1, layout=(1, ))
self._check_axes_shape(axes, axes_num=1, layout=(1, 1))

@slow
def test_plot_bar(self):
Expand Down Expand Up @@ -1486,7 +1491,7 @@ def test_kde(self):
self._check_legend_labels(ax, labels=expected)

axes = _check_plot_works(df.plot, kind='kde', subplots=True)
self._check_axes_shape(axes, axes_num=4, layout=(4, ))
self._check_axes_shape(axes, axes_num=4, layout=(4, 1))

axes = df.plot(kind='kde', logy=True, subplots=True)
self._check_ax_scales(axes, yaxis='log')
Expand Down Expand Up @@ -1949,8 +1954,7 @@ def test_hexbin_basic(self):
# hexbin should have 2 axes in the figure, 1 for plotting and another is colorbar
self.assertEqual(len(axes[0].figure.axes), 2)
# return value is single axes
self._check_axes_shape(axes, axes_num=1, layout=(1, ))

self._check_axes_shape(axes, axes_num=1, layout=(1, 1))

@slow
def test_hexbin_with_c(self):
Expand Down Expand Up @@ -2193,31 +2197,31 @@ class TestDataFrameGroupByPlots(TestPlotBase):
def test_boxplot(self):
grouped = self.hist_df.groupby(by='gender')
box = _check_plot_works(grouped.boxplot, return_type='dict')
self._check_axes_shape(self.plt.gcf().axes, axes_num=2)
self._check_axes_shape(self.plt.gcf().axes, axes_num=2, layout=(1, 2))

box = _check_plot_works(grouped.boxplot, subplots=False,
return_type='dict')
self._check_axes_shape(self.plt.gcf().axes, axes_num=2)
self._check_axes_shape(self.plt.gcf().axes, axes_num=2, layout=(1, 2))

tuples = lzip(string.ascii_letters[:10], range(10))
df = DataFrame(np.random.rand(10, 3),
index=MultiIndex.from_tuples(tuples))

grouped = df.groupby(level=1)
box = _check_plot_works(grouped.boxplot, return_type='dict')
self._check_axes_shape(self.plt.gcf().axes, axes_num=10)
self._check_axes_shape(self.plt.gcf().axes, axes_num=10, layout=(4, 3))

box = _check_plot_works(grouped.boxplot, subplots=False,
return_type='dict')
self._check_axes_shape(self.plt.gcf().axes, axes_num=10)
self._check_axes_shape(self.plt.gcf().axes, axes_num=10, layout=(4, 3))

grouped = df.unstack(level=1).groupby(level=0, axis=1)
box = _check_plot_works(grouped.boxplot, return_type='dict')
self._check_axes_shape(self.plt.gcf().axes, axes_num=3)
self._check_axes_shape(self.plt.gcf().axes, axes_num=3, layout=(2, 2))

box = _check_plot_works(grouped.boxplot, subplots=False,
return_type='dict')
self._check_axes_shape(self.plt.gcf().axes, axes_num=3)
self._check_axes_shape(self.plt.gcf().axes, axes_num=3, layout=(2, 2))

def test_series_plot_color_kwargs(self):
# GH1890
Expand Down Expand Up @@ -2327,35 +2331,35 @@ def test_grouped_box_layout(self):

box = _check_plot_works(df.groupby('gender').boxplot, column='height',
return_type='dict')
self._check_axes_shape(self.plt.gcf().axes, axes_num=2)
self._check_axes_shape(self.plt.gcf().axes, axes_num=2, layout=(1, 2))

box = _check_plot_works(df.groupby('category').boxplot, column='height',
return_type='dict')
self._check_axes_shape(self.plt.gcf().axes, axes_num=4)
self._check_axes_shape(self.plt.gcf().axes, axes_num=4, layout=(2, 2))

# GH 6769
box = _check_plot_works(df.groupby('classroom').boxplot,
column='height', return_type='dict')
self._check_axes_shape(self.plt.gcf().axes, axes_num=3)
self._check_axes_shape(self.plt.gcf().axes, axes_num=3, layout=(2, 2))

box = df.boxplot(column=['height', 'weight', 'category'], by='gender')
self._check_axes_shape(self.plt.gcf().axes, axes_num=3)
self._check_axes_shape(self.plt.gcf().axes, axes_num=3, layout=(2, 2))

box = df.groupby('classroom').boxplot(
column=['height', 'weight', 'category'], return_type='dict')
self._check_axes_shape(self.plt.gcf().axes, axes_num=3)
self._check_axes_shape(self.plt.gcf().axes, axes_num=3, layout=(2, 2))

box = _check_plot_works(df.groupby('category').boxplot, column='height',
layout=(3, 2), return_type='dict')
self._check_axes_shape(self.plt.gcf().axes, axes_num=4)
self._check_axes_shape(self.plt.gcf().axes, axes_num=4, layout=(3, 2))

box = df.boxplot(column=['height', 'weight', 'category'], by='gender', layout=(4, 1))
self._check_axes_shape(self.plt.gcf().axes, axes_num=3)
self._check_axes_shape(self.plt.gcf().axes, axes_num=3, layout=(4, 1))

box = df.groupby('classroom').boxplot(
column=['height', 'weight', 'category'], layout=(1, 4),
return_type='dict')
self._check_axes_shape(self.plt.gcf().axes, axes_num=3)
self._check_axes_shape(self.plt.gcf().axes, axes_num=3, layout=(1, 4))

@slow
def test_grouped_hist_layout(self):
Expand All @@ -2367,10 +2371,10 @@ def test_grouped_hist_layout(self):
layout=(1, 3))

axes = _check_plot_works(df.hist, column='height', by=df.gender, layout=(2, 1))
self._check_axes_shape(axes, axes_num=2, layout=(2, ), figsize=(10, 5))
self._check_axes_shape(axes, axes_num=2, layout=(2, 1), figsize=(10, 5))

axes = _check_plot_works(df.hist, column='height', by=df.category, layout=(4, 1))
self._check_axes_shape(axes, axes_num=4, layout=(4, ), figsize=(10, 5))
self._check_axes_shape(axes, axes_num=4, layout=(4, 1), figsize=(10, 5))

axes = _check_plot_works(df.hist, column='height', by=df.category,
layout=(4, 2), figsize=(12, 8))
Expand Down