Skip to content

Commit 10770e9

Browse files
committed
FIX: indexed pandas bar
1 parent b12a3a8 commit 10770e9

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

lib/matplotlib/axes/_axes.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -2140,12 +2140,31 @@ def _convert_dx(dx, x0, xconv, convert):
21402140
try:
21412141
x0 = x0[0]
21422142
except (TypeError, IndexError, KeyError):
2143-
x0 = x0
2143+
try:
2144+
xx = np.atleast_1d(np.asarray(x0))
2145+
if len(xx) == 1:
2146+
# this keeps units for singletons...
2147+
x0 = x0
2148+
else:
2149+
# fallback for objects with length, but strange
2150+
# ways of indexing (i.e. pandas)
2151+
x0 = xx[0]
2152+
except:
2153+
# generic except. Think this should never happen, but...
2154+
x0 = x0
21442155

21452156
try:
21462157
x = xconv[0]
21472158
except (TypeError, IndexError, KeyError):
2148-
x = xconv
2159+
try:
2160+
xx = np.atleast_1d(np.asarray(xconv))
2161+
if len(xx) == 1:
2162+
x = xconv
2163+
else:
2164+
x = xx[0]
2165+
except:
2166+
# generic except. Think this should never happen, but...
2167+
x = xconv
21492168

21502169
delist = False
21512170
if not np.iterable(dx):

lib/matplotlib/tests/test_axes.py

+8
Original file line numberDiff line numberDiff line change
@@ -1647,6 +1647,14 @@ def test_bar_pandas(pd):
16471647
ax.plot(dates, baseline, color='orange', lw=4)
16481648

16491649

1650+
def test_bar_pandas_indexed(pd):
1651+
# Smoke test for indexed pandas
1652+
df = pd.DataFrame({"x": [1., 2., 3.], "width": [.2, .4, .6]},
1653+
index=[1, 2, 3])
1654+
fig, ax = plt.subplots()
1655+
ax.bar(df.x, 1., width=df.width)
1656+
1657+
16501658
@image_comparison(['hist_log'], remove_text=True)
16511659
def test_hist_log():
16521660
data0 = np.linspace(0, 1, 200)**3

0 commit comments

Comments
 (0)