Skip to content

BUG: xticks unnecessarily rotated #34334

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 32 commits into from
Sep 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
4282445
adjust brackets in condition to rotate xticklabels
MarcoGorelli May 23, 2020
06e283f
check rotation in use_index cases too
MarcoGorelli May 23, 2020
2249626
revert empty line
MarcoGorelli May 23, 2020
aa8bd31
revert forced error
MarcoGorelli May 23, 2020
09f93bd
Merge remote-tracking branch 'upstream/master' into subplots-non-date
MarcoGorelli May 23, 2020
9c26c59
Merge remote-tracking branch 'upstream/master' into subplots-non-date
MarcoGorelli May 25, 2020
cdb6ce2
Update v1.1.0.rst
MarcoGorelli May 25, 2020
ea92292
add comment
MarcoGorelli Jun 14, 2020
d87f25f
Merge remote-tracking branch 'origin/subplots-non-date' into subplots…
MarcoGorelli Jun 14, 2020
3fc9930
add tests which cover all the conditions
MarcoGorelli Jun 14, 2020
85638c0
wip
MarcoGorelli Jun 14, 2020
76c3b8d
Merge remote-tracking branch 'upstream/master' into subplots-non-date
MarcoGorelli Jun 17, 2020
7e01df8
remove xfails
MarcoGorelli Jun 17, 2020
2ea78de
check rot in existing test
MarcoGorelli Jun 17, 2020
2358716
:art:
MarcoGorelli Jun 17, 2020
eacbe0a
add regular time series test
MarcoGorelli Jun 17, 2020
836dd0d
reword whatsnew
MarcoGorelli Jun 18, 2020
ea525ec
:art:
MarcoGorelli Jun 18, 2020
85c4dc8
move _size_of_fmt to module level func
MarcoGorelli Jun 18, 2020
645905d
regroup tests
MarcoGorelli Jun 18, 2020
c0c0e99
revert accidental change
MarcoGorelli Jun 18, 2020
fbff1bd
Merge branch 'master' into subplots-non-date
MarcoGorelli Jun 27, 2020
c897d57
Merge remote-tracking branch 'upstream/master' into subplots-non-date
MarcoGorelli Jul 17, 2020
bb5b080
typo
MarcoGorelli Jul 17, 2020
9bc93da
Merge remote-tracking branch 'upstream/master' into subplots-non-date
MarcoGorelli Aug 11, 2020
69a4c36
doc
MarcoGorelli Aug 11, 2020
b093d68
remove unrealtde
MarcoGorelli Aug 11, 2020
5746bd6
remove unrealtde
MarcoGorelli Aug 11, 2020
29de029
make whatsnew more readable
MarcoGorelli Aug 18, 2020
cc9aebf
Merge remote-tracking branch 'upstream/master' into subplots-non-date
MarcoGorelli Sep 4, 2020
ee4d9bd
Merge remote-tracking branch 'upstream/master' into subplots-non-date
MarcoGorelli Sep 13, 2020
7ef0382
:pencil2:
MarcoGorelli Sep 13, 2020
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.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ I/O
Plotting
^^^^^^^^

- Bug in :meth:`DataFrame.plot` was rotating xticklabels when ``subplots=True``, even if the x-axis wasn't an irregular time series (:issue:`29460`)
- Bug in :meth:`DataFrame.plot` where a marker letter in the ``style`` keyword sometimes causes a ``ValueError`` (:issue:`21003`)

Groupby/resample/rolling
Expand Down
10 changes: 7 additions & 3 deletions pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1231,11 +1231,15 @@ def get_label(i):
ax.xaxis.set_major_locator(FixedLocator(xticks))
ax.set_xticklabels(xticklabels)

# If the index is an irregular time series, then by default
# we rotate the tick labels. The exception is if there are
# subplots which don't share their x-axes, in which we case
# we don't rotate the ticklabels as by default the subplots
# would be too close together.
condition = (
not self._use_dynamic_x()
and data.index.is_all_dates
and not self.subplots
or (self.subplots and self.sharex)
and (data.index.is_all_dates and self.use_index)
and (not self.subplots or (self.subplots and self.sharex))
)

index_name = self._get_index_name()
Expand Down
28 changes: 27 additions & 1 deletion pandas/tests/plotting/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pandas._libs.tslibs import BaseOffset, to_offset
import pandas.util._test_decorators as td

from pandas import DataFrame, Index, NaT, Series, isna
from pandas import DataFrame, Index, NaT, Series, isna, to_datetime
import pandas._testing as tm
from pandas.core.indexes.datetimes import DatetimeIndex, bdate_range, date_range
from pandas.core.indexes.period import Period, PeriodIndex, period_range
Expand Down Expand Up @@ -1494,6 +1494,32 @@ def test_matplotlib_scatter_datetime64(self):
expected = "2017-12-12"
assert label.get_text() == expected

def test_check_xticks_rot(self):
# https://github.com/pandas-dev/pandas/issues/29460
# regular time series
x = to_datetime(["2020-05-01", "2020-05-02", "2020-05-03"])
df = DataFrame({"x": x, "y": [1, 2, 3]})
axes = df.plot(x="x", y="y")
self._check_ticks_props(axes, xrot=0)

# irregular time series
x = to_datetime(["2020-05-01", "2020-05-02", "2020-05-04"])
df = DataFrame({"x": x, "y": [1, 2, 3]})
axes = df.plot(x="x", y="y")
self._check_ticks_props(axes, xrot=30)

