Skip to content

Commit debbb2d

Browse files
committed
BUG: fix GH #326, start unit testing plot methods
1 parent 46af335 commit debbb2d

File tree

7 files changed

+121
-14
lines changed

7 files changed

+121
-14
lines changed

pandas/core/frame.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2738,7 +2738,7 @@ def clip_lower(self, threshold):
27382738
# Plotting
27392739

27402740
def plot(self, subplots=False, sharex=True, sharey=False, use_index=True,
2741-
figsize=None, grid=True, **kwds): # pragma: no cover
2741+
figsize=None, grid=True, legend=True, ax=None, **kwds):
27422742
"""
27432743
Make line plot of DataFrame's series with the index on the x-axis using
27442744
matplotlib / pylab.
@@ -2764,12 +2764,15 @@ def plot(self, subplots=False, sharex=True, sharey=False, use_index=True,
27642764
import matplotlib.pyplot as plt
27652765

27662766
if subplots:
2767-
_, axes = plt.subplots(nrows=len(self.columns),
2767+
fig, axes = plt.subplots(nrows=len(self.columns),
27682768
sharex=sharex, sharey=sharey,
27692769
figsize=figsize)
27702770
else:
2771-
fig = plt.figure(figsize=figsize)
2772-
ax = fig.add_subplot(111)
2771+
if ax is None:
2772+
fig = plt.figure(figsize=figsize)
2773+
ax = fig.add_subplot(111)
2774+
else:
2775+
fig = ax.get_figure()
27732776

27742777
if use_index:
27752778
x = self.index
@@ -2781,23 +2784,25 @@ def plot(self, subplots=False, sharex=True, sharey=False, use_index=True,
27812784
y = self[col].values if not empty else np.zeros(x.shape)
27822785
if subplots:
27832786
ax = axes[i]
2784-
ax.plot(x, y, 'k', label=col, **kwds)
2787+
ax.plot(x, y, 'k', label=str(col), **kwds)
27852788
ax.legend(loc='best')
27862789
else:
2787-
ax.plot(x, y, label=col, **kwds)
2790+
ax.plot(x, y, label=str(col), **kwds)
27882791

27892792
ax.grid(grid)
27902793

27912794
# try to make things prettier
27922795
try:
2793-
fig = plt.gcf()
27942796
fig.autofmt_xdate()
27952797
except Exception:
27962798
pass
27972799

2800+
if legend and not subplots:
2801+
ax.legend(loc='best')
2802+
27982803
plt.draw_if_interactive()
27992804

2800-
def hist(self, grid=True, **kwds): # pragma: no cover
2805+
def hist(self, grid=True, **kwds):
28012806
"""
28022807
Draw Histogram the DataFrame's series using matplotlib / pylab.
28032808

pandas/core/series.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,7 +1636,7 @@ def isin(self, values):
16361636
# Miscellaneous
16371637

16381638
def plot(self, label=None, kind='line', use_index=True, rot=30, ax=None,
1639-
style='-', grid=True, **kwds): # pragma: no cover
1639+
style='-', grid=True, **kwds):
16401640
"""
16411641
Plot the input series with the index on the x-axis using matplotlib
16421642
@@ -1697,12 +1697,12 @@ def plot(self, label=None, kind='line', use_index=True, rot=30, ax=None,
16971697
try:
16981698
fig = plt.gcf()
16991699
fig.autofmt_xdate()
1700-
except Exception:
1700+
except Exception: # pragma: no cover
17011701
pass
17021702

17031703
plt.draw_if_interactive()
17041704

1705-
def hist(self, ax=None, grid=True, **kwds): # pragma: no cover
1705+
def hist(self, ax=None, grid=True, **kwds):
17061706
"""
17071707
Draw histogram of the input series using matplotlib
17081708

pandas/src/tseries.pyx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,18 @@ def isAllDates(ndarray[object, ndim=1] arr):
165165
return True
166166

