Skip to content

Commit 2e5e997

Browse files
committed
TST: Modify tests/plotting/test_frame.test_bar_categorical
Ticklocs are now float also for categorical bar data (as they are position on the axis). The test is changed to compare to a array of np.float.
1 parent 1a3038c commit 2e5e997

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

pandas/plotting/_matplotlib/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,11 +1357,11 @@ def _make_plot(self):
13571357
for i, (label, y) in enumerate(self._iter_data(fillna=0)):
13581358
ax = self._get_ax(i)
13591359

1360-
if self.orientation == 'vertical':
1360+
if self.orientation == "vertical":
13611361
ax.xaxis.update_units(self.ax_index)
13621362
self.tick_pos = ax.convert_xunits(self.ax_index)
13631363
self.ax_pos = self.tick_pos - self.tickoffset
1364-
elif self.orientation == 'horizontal':
1364+
elif self.orientation == "horizontal":
13651365
ax.yaxis.update_units(self.ax_index)
13661366
self.tick_pos = ax.convert_yunits(self.ax_index)
13671367
self.ax_pos = self.tick_pos - self.tickoffset

pandas/tests/plotting/test_frame.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,14 +1130,18 @@ def test_bar_categorical(self):
11301130
for df in [df1, df2]:
11311131
ax = df.plot.bar()
11321132
ticks = ax.xaxis.get_ticklocs()
1133-
tm.assert_numpy_array_equal(ticks, np.array([0, 1, 2, 3, 4, 5]))
1133+
tm.assert_numpy_array_equal(
1134+
ticks, np.array([0, 1, 2, 3, 4, 5], dtype=np.float)
1135+
)
11341136
assert ax.get_xlim() == (-0.5, 5.5)
11351137
# check left-edge of bars
11361138
assert ax.patches[0].get_x() == -0.25
11371139
assert ax.patches[-1].get_x() == 5.15
11381140

11391141
ax = df.plot.bar(stacked=True)
1140-
tm.assert_numpy_array_equal(ticks, np.array([0, 1, 2, 3, 4, 5]))
1142+
tm.assert_numpy_array_equal(
1143+
ticks, np.array([0, 1, 2, 3, 4, 5], dtype=np.float)
1144+
)
11411145
assert ax.get_xlim() == (-0.5, 5.5)
11421146
assert ax.patches[0].get_x() == -0.25
11431147
assert ax.patches[-1].get_x() == 4.75
@@ -3251,30 +3255,27 @@ def test_plot_no_numeric_data(self):
32513255
df.plot()
32523256

32533257
@pytest.mark.slow
3254-
@pytest.mark.parametrize('method', ['bar', 'barh'])
3258+
@pytest.mark.parametrize("method", ["bar", "barh"])
32553259
def test_bar_ticklabel_consistence(self, method):
32563260
# Draw two consecutiv bar plot with consistent ticklabels
32573261
# GH: 26186
32583262
def get_main_axis(ax):
3259-
if method == 'barh':
3263+
if method == "barh":
32603264
return ax.yaxis
3261-
elif method == 'bar':
3265+
elif method == "bar":
32623266
return ax.xaxis
3267+
32633268
data = {"A": 0, "B": 3, "C": -4}
32643269
df = pd.DataFrame.from_dict(data, orient="index", columns=["Value"])
32653270
ax = getattr(df.plot, method)()
32663271
ax.get_figure().canvas.draw()
3267-
xticklabels = [t.get_text()
3268-
for t in get_main_axis(ax).get_ticklabels()]
3269-
label_positions_1 = dict(zip(xticklabels,
3270-
get_main_axis(ax).get_ticklocs()))
3271-
df = df.sort_values("Value") * - 2
3272+
xticklabels = [t.get_text() for t in get_main_axis(ax).get_ticklabels()]
3273+
label_positions_1 = dict(zip(xticklabels, get_main_axis(ax).get_ticklocs()))
3274+
df = df.sort_values("Value") * -2
32723275
ax = getattr(df.plot, method)(ax=ax, color="red")
32733276
ax.get_figure().canvas.draw()
3274-
xticklabels = [t.get_text()
3275-
for t in get_main_axis(ax).get_ticklabels()]
3276-
label_positions_2 = dict(zip(xticklabels,
3277-
get_main_axis(ax).get_ticklocs()))
3277+
xticklabels = [t.get_text() for t in get_main_axis(ax).get_ticklabels()]
3278+
label_positions_2 = dict(zip(xticklabels, get_main_axis(ax).get_ticklocs()))
32783279
assert label_positions_1 == label_positions_2
32793280

32803281
def test_bar_numeric(self):

0 commit comments

Comments
 (0)