Skip to content

BUG: Plotting use FixedLocator #26185

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 7 commits into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 11 additions & 2 deletions pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,13 @@ def get_label(i):

if self.orientation == 'vertical' or self.orientation is None:
if self._need_to_set_index:
xticklabels = [get_label(x) for x in ax.get_xticks()]
xticks = ax.get_xticks()
xticklabels = [get_label(x) for x in xticks]
ax.set_xticklabels(xticklabels)
from matplotlib.ticker import FixedLocator, FixedFormatter
ax.xaxis.set_major_locator(FixedLocator(xticks))
ax.xaxis.set_major_formatter(FixedFormatter(xticklabels))

self._apply_axis_properties(ax.xaxis, rot=self.rot,
fontsize=self.fontsize)
self._apply_axis_properties(ax.yaxis, fontsize=self.fontsize)
Expand All @@ -422,8 +427,12 @@ def get_label(i):

elif self.orientation == 'horizontal':
if self._need_to_set_index:
yticklabels = [get_label(y) for y in ax.get_yticks()]
yticks = ax.get_yticks()
yticklabels = [get_label(y) for y in yticks]
ax.set_yticklabels(yticklabels)
from matplotlib.ticker import FixedLocator, FixedFormatter
ax.xaxis.set_major_locator(FixedLocator(yticks))
ax.xaxis.set_major_formatter(FixedFormatter(yticklabels))

Choose a reason for hiding this comment

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

Why is the x axis given the y-tick(label)s here? Seems like an error to me.
Meaning the "horizontal" direction is not sufficiently tested.

Also note that ax.set_xticklabels(xticklabels) and ax.xaxis.set_major_formatter(FixedFormatter(xticklabels)) are redundant. One of those lines should be enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is definitly an error.
And this part is definitly not tested at all. None of the tests in plotting even hit this lines. This is because the self._need_to_set_index is only set to True for plot that have a self.orientation "vertical", so there is no way to go inside this loop.
What is the best course of action? Deleting this part that could never get hits?

Copy link
Member

Choose a reason for hiding this comment

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

If you have identified dead code you can submit a follow up PR to remove

self._apply_axis_properties(ax.yaxis, rot=self.rot,
fontsize=self.fontsize)
self._apply_axis_properties(ax.xaxis, fontsize=self.fontsize)
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/plotting/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2995,6 +2995,23 @@ def test_secondary_axis_font_size(self, method):
self._check_ticks_props(axes=ax.right_ax,
ylabelsize=fontsize)

@pytest.mark.slow
def test_x_string_values_ticks(self):
# GH: 7612
# GH: 22334
# GH: 15912
df = pd.DataFrame({'sales': [3, 2, 3],
'visits': [20, 42, 28],
'day': ['Monday', 'Tuesday', 'Wednesday']})
ax = df.plot.area(x='day')
ax.set_xlim(-1, 3)
xticklabels = [t.get_text() for t in ax.get_xticklabels()]
labels_position = dict(zip(xticklabels, ax.get_xticks()))
# Testing if the label stayed at the right position
assert labels_position['Monday'] == 0.0
assert labels_position['Tuesday'] == 1.0
assert labels_position['Wednesday'] == 2.0


def _generate_4_axes_via_gridspec():
import matplotlib.pyplot as plt
Expand Down