Skip to content

Commit 6a9eb51

Browse files
committed
TST: Add lineplot tests with unsorted x data
Two new tests check interaction of non-monotonic x data and xlims: test_frame / test_unsorted_index_lims test_series / test_unsorted_index_xlim
1 parent 328c7e1 commit 6a9eb51

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

pandas/tests/plotting/test_frame.py

+23
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,29 @@ def test_unsorted_index(self):
304304
rs = Series(rs[:, 1], rs[:, 0], dtype=np.int64, name='y')
305305
tm.assert_series_equal(rs, df.y)
306306

307+
def test_unsorted_index_lims(self):
308+
df = DataFrame({'y': [0., 1., 2., 3.]}, index=[1., 0., 3., 2.])
309+
ax = df.plot()
310+
xmin, xmax = ax.get_xlim()
311+
lines = ax.get_lines()
312+
assert xmin <= np.nanmin(lines[0].get_data()[0])
313+
assert xmax >= np.nanmax(lines[0].get_data()[0])
314+
315+
df = DataFrame({'y': [0., 1., np.nan, 3., 4., 5., 6.]},
316+
index=[1., 0., 3., 2., np.nan, 3., 2.])
317+
ax = df.plot()
318+
xmin, xmax = ax.get_xlim()
319+
lines = ax.get_lines()
320+
assert xmin <= np.nanmin(lines[0].get_data()[0])
321+
assert xmax >= np.nanmax(lines[0].get_data()[0])
322+
323+
df = DataFrame({'y': [0., 1., 2., 3.], 'z': [91., 90., 93., 92.]})
324+
ax = df.plot(x='z', y='y')
325+
xmin, xmax = ax.get_xlim()
326+
lines = ax.get_lines()
327+
assert xmin <= np.nanmin(lines[0].get_data()[0])
328+
assert xmax >= np.nanmax(lines[0].get_data()[0])
329+
307330
@pytest.mark.slow
308331
def test_subplots(self):
309332
df = DataFrame(np.random.rand(10, 3),

pandas/tests/plotting/test_series.py

+10
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,16 @@ def test_irregular_datetime(self):
279279
ax.set_xlim('1/1/1999', '1/1/2001')
280280
assert xp == ax.get_xlim()[0]
281281

282+
def test_unsorted_index_xlim(self):
283+
ser = Series([0., 1., np.nan, 3., 4., 5., 6.],
284+
index=[1., 0., 3., 2., np.nan, 3., 2.])
285+
_, ax = self.plt.subplots()
286+
ax = ser.plot(ax=ax)
287+
xmin, xmax = ax.get_xlim()
288+
lines = ax.get_lines()
289+
assert xmin <= np.nanmin(lines[0].get_data(orig=False)[0])
290+
assert xmax >= np.nanmax(lines[0].get_data(orig=False)[0])
291+
282292
@pytest.mark.slow
283293
def test_pie_series(self):
284294
# if sum of values is less than 1.0, pie handle them as rate and draw

0 commit comments

Comments
 (0)