167167
def ismember(ndarray arr, set values):
168+
'''
169+
Checks whether
170+
171+
Parameters
172+
----------
173+
arr : ndarray
174+
values : set
175+
176+
Returns
177+
-------
178+
ismember : ndarray (boolean dtype)
179+
'''
168180
cdef:
169181
Py_ssize_t i, n
170182
flatiter it

pandas/stats/tests/test_ols.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ class TestOLS(BaseTest):
4747
# TODO: Add tests for non pooled OLS.
4848

4949
@classmethod
50-
def setupClass(cls):
50+
def setUpClass(cls):
51+
try:
52+
import matplotlib as mpl
53+
mpl.use('Agg', warn=False)
54+
except ImportError:
55+
pass
56+
5157
try:
5258
import scikits.statsmodels.api as _
5359
except ImportError:

pandas/tests/test_frame.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3219,7 +3219,6 @@ def test_series_put_names(self):
32193219
self.assertEqual(v.name, k)
32203220

32213221

3222-
32233222
class TestDataFrameJoin(unittest.TestCase):
32243223

32253224
def setUp(self):

pandas/tests/test_graphics.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import nose
2+
import os
3+
import unittest
4+
5+
from pandas import Series, DataFrame
6+
import pandas.util.testing as tm
7+
8+
import numpy as np
9+
10+
class TestSeriesPlots(unittest.TestCase):
11+
12+
@classmethod
13+
def setUpClass(cls):
14+
import sys
15+
if 'IPython' in sys.modules:
16+
raise nose.SkipTest
17+
18+
try:
19+
import matplotlib as mpl
20+
mpl.use('Agg', warn=False)
21+
except ImportError:
22+
raise nose.SkipTest
23+
24+
def setUp(self):
25+
self.ts = tm.makeTimeSeries()
26+
self.ts.name = 'ts'
27+
28+
self.series = tm.makeStringSeries()
29+
self.series.name = 'series'
30+
31+
def test_plot(self):
32+
_check_plot_works(self.ts.plot, label='foo')
33+
_check_plot_works(self.ts.plot, use_index=False)
34+
_check_plot_works(self.ts.plot, rot=0)
35+
_check_plot_works(self.ts.plot, style='.')
36+
_check_plot_works(self.ts[:10].plot, kind='bar')
37+
_check_plot_works(self.series[:5].plot, kind='bar')
38+
39+
def test_hist(self):
40+
_check_plot_works(self.ts.hist)
41+
_check_plot_works(self.ts.hist, grid=False)
42+
43+
44+
class TestDataFramePlots(unittest.TestCase):
45+
46+
@classmethod
47+
def setUpClass(cls):
48+
import sys
49+
if 'IPython' in sys.modules:
50+
raise nose.SkipTest
51+
52+
try:
53+
import matplotlib as mpl
54+
mpl.use('Agg', warn=False)
55+
except ImportError:
56+
raise nose.SkipTest
57+
58+
def test_plot(self):
59+
pass
60+
61+
def test_plot_int_columns(self):
62+
df = DataFrame(np.random.randn(100, 4)).cumsum()
63+
_check_plot_works(df.plot, legend=True)
64+
65+
66+
PNG_PATH = 'tmp.png'
67+
68+
def _check_plot_works(f, *args, **kwargs):
69+
import matplotlib.pyplot as plt
70+
71+
fig = plt.gcf()
72+
plt.clf()
73+
ax = fig.add_subplot(211)
74+
f(*args, **kwargs)
75+
76+
ax = fig.add_subplot(212)
77+
kwargs['ax'] = ax
78+
f(*args, **kwargs)
79+
plt.savefig(PNG_PATH)
80+
os.remove(PNG_PATH)
81+
82+
if __name__ == '__main__':
83+
nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],
84+
exit=False)

pandas/tests/test_series.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import operator
66
import unittest
77

8+
import nose
9+
810
from numpy import nan
911
import numpy as np
1012

@@ -1377,7 +1379,6 @@ def test_dropna_preserve_name(self):
13771379
self.assertEquals(result.name, self.ts.name)
13781380

13791381
if __name__ == '__main__':
1380-
import nose
13811382
nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],
13821383
exit=False)
13831384

0 commit comments

Comments
 (0)