# use timeseries index or not
axes = df.set_index("x").plot(y="y", use_index=True)
self._check_ticks_props(axes, xrot=30)
axes = df.set_index("x").plot(y="y", use_index=False)
self._check_ticks_props(axes, xrot=0)

# separate subplots
axes = df.plot(x="x", y="y", subplots=True, sharex=True)
self._check_ticks_props(axes, xrot=30)
axes = df.plot(x="x", y="y", subplots=True, sharex=False)
self._check_ticks_props(axes, xrot=0)


def _check_plot_works(f, freq=None, series=None, *args, **kwargs):
import matplotlib.pyplot as plt
Expand Down
13 changes: 9 additions & 4 deletions pandas/tests/plotting/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ def _assert_xtickslabels_visibility(self, axes, expected):
for ax, exp in zip(axes, expected):
self._check_visible(ax.get_xticklabels(), visible=exp)

@pytest.mark.xfail(reason="Waiting for PR 34334", strict=True)
@pytest.mark.slow
def test_plot(self):
from pandas.plotting._matplotlib.compat import mpl_ge_3_1_0
Expand All @@ -66,6 +65,7 @@ def test_plot(self):

with tm.assert_produces_warning(UserWarning):
axes = _check_plot_works(df.plot, subplots=True, use_index=False)
self._check_ticks_props(axes, xrot=0)
self._check_axes_shape(axes, axes_num=4, layout=(4, 1))

df = DataFrame({"x": [1, 2], "y": [3, 4]})
Expand All @@ -78,7 +78,8 @@ def test_plot(self):

df = DataFrame(np.random.rand(10, 3), index=list(string.ascii_letters[:10]))

_check_plot_works(df.plot, use_index=True)
ax = _check_plot_works(df.plot, use_index=True)
self._check_ticks_props(ax, xrot=0)
_check_plot_works(df.plot, sort_columns=False)
_check_plot_works(df.plot, yticks=[1, 5, 10])
_check_plot_works(df.plot, xticks=[1, 5, 10])
Expand Down Expand Up @@ -110,7 +111,8 @@ def test_plot(self):

tuples = zip(string.ascii_letters[:10], range(10))
df = DataFrame(np.random.rand(10, 3), index=MultiIndex.from_tuples(tuples))
_check_plot_works(df.plot, use_index=True)
ax = _check_plot_works(df.plot, use_index=True)
self._check_ticks_props(ax, xrot=0)

# unicode
index = MultiIndex.from_tuples(
Expand Down Expand Up @@ -304,12 +306,14 @@ def test_xcompat(self):
ax = df.plot(x_compat=True)
lines = ax.get_lines()
assert not isinstance(lines[0].get_xdata(), PeriodIndex)
self._check_ticks_props(ax, xrot=30)

tm.close()
pd.plotting.plot_params["xaxis.compat"] = True
ax = df.plot()
lines = ax.get_lines()
assert not isinstance(lines[0].get_xdata(), PeriodIndex)
self._check_ticks_props(ax, xrot=30)

tm.close()
pd.plotting.plot_params["x_compat"] = False
Expand All @@ -325,12 +329,14 @@ def test_xcompat(self):
ax = df.plot()
lines = ax.get_lines()
assert not isinstance(lines[0].get_xdata(), PeriodIndex)
self._check_ticks_props(ax, xrot=30)

tm.close()
ax = df.plot()
lines = ax.get_lines()
assert not isinstance(lines[0].get_xdata(), PeriodIndex)
assert isinstance(PeriodIndex(lines[0].get_xdata()), PeriodIndex)
self._check_ticks_props(ax, xrot=0)

def test_period_compat(self):
# GH 9012
Expand Down Expand Up @@ -486,7 +492,6 @@ def test_groupby_boxplot_sharex(self):
expected = [False, False, True, True]
self._assert_xtickslabels_visibility(axes, expected)

@pytest.mark.xfail(reason="Waiting for PR 34334", strict=True)
@pytest.mark.slow
def test_subplots_timeseries(self):
idx = date_range(start="2014-07-01", freq="M", periods=10)
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/plotting/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def test_ts_area_lim(self):
line = ax.get_lines()[0].get_data(orig=False)[0]
assert xmin <= line[0]
assert xmax >= line[-1]
self._check_ticks_props(ax, xrot=0)
tm.close()

# GH 7471
Expand All @@ -118,6 +119,7 @@ def test_ts_area_lim(self):
line = ax.get_lines()[0].get_data(orig=False)[0]
assert xmin <= line[0]
assert xmax >= line[-1]
self._check_ticks_props(ax, xrot=30)
tm.close()

tz_ts = self.ts.copy()
Expand All @@ -128,6 +130,7 @@ def test_ts_area_lim(self):
line = ax.get_lines()[0].get_data(orig=False)[0]
assert xmin <= line[0]
assert xmax >= line[-1]
self._check_ticks_props(ax, xrot=0)
tm.close()

_, ax = self.plt.subplots()
Expand All @@ -136,6 +139,7 @@ def test_ts_area_lim(self):
line = ax.get_lines()[0].get_data(orig=False)[0]
assert xmin <= line[0]
assert xmax >= line[-1]
self._check_ticks_props(ax, xrot=0)

def test_label(self):
s = Series([1, 2])
Expand Down Expand Up @@ -284,6 +288,7 @@ def test_irregular_datetime(self):
xp = DatetimeConverter.convert(datetime(1999, 1, 1), "", ax)
ax.set_xlim("1/1/1999", "1/1/2001")
assert xp == ax.get_xlim()[0]
self._check_ticks_props(ax, xrot=30)

def test_unsorted_index_xlim(self):
ser = Series(
Expand Down