Skip to content

PLT: plot('line') or plot('area') produces wrong xlim in xaxis in 0.25.0 #27993

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 17 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from 15 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ Plotting
- Added a pandas_plotting_backends entrypoint group for registering plot backends. See :ref:`extending.plotting-backends` for more (:issue:`26747`).
- Fix compatibility issue with matplotlib when passing a pandas ``Index`` to a plot call (:issue:`27775`).
-
- BUG in :meth:`DataFrame.plot('line')` produces wrong xlim in xaxis in 0.25.0 (:issue:`27686`, :issue:`25160`)

Groupby/resample/rolling
^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
7 changes: 2 additions & 5 deletions pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
from pandas.plotting._matplotlib.style import _get_standard_colors
from pandas.plotting._matplotlib.tools import (
_flatten,
_get_all_lines,
_get_xlim,
_handle_shared_axes,
_subplots,
format_date_labels,
Expand Down Expand Up @@ -1101,9 +1099,8 @@ def _make_plot(self):
)
self._add_legend_handle(newlines[0], label, index=i)

lines = _get_all_lines(ax)
left, right = _get_xlim(lines)
ax.set_xlim(left, right)
# GH27686 set_xlim will truncate xaxis to fixed space
ax.relim()

@classmethod
def _plot(cls, ax, x, y, style=None, column_num=None, stacking_id=None, **kwds):
Expand Down
21 changes: 0 additions & 21 deletions pandas/plotting/_matplotlib/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,27 +343,6 @@ def _flatten(axes):
return np.array(axes)


def _get_all_lines(ax):
lines = ax.get_lines()

if hasattr(ax, "right_ax"):
lines += ax.right_ax.get_lines()

if hasattr(ax, "left_ax"):
lines += ax.left_ax.get_lines()

return lines


def _get_xlim(lines):
left, right = np.inf, -np.inf
for l in lines:
x = l.get_xdata(orig=False)
left = min(np.nanmin(x), left)
right = max(np.nanmax(x), right)
return left, right


def _set_ticks_props(axes, xlabelsize=None, xrot=None, ylabelsize=None, yrot=None):
import matplotlib.pyplot as plt

Expand Down
20 changes: 13 additions & 7 deletions pandas/tests/plotting/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ def test_get_finder(self):
assert conv.get_finder("A") == conv._annual_finder
assert conv.get_finder("W") == conv._daily_finder

@pytest.mark.xfail # I am not sure if this test is correct
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you expand on this? Perhaps open a new issue with the description of the bug, and reference it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you post images of the output for one of the failing tests, before and after your changes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomAugspurger yes, i feel the best way is to xfail the tests here and open up a new issue and do a follow-up PR to fix the tests, Because the main purpose is to test if the finder is being correctly used or not. And I honestly do not understand especially the second one in which there is a vmin + 0.9, I assume that's because of this wrong xaxis output, so has to manually add a constant to match the behavior.

Copy link
Member Author

@charlesdong1991 charlesdong1991 Aug 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After my change: (this is also aligned with the behaviour on matplotlib)

Screen Shot 2019-08-19 at 6 20 18 PM

Before my change:(i feel this is a bit wrong, because plot should start from 1987Q2, while due to this error, it starts from Q3 IIUC)

Screen Shot 2019-08-19 at 6 18 23 PM

@TomAugspurger

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks... I think I agree with your assessment.

Can you open a new issue to discuss removing / updating those tests? And reference that in the skip / xfails?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, i was thinking of doing it, but then i thought this PR would become a blocker for the issue if it is not merged because the test has to use the output of this PR. Probably I just overthink it ^^ thanks, will open and reference it. @TomAugspurger

@pytest.mark.slow
def test_finder_daily(self):
day_lst = [10, 40, 252, 400, 950, 2750, 10000]
Expand All @@ -442,6 +443,7 @@ def test_finder_daily(self):
assert rs1 == xpl1
assert rs2 == xpl2

@pytest.mark.xfail # I am not sure if this test is correct
@pytest.mark.slow
def test_finder_quarterly(self):
yrs = [3.5, 11]
Expand All @@ -465,6 +467,7 @@ def test_finder_quarterly(self):
assert rs1 == xpl1
assert rs2 == xpl2

@pytest.mark.xfail # I am not sure if this test is correct
@pytest.mark.slow
def test_finder_monthly(self):
yrs = [1.15, 2.5, 4, 11]
Expand Down Expand Up @@ -498,6 +501,7 @@ def test_finder_monthly_long(self):
xp = Period("1989Q1", "M").ordinal
assert rs == xp

