Skip to content

BUG: Make xticks from _quarterly_finder() line up better #47602

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 15 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all 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/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,7 @@ Plotting
- Bug in :meth:`DataFrame.plot.scatter` that prevented specifying ``norm`` (:issue:`45809`)
- The function :meth:`DataFrame.plot.scatter` now accepts ``color`` as an alias for ``c`` and ``size`` as an alias for ``s`` for consistency to other plotting functions (:issue:`44670`)
- Fix showing "None" as ylabel in :meth:`Series.plot` when not setting ylabel (:issue:`46129`)
- Bug in :meth:`DataFrame.plot` that led to xticks and vertical grids being improperly placed when plotting a quarterly series (:issue:`47602`)

Groupby/resample/rolling
^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
3 changes: 2 additions & 1 deletion pandas/plotting/_matplotlib/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,8 @@ def _quarterly_finder(vmin, vmax, freq):
info_fmt[year_start] = "%F"

else:
years = dates_[year_start] // 4 + 1
# https://github.com/pandas-dev/pandas/pull/47602
years = dates_[year_start] // 4 + 1970
nyears = span / periodsperyear
(min_anndef, maj_anndef) = _get_default_annual_spacing(nyears)
major_idx = year_start[(years % maj_anndef == 0)]
Expand Down
31 changes: 31 additions & 0 deletions pandas/tests/plotting/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
from pandas import (
Index,
Period,
PeriodIndex,
Series,
Timestamp,
arrays,
date_range,
)
import pandas._testing as tm
Expand Down Expand Up @@ -375,3 +377,32 @@ def get_view_interval(self):
tdc = converter.TimeSeries_TimedeltaFormatter()
monkeypatch.setattr(tdc, "axis", mock_axis())
tdc(0.0, 0)


@pytest.mark.parametrize("year_span", [11.25, 30, 80, 150, 400, 800, 1500, 2500, 3500])
# The range is limited to 11.25 at the bottom by if statements in
# the _quarterly_finder() function
def test_quarterly_finder(year_span):
vmin = -1000
vmax = vmin + year_span * 4
span = vmax - vmin + 1
if span < 45: # the quarterly finder is only invoked if the span is >= 45
return
nyears = span / 4
(min_anndef, maj_anndef) = converter._get_default_annual_spacing(nyears)
result = converter._quarterly_finder(vmin, vmax, "Q")
quarters = PeriodIndex(
arrays.PeriodArray(np.array([x[0] for x in result]), freq="Q")
)
majors = np.array([x[1] for x in result])
minors = np.array([x[2] for x in result])
major_quarters = quarters[majors]
minor_quarters = quarters[minors]
check_major_years = major_quarters.year % maj_anndef == 0
check_minor_years = minor_quarters.year % min_anndef == 0
check_major_quarters = major_quarters.quarter == 1
check_minor_quarters = minor_quarters.quarter == 1
assert np.all(check_major_years)
assert np.all(check_minor_years)
assert np.all(check_major_quarters)
assert np.all(check_minor_quarters)