Skip to content

BUG: bar and line plots are not aligned on the x-axis/xticks #56653

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

Closed
wants to merge 8 commits into from
Closed
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/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,7 @@ Plotting
^^^^^^^^
- Bug in :meth:`DataFrame.plot.box` with ``vert=False`` and a Matplotlib ``Axes`` created with ``sharey=True`` (:issue:`54941`)
- Bug in :meth:`DataFrame.plot.scatter` discarding string columns (:issue:`56142`)
- Bug in :meth:`DataFrame.plot` where bar and line plots are not aligned on the x-axis (:issue:`56611`)
- Bug in :meth:`Series.plot` when reusing an ``ax`` object failing to raise when a ``how`` keyword is passed (:issue:`55953`)

Groupby/resample/rolling
Expand Down
10 changes: 9 additions & 1 deletion pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1804,6 +1804,13 @@ def _kind(self) -> Literal["bar", "barh"]:
def orientation(self) -> PlottingOrientation:
return "vertical"

@final
def _set_tick_pos(self, data) -> np.ndarray:
if self._is_series and is_integer_dtype(data.index):
return np.array(data.index)
else:
return np.arange(len(data))

def __init__(
self,
data,
Expand All @@ -1822,7 +1829,6 @@ def __init__(
self.bar_width = width
self._align = align
self._position = position
self.tick_pos = np.arange(len(data))

if is_list_like(bottom):
bottom = np.array(bottom)
Expand All @@ -1835,6 +1841,8 @@ def __init__(

MPLPlot.__init__(self, data, **kwargs)

self.tick_pos = self._set_tick_pos(data)

@cache_readonly
def ax_pos(self) -> np.ndarray:
return self.tick_pos - self.tickoffset
Expand Down
30 changes: 26 additions & 4 deletions pandas/tests/plotting/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,10 +919,7 @@ def test_plot_order(self, data, index):
ax = ser.plot(kind="bar")

expected = ser.tolist()
result = [
patch.get_bbox().ymax
for patch in sorted(ax.patches, key=lambda patch: patch.get_bbox().xmax)
]
result = [patch.get_bbox().ymax for patch in ax.patches]
Copy link
Contributor Author

@sharonwoo sharonwoo Jan 4, 2024

Choose a reason for hiding this comment

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

as is, this test expects [1,2,3,4] and subsequently [10, 50, 20, 30]. it actually passes on both current BarPlot and PR implementations.

but on the actual displayed graph, the xticks are sorted in ascending order. to investigate what's causing this behaviour and to fix the test (or create a new one to test fixed logic)

assert expected == result

def test_style_single_ok(self):
Expand Down Expand Up @@ -977,6 +974,31 @@ def test_series_none_color(self):
expected = _unpack_cycler(mpl.pyplot.rcParams)[:1]
_check_colors(ax.get_lines(), linecolors=expected)

def test_bar_plot_x_axis(self):
df = DataFrame(
{
"bars": {-1: 0.5, 0: 1.0, 1: 3.0, 2: 3.5, 3: 1.5},
"pct": {-1: 1.0, 0: 2.0, 1: 3.0, 2: 4.0, 3: 8.0},
}
)
ax_bar = df["bars"].plot(kind="bar")
df["pct"].plot(kind="line")
actual_bar_x = [ax.get_x() + ax.get_width() / 2.0 for ax in ax_bar.patches]
Copy link
Member

Choose a reason for hiding this comment

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

This part of the code base is not my area of expertise but what does ax.get_x() return here? I'm a bit surprised we have to do this much work to get the labels

expected_x = [-1, 0, 1, 2, 3]
assert actual_bar_x == expected_x

def test_non_numeric_bar_plot_x_axis(self):
df = DataFrame(
{
"bars": {"a": 0.5, "b": 1.0},
"pct": {"a": 4.0, "b": 2.0},
}
)
ax_bar = df["bars"].plot(kind="bar")
actual_bar_x = [ax.get_x() + ax.get_width() / 2.0 for ax in ax_bar.patches]
expected_x = [0, 1]
assert actual_bar_x == expected_x

@pytest.mark.slow
def test_plot_no_warning(self, ts):
# GH 55138
Expand Down