@pytest.mark.xfail # I am not sure if this test is correct
@pytest.mark.slow
def test_finder_annual(self):
xp = [1987, 1988, 1990, 1990, 1995, 2020, 2070, 2170]
Expand All @@ -522,7 +526,7 @@ def test_finder_minutely(self):
_, ax = self.plt.subplots()
ser.plot(ax=ax)
xaxis = ax.get_xaxis()
rs = xaxis.get_majorticklocs()[0]
rs = xaxis.get_majorticklocs()[1]
xp = Period("1/1/1999", freq="Min").ordinal

assert rs == xp
Expand All @@ -534,7 +538,7 @@ def test_finder_hourly(self):
_, ax = self.plt.subplots()
ser.plot(ax=ax)
xaxis = ax.get_xaxis()
rs = xaxis.get_majorticklocs()[0]
rs = xaxis.get_majorticklocs()[1]
xp = Period("1/1/1999", freq="H").ordinal

assert rs == xp
Expand Down Expand Up @@ -1410,7 +1414,9 @@ def test_plot_outofbounds_datetime(self):

def test_format_timedelta_ticks_narrow(self):

expected_labels = ["00:00:00.0000000{:0>2d}".format(i) for i in range(10)]
expected_labels = [
"00:00:00.0000000{:0>2d}".format(i) for i in np.arange(0, 10, 2)
]

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

result_labels = [x.get_text() for x in labels]
assert len(result_labels) == len(expected_labels)
assert result_labels == expected_labels
assert (len(result_labels) - 2) == len(expected_labels)
assert result_labels[1:-1] == expected_labels

def test_format_timedelta_ticks_wide(self):
expected_labels = [
Expand All @@ -1444,8 +1450,8 @@ def test_format_timedelta_ticks_wide(self):
labels = ax.get_xticklabels()

result_labels = [x.get_text() for x in labels]
assert len(result_labels) == len(expected_labels)
assert result_labels == expected_labels
assert (len(result_labels) - 2) == len(expected_labels)
assert result_labels[1:-1] == expected_labels

def test_timedelta_plot(self):
# test issue #8711
Expand Down
52 changes: 52 additions & 0 deletions pandas/tests/plotting/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3177,6 +3177,58 @@ def test_x_multiindex_values_ticks(self):
assert labels_position["(2013, 1)"] == 2.0
assert labels_position["(2013, 2)"] == 3.0

@pytest.mark.parametrize("kind", ["line", "area"])
def test_xlim_plot_line(self, kind):
# test if xlim is set correctly in plot.line and plot.area
# GH 27686
df = pd.DataFrame([2, 4], index=[1, 2])
ax = df.plot(kind=kind)
xlims = ax.get_xlim()
assert xlims[0] < 1
assert xlims[1] > 2

def test_xlim_plot_line_correctly_in_mixed_plot_type(self):
# test if xlim is set correctly when ax contains multiple different kinds
# of plots, GH 27686
fig, ax = self.plt.subplots()

indexes = ["k1", "k2", "k3", "k4"]
df = pd.DataFrame(
{
"s1": [1000, 2000, 1500, 2000],
"s2": [900, 1400, 2000, 3000],
"s3": [1500, 1500, 1600, 1200],
"secondary_y": [1, 3, 4, 3],
},
index=indexes,
)
df[["s1", "s2", "s3"]].plot.bar(ax=ax, stacked=False)
df[["secondary_y"]].plot(ax=ax, secondary_y=True)

xlims = ax.get_xlim()
assert xlims[0] < 0
assert xlims[1] > 3

# make sure axis labels are plotted correctly as well
xticklabels = [t.get_text() for t in ax.get_xticklabels()]
assert xticklabels == indexes

def test_subplots_sharex_false(self):
# test when sharex is set to False, two plots should have different
# labels, GH 25160
df = pd.DataFrame(np.random.rand(10, 2))
df.iloc[5:, 1] = np.nan
df.iloc[:5, 0] = np.nan

figs, axs = self.plt.subplots(2, 1)
df.plot.line(ax=axs, subplots=True, sharex=False)

expected_ax1 = np.arange(4.5, 10, 0.5)
expected_ax2 = np.arange(-0.5, 5, 0.5)

tm.assert_numpy_array_equal(axs[0].get_xticks(), expected_ax1)
tm.assert_numpy_array_equal(axs[1].get_xticks(), expected_ax2)


def _generate_4_axes_via_gridspec():
import matplotlib.pyplot as plt
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/plotting/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -897,3 +897,15 @@ def test_plot_accessor_updates_on_inplace(self):
_, ax = self.plt.subplots()
after = ax.xaxis.get_ticklocs()
tm.assert_numpy_array_equal(before, after)

@pytest.mark.parametrize("kind", ["line", "area"])
def test_plot_xlim_for_series(self, kind):
# test if xlim is also correctly plotted in Series for line and area
# GH 27686
s = Series([2, 3])
_, ax = self.plt.subplots()
s.plot(kind=kind, ax=ax)
xlims = ax.get_xlim()

assert xlims[0] < 0
assert xlims[1] > 1