When trying to plot a pandas Series with pyplot.bar(), it seems Series whose first index is not 0 cause a TypeError: only size-1 arrays can be converted to Python scalars exception when the bottom argument is given. When bottom is not given, bar width is not uniform as expected.
Code for reproduction
importpandasaspdimportmatplotlib.pyplotaspltdf=pd.DataFrame({'i':[1,4,4,4,6,6], 'n':[3,5,1,2,3,4]})
ix=df.index%2==0plt.figure()
# produces expected plot with bars offset from x axisplt.bar(x=df.loc[ix,'i'], height=df.loc[ix, 'n'], bottom=df.loc[ix, 'i'])
plt.figure()
# produces expected plotplt.bar(x=df.loc[ix,'i'], height=df.loc[ix, 'n'])
ix=df.index%2==1plt.figure()
# the following fails with `TypeError`# plt.bar(x=df.loc[ix,'i'], height=df.loc[ix, 'n'], bottom=df.loc[ix, 'i'])# converting to a list fixes the issue and produces the expected outcome.# note that casting both x and bottom to list is required to produce expected outcome.plt.bar(x=list(df.loc[ix,'i']), height=df.loc[ix, 'n'], bottom=list(df.loc[ix, 'i']))
plt.figure()
# produces plot with unexpected bar widthplt.bar(x=df.loc[ix,'i'], height=df.loc[ix, 'n'])
# again, converting to list produces expected outcomeplt.figure()
plt.bar(x=list(df.loc[ix,'i']), height=df.loc[ix, 'n'])
Activity
jklymak commented on Feb 1, 2020
I think #15166 fixed this in 3.1.2. Can you upgrade and see if thats the case?
zeeMonkeez commented on Feb 1, 2020
indeed. sorry for noise
jklymak commented on Feb 1, 2020
No problem. Glad it works now!