Skip to content

Commit e2d1ebe

Browse files
committed
Add test for issue pandas-dev#26186
1 parent c0a3a86 commit e2d1ebe

File tree

3 files changed

+40
-25
lines changed

3 files changed

+40
-25
lines changed

pandas/plotting/_core.py

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

12981298
s_edge = self.ax_pos[0] - 0.25 + self.lim_offset
12991299
e_edge = self.ax_pos[-1] + 0.25 + self.bar_width + self.lim_offset
1300-
ax.set_xlim((s_edge, e_edge))
13011300

1302-
if name is not None and self.use_index:
1303-
ax.set_xlabel(name)
1301+
self._decorate_ticks(ax, name, self.ax_index, s_edge, e_edge)
13041302

13051303
def _decorate_ticks(self, ax, name, ticklabels, start_edge, end_edge):
13061304
ax.set_xlim((start_edge, end_edge))
1307-
ax.set_xticks(self.tick_pos)
1308-
ax.set_xticklabels(ticklabels)
1305+
ax.set_xticks(self.ax_index)
1306+
# ax.set_xticklabels(ticklabels)
13091307
if name is not None and self.use_index:
13101308
ax.set_xlabel(name)
13111309

@@ -1817,7 +1815,6 @@ def _plot(data, x=None, y=None, subplots=False,
18171815

18181816
plot_obj.generate()
18191817
plot_obj.draw()
1820-
18211818
return plot_obj.result
18221819

18231820

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: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,9 +1341,6 @@ def _check_bar_alignment(self, df, kind='bar', stacked=False,
13411341
raise ValueError
13421342

13431343
# Check the ticks locates on integer
1344-
print(axis.get_ticklocs())
1345-
print([x.get_text() for x in axis.get_ticklabels()])
1346-
print(np.arange(len(df)))
13471344
assert (axis.get_ticklocs() == np.arange(len(df))).all()
13481345

13491346
if align == 'center':
@@ -2998,6 +2995,22 @@ def test_secondary_axis_font_size(self, method):
29982995
self._check_ticks_props(axes=ax.right_ax,
29992996
ylabelsize=fontsize)
30002997

2998+
def test_bar_ticklabel_consistence(self):
2999+
# Draw two consecutiv bar plot with consistent ticklabels
3000+
# GH: 26186
3001+
data = {"A": 0, "B": 3, "C": -4}
3002+
df = pd.DataFrame.from_dict(data, orient="index", columns=["Value"])
3003+
ax = df.plot.bar()
3004+
ax.get_figure().canvas.draw()
3005+
xticklabels = [t.get_text() for t in ax.get_xticklabels()]
3006+
label_positions_1 = dict(zip(xticklabels, ax.get_xticks()))
3007+
df = df.sort_values("Value") * - 2
3008+
df.plot.bar(ax=ax, color="red")
3009+
ax.get_figure().canvas.draw()
3010+
xticklabels = [t.get_text() for t in ax.get_xticklabels()]
3011+
label_positions_2 = dict(zip(xticklabels, ax.get_xticks()))
3012+
assert label_positions_1 == label_positions_2
3013+
30013014

30023015
def _generate_4_axes_via_gridspec():
30033016
import matplotlib.pyplot as plt

0 commit comments

Comments
 (0)