Skip to content

Plotting a pandas DataFrame with string MultiIndex #18371

Closed
@kasra-keshavarz

Description

@kasra-keshavarz

Bug report

I am trying to plot a violinplot of my DataFrame which its elements dtypes are all np.float 64. Also, it has a MultiIndex of mixed integer and string values. The problem is when the DataFrame is given to matplotlib.pyplot.boxplot the following error is raised, while in earlier version of matplotlib this problem did not exist.

TLDR: the matplotlib fails to plot DataFrame with string index.

Python code

arrays = [[bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], \
          ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
s = pd.DataFrame(np.random.randn(8,5), index=index, columns = ['un', 'deux', 'trois', 'quatre', 'cinq'])
s.unstack().boxplot() # works perfectly fine
plt.boxplot(s.unstack()) # fails

Actual outcome

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-64fecc3e6540> in <module>
----> 1 plt.boxplot(s.unstack())

~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\pyplot.py in boxplot(x, notch, sym, vert, whis, positions, widths, patch_artist, bootstrap, usermedians, conf_intervals, meanline, showmeans, showcaps, showbox, showfliers, boxprops, labels, flierprops, medianprops, meanprops, capprops, whiskerprops, manage_ticks, autorange, zorder, data)
   2510         whiskerprops=whiskerprops, manage_ticks=manage_ticks,
   2511         autorange=autorange, zorder=zorder,
-> 2512         **({"data": data} if data is not None else {}))
   2513 
   2514 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
   1436     def inner(ax, *args, data=None, **kwargs):
   1437         if data is None:
-> 1438             return func(ax, *map(sanitize_sequence, args), **kwargs)
   1439 
   1440         bound = new_sig.bind(ax, *args, **kwargs)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\axes\_axes.py in boxplot(self, x, notch, sym, vert, whis, positions, widths, patch_artist, bootstrap, usermedians, conf_intervals, meanline, showmeans, showcaps, showbox, showfliers, boxprops, labels, flierprops, medianprops, meanprops, capprops, whiskerprops, manage_ticks, autorange, zorder)
   3682 
   3683         bxpstats = cbook.boxplot_stats(x, whis=whis, bootstrap=bootstrap,
-> 3684                                        labels=labels, autorange=autorange)
   3685         if notch is None:
   3686             notch = rcParams['boxplot.notch']

~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\cbook\__init__.py in boxplot_stats(X, whis, bootstrap, labels, autorange)
   1175 
   1176         # arithmetic mean
-> 1177         stats['mean'] = np.mean(x)
   1178 
   1179         # medians and quartiles

<__array_function__ internals> in mean(*args, **kwargs)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\core\fromnumeric.py in mean(a, axis, dtype, out, keepdims)
   3371 
   3372     return _methods._mean(a, axis=axis, dtype=dtype,
-> 3373                           out=out, **kwargs)
   3374 
   3375 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\core\_methods.py in _mean(a, axis, dtype, out, keepdims)
    158             is_float16_result = True
    159 
--> 160     ret = umr_sum(arr, axis, dtype, out, keepdims)
    161     if isinstance(ret, mu.ndarray):
    162         ret = um.true_divide(

TypeError: cannot perform reduce with flexible type

Expected outcome

image
Just like the boxplot method of pandas.DataFrame.boxplot()

Matplotlib version

  • Operating system: Windows 10
  • Matplotlib version: 3.3.1
  • Matplotlib backend: module://ipykernel.pylab.backend_inline
  • Python version: Python 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)]
  • Jupyter version (if applicable): 6.1.1
  • Other libraries: Pandas 1.1.1

I am using Anaconda and updated all my libraries to the latest version via the 'conda update --update-all' code.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions