Skip to content

Commit 5b80652

Browse files
committed
Add test for issue pandas-dev#26186
1 parent 5f438ff commit 5b80652

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

pandas/plotting/_core.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,15 +1305,13 @@ def _post_plot_logic(self, ax, data):
13051305

13061306
s_edge = self.ax_pos[0] - 0.25 + self.lim_offset
13071307
e_edge = self.ax_pos[-1] + 0.25 + self.bar_width + self.lim_offset
1308-
ax.set_xlim((s_edge, e_edge))
13091308

1310-
if name is not None and self.use_index:
1311-
ax.set_xlabel(name)
1309+
self._decorate_ticks(ax, name, self.ax_index, s_edge, e_edge)
13121310

13131311
def _decorate_ticks(self, ax, name, ticklabels, start_edge, end_edge):
13141312
ax.set_xlim((start_edge, end_edge))
1315-
ax.set_xticks(self.tick_pos)
1316-
ax.set_xticklabels(ticklabels)
1313+
ax.set_xticks(self.ax_index)
1314+
# ax.set_xticklabels(ticklabels)
13171315
if name is not None and self.use_index:
13181316
ax.set_xlabel(name)
13191317

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

18261824
plot_obj.generate()
18271825
plot_obj.draw()
1828-
18291826
return plot_obj.result
18301827

18311828

pandas/tests/plotting/mytest.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,27 @@
22
import pandas as pd
33
import matplotlib.pyplot as plt
44

5-
data = {"A":0, "B":3, "C":-4}
6-
df = pd.DataFrame.from_dict(data, orient = "index", columns = ["Value"])
7-
fig = plt.figure()
8-
ax = plt.gca()
9-
ax = df.plot.bar(ax = ax)
10-
11-
df2 = df.sort_values("Value") * - 2
12-
df2.plot.bar(ax = ax, color = "red")
5+
data = {"A": 0, "B": 3, "C": -4}
6+
df = pd.DataFrame.from_dict(data, orient="index", columns=["Value"])
7+
ax = df.plot.bar()
8+
ax.get_figure().canvas.draw()
9+
xticklabels = [t.get_text() for t in ax.get_xticklabels()]
10+
label_positions_1 = dict(zip(xticklabels, ax.get_xticks()))
11+
df = df.sort_values("Value") * - 2
12+
df.plot.bar(ax=ax, color="red")
13+
ax.get_figure().canvas.draw()
14+
xticklabels = [t.get_text() for t in ax.get_xticklabels()]
15+
label_positions_2 = dict(zip(xticklabels, ax.get_xticks()))
16+
assert label_positions_1 == label_positions_2
1317
plt.show()
1418

15-
fig = plt.figure(num='purepyplot')
16-
plt.bar(df.index.values, df.Value.values)
17-
ax = plt.gca()
18-
ax.bar(df2.index.values, df2.Value.values)
19-
plt.show()
2019

21-
import tests.plotting.test_frame as tf
22-
T = tf.TestDataFramePlots()
23-
T.test_bar_align_single_column()
20+
# fig = plt.figure(num='purepyplot')
21+
# plt.bar(df.index.values, df.Value.values)
22+
# ax = plt.gca()
23+
# ax.bar(df2.index.values, df2.Value.values)
24+
# plt.show()
25+
#
26+
# import tests.plotting.test_frame as tf
27+
# T = tf.TestDataFramePlots()
28+
# T.test_bar_align_single_column()

pandas/tests/plotting/test_frame.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3032,6 +3032,23 @@ def test_x_multiindex_values_ticks(self):
30323032
assert labels_position['(2013, 1)'] == 2.0
30333033
assert labels_position['(2013, 2)'] == 3.0
30343034

3035+
@pytest.mark.slow
3036+
def test_bar_ticklabel_consistence(self):
3037+
# Draw two consecutiv bar plot with consistent ticklabels
3038+
# GH: 26186
3039+
data = {"A": 0, "B": 3, "C": -4}
3040+
df = pd.DataFrame.from_dict(data, orient="index", columns=["Value"])
3041+
ax = df.plot.bar()
3042+
ax.get_figure().canvas.draw()
3043+
xticklabels = [t.get_text() for t in ax.get_xticklabels()]
3044+
label_positions_1 = dict(zip(xticklabels, ax.get_xticks()))
3045+
df = df.sort_values("Value") * - 2
3046+
df.plot.bar(ax=ax, color="red")
3047+
ax.get_figure().canvas.draw()
3048+
xticklabels = [t.get_text() for t in ax.get_xticklabels()]
3049+
label_positions_2 = dict(zip(xticklabels, ax.get_xticks()))
3050+
assert label_positions_1 == label_positions_2
3051+
30353052

30363053
def _generate_4_axes_via_gridspec():
30373054
import matplotlib.pyplot as plt

0 commit comments

Comments
 (0)