Skip to content

Commit 66690f2

Browse files
Chang Shewesm
Chang She
authored andcommitted
BUG/TST: plot irregular and reg freq on same subplot
1 parent c903094 commit 66690f2

File tree

3 files changed

+57
-14
lines changed

3 files changed

+57
-14
lines changed

pandas/tools/plotting.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -596,10 +596,13 @@ def __init__(self, data, **kwargs):
596596

597597
@property
598598
def has_ts_index(self):
599+
# TODO refactor this whole regular/irregular kludge
599600
from pandas.core.frame import DataFrame
600601
if isinstance(self.data, (Series, DataFrame)):
602+
ax, _ = self._get_ax_and_style(0)
601603
freq = (getattr(self.data.index, 'freq', None)
602-
or getattr(self.data.index, 'inferred_freq', None))
604+
or getattr(self.data.index, 'inferred_freq', None)
605+
or getattr(ax, 'freq', None))
603606
return (freq is not None) and self._has_dynamic_index_freq(freq)
604607
return False
605608

@@ -649,8 +652,12 @@ def _maybe_convert_index(self, data):
649652

650653
freq = get_period_alias(freq)
651654

652-
if freq is None and hasattr(data.index, 'inferred_freq'):
653-
freq = data.index.inferred_freq
655+
if freq is None:
656+
freq = getattr(data.index, 'inferred_freq', None)
657+
658+
if freq is None:
659+
ax, _ = self._get_ax_and_style(0)
660+
freq = getattr(ax, 'freq', None)
654661

655662
if isinstance(freq, DateOffset):
656663
freq = freq.rule_code

pandas/tseries/plotting.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,17 @@ def tsplot(series, plotf, **kwargs):
4444
"""
4545
# Used inferred freq is possible, need a test case for inferred
4646
freq = getattr(series.index, 'freq', None)
47-
if freq is None and hasattr(series.index, 'inferred_freq'):
48-
freq = series.index.inferred_freq
47+
if freq is None:
48+
freq = getattr(series.index, 'inferred_freq', None)
49+
50+
if 'ax' in kwargs:
51+
ax = kwargs.pop('ax')
52+
else:
53+
import matplotlib.pyplot as plt
54+
ax = plt.gca()
55+
56+
if freq is None:
57+
freq = getattr(ax, 'freq', None)
4958

5059
if isinstance(freq, DateOffset):
5160
freq = freq.rule_code
@@ -62,12 +71,6 @@ def tsplot(series, plotf, **kwargs):
6271

6372
style = kwargs.pop('style', None)
6473

65-
if 'ax' in kwargs:
66-
ax = kwargs.pop('ax')
67-
else:
68-
import matplotlib.pyplot as plt
69-
ax = plt.gca()
70-
7174
# Specialized ts plotting attributes for Axes
7275
ax.freq = freq
7376
xaxis = ax.get_xaxis()
@@ -93,12 +96,19 @@ def tsplot(series, plotf, **kwargs):
9396

9497
format_dateaxis(ax, ax.freq)
9598

96-
left = series.index[0] #get_datevalue(series.index[0], freq)
97-
right = series.index[-1] #get_datevalue(series.index[-1], freq)
99+
left, right = _get_xlim(ax.get_lines())
98100
ax.set_xlim(left, right)
99101

100102
return ax
101103

104+
def _get_xlim(lines):
105+
left, right = np.inf, -np.inf
106+
for l in lines:
107+
x = l.get_xdata()
108+
left = min(x[0].ordinal, left)
109+
right = max(x[-1].ordinal, right)
110+
return left, right
111+
102112
def get_datevalue(date, freq):
103113
if isinstance(date, Period):
104114
return date.asfreq(freq).ordinal

pandas/tseries/tests/test_plotting.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,10 @@ def test_finder_daily(self):
234234

235235
@slow
236236
def test_finder_quarterly(self):
237+
import matplotlib.pyplot as plt
237238
xp = Period('1988Q1').ordinal
238239
yrs = [3.5, 11]
240+
plt.close('all')
239241
for n in yrs:
240242
rng = period_range('1987Q2', periods=int(n * 4), freq='Q')
241243
ser = Series(np.random.randn(len(rng)), rng)
@@ -250,8 +252,10 @@ def test_finder_quarterly(self):
250252

251253
@slow
252254
def test_finder_monthly(self):
255+
import matplotlib.pyplot as plt
253256
xp = Period('1988-1').ordinal
254257
yrs = [1.15, 2.5, 4, 11]
258+
plt.close('all')
255259
for n in yrs:
256260
rng = period_range('1987Q2', periods=int(n * 12), freq='M')
257261
ser = Series(np.random.randn(len(rng)), rng)
@@ -263,8 +267,12 @@ def test_finder_monthly(self):
263267
ax.set_xlim(vmin + 0.9, vmax)
264268
rs = xaxis.get_majorticklocs()[0]
265269
self.assertEqual(xp, rs)
270+
plt.close('all')
266271

267-
272+
@slow
273+
def test_finder_monthly_long(self):
274+
import matplotlib.pyplot as plt
275+
plt.close('all')
268276
rng = period_range('1988Q1', periods=24*12, freq='M')
269277
ser = Series(np.random.randn(len(rng)), rng)
270278
ax = ser.plot()
@@ -433,6 +441,24 @@ def test_secondary_frame(self):
433441
self.assert_(axes[1].get_yaxis().get_ticks_position() == 'default')
434442
self.assert_(axes[2].get_yaxis().get_ticks_position() == 'right')
435443

444+
@slow
445+
def test_mixed_freq(self):
446+
import matplotlib.pyplot as plt
447+
plt.close('all')
448+
s1 = tm.makeTimeSeries()
449+
s2 = s1[[0, 5, 10, 11, 12, 13, 14, 15]]
450+
s1.plot()
451+
ax2 = s2.plot(style='g')
452+
lines = ax2.get_lines()
453+
idx1 = lines[0].get_xdata()
454+
idx2 = lines[1].get_xdata()
455+
self.assert_(idx1.equals(s1.index.to_period('B')))
456+
self.assert_(idx2.equals(s2.index.to_period('B')))
457+
left, right = ax2.get_xlim()
458+
pidx = s1.index.to_period()
459+
self.assert_(left == pidx[0].ordinal)
460+
self.assert_(right == pidx[-1].ordinal)
461+
436462
PNG_PATH = 'tmp.png'
437463
def _check_plot_works(f, freq=None, series=None, *args, **kwargs):
438464
import matplotlib.pyplot as plt

0 commit comments

Comments
 (0)