I upgraded matplotlib recently and my boxplot stopped working. I realized it was the keyword positions which was taking in a Int64Index array created by pandas. Using Int64Index.values fixes this, but plot takes a Int64Index array so can't see why it isn't desirable for boxplot or why it was removed/no longer works.
Code for reproduction
importnumpyasnpimportpandasaspdimportmatplotlib.pyplotaspltdata=np.random.rand(5,2)
years=pd.date_range('1/1/2000',
periods=2, freq=pd.DateOffset(years=1)).year# Does not workplt.figure()
plt.boxplot(data, positions=years)
# Worksplt.figure()
plt.boxplot(data, positions=years.values)
# Worksplt.figure()
plt.plot(years, data[0,:])
Actual outcome
Traceback (most recent call last):
File "<ipython-input-38-2540b7c8b9eb>", line 6, in <module>
plt.boxplot(data, positions=years)
File "/Users/Ray/anaconda/lib/python3.6/site-packages/matplotlib/pyplot.py", line 2729, in boxplot
zorder=zorder, data=data)
File "/Users/Ray/anaconda/lib/python3.6/site-packages/matplotlib/__init__.py", line 1717, in inner
return func(ax, *args, **kwargs)
File "/Users/Ray/anaconda/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 3378, in boxplot
manage_xticks=manage_xticks, zorder=zorder)
File "/Users/Ray/anaconda/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 3698, in bxp
cap_x = np.array([cap_left, cap_right])
ValueError: setting an array element with a sequence.
Expected outcome
Expect to produce boxplot and label the xaxis '2000, 2001'
Unfortunately I cannot remember which version I previously used...
The issue is likely that Index objects used to act more like arrays than they do now or we are now using an array-like interface that Index does not support, or numpy is doing better and preserving types(would have to trace through the code to be sure of who broke who which is probably not worth doing).
is how we compute the box widths if position is passed in and we expect a list of floats, but for the Index types it is is returning a list of Index types (because np.ptp returns an Index type with one value in it). This makes sense for Index types in the context of pandas, but seems to be causing problems at the line the stack trace is on when we try to make an array out of the result of +/- operations on the Index object (which is still and Index object).
Just passing this to float does not work (float blows up), adding in a call to np.asanyarray might do the trick though...
Activity
jklymak commented on Dec 16, 2017
I think this is #9771. Pandas no longer registers their converter...
raybellwaves commented on Dec 16, 2017
Thanks. I wasn't aware of the change in pandas.
It may be slightly different as it works for plot but not boxplot?
I tried adding the
to the top of my code but it didn't work. Am I missing something?
jklymak commented on Dec 16, 2017
Well, not sure what the problem is then. Sorry for the misdirect...
jklymak commented on Dec 16, 2017
But just to be sure, did you upgrade pandas at the same time? If so, I'd check if the change was on their end...
raybellwaves commented on Dec 16, 2017
Yeah, I think I upgraded a few packages at the same time. I'm using pandas: 0.21.1 (added this to my summary). I'll update if I find out more info
tacaswell commented on Dec 17, 2017
The issue is likely that Index objects used to act more like arrays than they do now or we are now using an array-like interface that Index does not support, or numpy is doing better and preserving types(would have to trace through the code to be sure of who broke who which is probably not worth doing).
It looks like the exact issue is that
matplotlib/lib/matplotlib/axes/_axes.py
Lines 3680 to 3686 in 7a6bab7
Just passing this to float does not work (float blows up), adding in a call to np.asanyarray might do the trick though...