diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index e392a67ce3c16c..98b109441103ec 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -1305,15 +1305,13 @@ def _post_plot_logic(self, ax, data): s_edge = self.ax_pos[0] - 0.25 + self.lim_offset e_edge = self.ax_pos[-1] + 0.25 + self.bar_width + self.lim_offset - ax.set_xlim((s_edge, e_edge)) - if name is not None and self.use_index: - ax.set_xlabel(name) + self._decorate_ticks(ax, name, self.ax_index, s_edge, e_edge) def _decorate_ticks(self, ax, name, ticklabels, start_edge, end_edge): ax.set_xlim((start_edge, end_edge)) - ax.set_xticks(self.tick_pos) - ax.set_xticklabels(ticklabels) + ax.set_xticks(self.ax_index) + # ax.set_xticklabels(ticklabels) if name is not None and self.use_index: ax.set_xlabel(name) @@ -1825,7 +1823,6 @@ def _plot(data, x=None, y=None, subplots=False, plot_obj.generate() plot_obj.draw() - return plot_obj.result diff --git a/pandas/tests/plotting/mytest.py b/pandas/tests/plotting/mytest.py index 3cafcad0f5e97a..564374d948eebc 100644 --- a/pandas/tests/plotting/mytest.py +++ b/pandas/tests/plotting/mytest.py @@ -2,22 +2,27 @@ import pandas as pd import matplotlib.pyplot as plt -data = {"A":0, "B":3, "C":-4} -df = pd.DataFrame.from_dict(data, orient = "index", columns = ["Value"]) -fig = plt.figure() -ax = plt.gca() -ax = df.plot.bar(ax = ax) - -df2 = df.sort_values("Value") * - 2 -df2.plot.bar(ax = ax, color = "red") +data = {"A": 0, "B": 3, "C": -4} +df = pd.DataFrame.from_dict(data, orient="index", columns=["Value"]) +ax = df.plot.bar() +ax.get_figure().canvas.draw() +xticklabels = [t.get_text() for t in ax.get_xticklabels()] +label_positions_1 = dict(zip(xticklabels, ax.get_xticks())) +df = df.sort_values("Value") * - 2 +df.plot.bar(ax=ax, color="red") +ax.get_figure().canvas.draw() +xticklabels = [t.get_text() for t in ax.get_xticklabels()] +label_positions_2 = dict(zip(xticklabels, ax.get_xticks())) +assert label_positions_1 == label_positions_2 plt.show() -fig = plt.figure(num='purepyplot') -plt.bar(df.index.values, df.Value.values) -ax = plt.gca() -ax.bar(df2.index.values, df2.Value.values) -plt.show() -import tests.plotting.test_frame as tf -T = tf.TestDataFramePlots() -T.test_bar_align_single_column() +# fig = plt.figure(num='purepyplot') +# plt.bar(df.index.values, df.Value.values) +# ax = plt.gca() +# ax.bar(df2.index.values, df2.Value.values) +# plt.show() +# +# import tests.plotting.test_frame as tf +# T = tf.TestDataFramePlots() +# T.test_bar_align_single_column() diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index f9d7bbfa0713f8..e4f66b49da0a25 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -3032,6 +3032,23 @@ def test_x_multiindex_values_ticks(self): assert labels_position['(2013, 1)'] == 2.0 assert labels_position['(2013, 2)'] == 3.0 + @pytest.mark.slow + def test_bar_ticklabel_consistence(self): + # Draw two consecutiv bar plot with consistent ticklabels + # GH: 26186 + data = {"A": 0, "B": 3, "C": -4} + df = pd.DataFrame.from_dict(data, orient="index", columns=["Value"]) + ax = df.plot.bar() + ax.get_figure().canvas.draw() + xticklabels = [t.get_text() for t in ax.get_xticklabels()] + label_positions_1 = dict(zip(xticklabels, ax.get_xticks())) + df = df.sort_values("Value") * - 2 + df.plot.bar(ax=ax, color="red") + ax.get_figure().canvas.draw() + xticklabels = [t.get_text() for t in ax.get_xticklabels()] + label_positions_2 = dict(zip(xticklabels, ax.get_xticks())) + assert label_positions_1 == label_positions_2 + def _generate_4_axes_via_gridspec(): import matplotlib.pyplot as plt