Skip to content

Commit 32b4710

Browse files
charlesdong1991TomAugspurger
authored andcommitted
BUG: Correct the previous bug fixing on xlim for plotting (#28059)
* Restore change in datetime like
1 parent e4c4b78 commit 32b4710

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

pandas/plotting/_matplotlib/core.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
from pandas.plotting._matplotlib.style import _get_standard_colors
3434
from pandas.plotting._matplotlib.tools import (
3535
_flatten,
36+
_get_all_lines,
37+
_get_xlim,
3638
_handle_shared_axes,
3739
_subplots,
3840
format_date_labels,
@@ -1099,8 +1101,13 @@ def _make_plot(self):
10991101
)
11001102
self._add_legend_handle(newlines[0], label, index=i)
11011103

1102-
# GH27686 set_xlim will truncate xaxis to fixed space
1103-
ax.relim()
1104+
if self._is_ts_plot():
1105+
1106+
# reset of xlim should be used for ts data
1107+
# TODO: GH28021, should find a way to change view limit on xaxis
1108+
lines = _get_all_lines(ax)
1109+
left, right = _get_xlim(lines)
1110+
ax.set_xlim(left, right)
11041111

11051112
@classmethod
11061113
def _plot(cls, ax, x, y, style=None, column_num=None, stacking_id=None, **kwds):

pandas/plotting/_matplotlib/tools.py

+21
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,24 @@ def _set_ticks_props(axes, xlabelsize=None, xrot=None, ylabelsize=None, yrot=Non
356356
if yrot is not None:
357357
plt.setp(ax.get_yticklabels(), rotation=yrot)
358358
return axes
359+
360+
361+
def _get_all_lines(ax):
362+
lines = ax.get_lines()
363+
364+
if hasattr(ax, "right_ax"):
365+
lines += ax.right_ax.get_lines()
366+
367+
if hasattr(ax, "left_ax"):
368+
lines += ax.left_ax.get_lines()
369+
370+
return lines
371+
372+
373+
def _get_xlim(lines):
374+
left, right = np.inf, -np.inf
375+
for l in lines:
376+
x = l.get_xdata(orig=False)
377+
left = min(np.nanmin(x), left)
378+
right = max(np.nanmax(x), right)
379+
return left, right

pandas/tests/plotting/test_datetimelike.py

+7-17
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,6 @@ def test_get_finder(self):
419419
assert conv.get_finder("A") == conv._annual_finder
420420
assert conv.get_finder("W") == conv._daily_finder
421421

422-
# TODO: The finder should be retested due to wrong xlim values on x-axis
423-
@pytest.mark.xfail(reason="TODO: check details in GH28021")
424422
@pytest.mark.slow
425423
def test_finder_daily(self):
426424
day_lst = [10, 40, 252, 400, 950, 2750, 10000]
@@ -444,8 +442,6 @@ def test_finder_daily(self):
444442
assert rs1 == xpl1
445443
assert rs2 == xpl2
446444

447-
# TODO: The finder should be retested due to wrong xlim values on x-axis
448-
@pytest.mark.xfail(reason="TODO: check details in GH28021")
449445
@pytest.mark.slow
450446
def test_finder_quarterly(self):
451447
yrs = [3.5, 11]
@@ -469,8 +465,6 @@ def test_finder_quarterly(self):
469465
assert rs1 == xpl1
470466
assert rs2 == xpl2
471467

472-
# TODO: The finder should be retested due to wrong xlim values on x-axis
473-
@pytest.mark.xfail(reason="TODO: check details in GH28021")
474468
@pytest.mark.slow
475469
def test_finder_monthly(self):
476470
yrs = [1.15, 2.5, 4, 11]
@@ -504,8 +498,6 @@ def test_finder_monthly_long(self):
504498
xp = Period("1989Q1", "M").ordinal
505499
assert rs == xp
506500

507-
# TODO: The finder should be retested due to wrong xlim values on x-axis
508-
@pytest.mark.xfail(reason="TODO: check details in GH28021")
509501
@pytest.mark.slow
510502
def test_finder_annual(self):
511503
xp = [1987, 1988, 1990, 1990, 1995, 2020, 2070, 2170]
@@ -530,7 +522,7 @@ def test_finder_minutely(self):
530522
_, ax = self.plt.subplots()
531523
ser.plot(ax=ax)
532524
xaxis = ax.get_xaxis()
533-
rs = xaxis.get_majorticklocs()[1]
525+
rs = xaxis.get_majorticklocs()[0]
534526
xp = Period("1/1/1999", freq="Min").ordinal
535527

536528
assert rs == xp
@@ -542,7 +534,7 @@ def test_finder_hourly(self):
542534
_, ax = self.plt.subplots()
543535
ser.plot(ax=ax)
544536
xaxis = ax.get_xaxis()
545-
rs = xaxis.get_majorticklocs()[1]
537+
rs = xaxis.get_majorticklocs()[0]
546538
xp = Period("1/1/1999", freq="H").ordinal
547539

548540
assert rs == xp
@@ -1418,9 +1410,7 @@ def test_plot_outofbounds_datetime(self):
14181410

14191411
def test_format_timedelta_ticks_narrow(self):
14201412

1421-
expected_labels = [
1422-
"00:00:00.0000000{:0>2d}".format(i) for i in np.arange(0, 10, 2)
1423-
]
1413+
expected_labels = ["00:00:00.0000000{:0>2d}".format(i) for i in np.arange(10)]
14241414

14251415
rng = timedelta_range("0", periods=10, freq="ns")
14261416
df = DataFrame(np.random.randn(len(rng), 3), rng)
@@ -1430,8 +1420,8 @@ def test_format_timedelta_ticks_narrow(self):
14301420
labels = ax.get_xticklabels()
14311421

14321422
result_labels = [x.get_text() for x in labels]
1433-
assert (len(result_labels) - 2) == len(expected_labels)
1434-
assert result_labels[1:-1] == expected_labels
1423+
assert len(result_labels) == len(expected_labels)
1424+
assert result_labels == expected_labels
14351425

14361426
def test_format_timedelta_ticks_wide(self):
14371427
expected_labels = [
@@ -1454,8 +1444,8 @@ def test_format_timedelta_ticks_wide(self):
14541444
labels = ax.get_xticklabels()
14551445

14561446
result_labels = [x.get_text() for x in labels]
1457-
assert (len(result_labels) - 2) == len(expected_labels)
1458-
assert result_labels[1:-1] == expected_labels
1447+
assert len(result_labels) == len(expected_labels)
1448+
assert result_labels == expected_labels
14591449

14601450
def test_timedelta_plot(self):
14611451
# test issue #8711

0 commit comments

Comments
 (0)