Skip to content

Commit

Permalink
Add test for issue pandas-dev#26186
Browse files Browse the repository at this point in the history
  • Loading branch information
nrebena committed May 1, 2019
1 parent 5f438ff commit 5b80652
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
9 changes: 3 additions & 6 deletions pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -1825,7 +1823,6 @@ def _plot(data, x=None, y=None, subplots=False,

plot_obj.generate()
plot_obj.draw()

return plot_obj.result


Expand Down
37 changes: 21 additions & 16 deletions pandas/tests/plotting/mytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
17 changes: 17 additions & 0 deletions pandas/tests/plotting/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5b80652

Please sign in to comment.