不支持的操作数类型

不支持的操作数类型

本文介绍了Seaborn boxplot:TypeError:不支持的操作数类型/:'str'和'int'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着像这样制作垂直的seaborn boxplot

I try to make vertical seaborn boxplot like this

import pandas as pd
df = pd.DataFrame({'a' : ['a', 'b' , 'b', 'a'], 'b' : [5, 6, 4, 3] })
import seaborn as sns
import matplotlib.pylab as plt
%matplotlib inline
sns.boxplot(  x= "b",y="a",data=df )

我明白了

我写东方

sns.boxplot(  x= "c",y="a",data=df , orient = "v")

并得到

TypeError: unsupported operand type(s) for /: 'str' and 'int'

但是

sns.boxplot(  x= "c",y="a",data=df , orient = "h")

正确工作!怎么了?

TypeError                                 Traceback (most recent call last)
<ipython-input-16-5291a1613328> in <module>()
----> 1 sns.boxplot(  x= "b",y="a",data=df , orient = "v")

C:\Program Files\Anaconda3\lib\site-packages\seaborn\categorical.py in boxplot(x, y, hue, data, order, hue_order, orient, color, palette, saturation, width, fliersize, linewidth, whis, notch, ax, **kwargs)
   2179     kwargs.update(dict(whis=whis, notch=notch))
   2180
-> 2181     plotter.plot(ax, kwargs)
   2182     return ax
   2183

C:\Program Files\Anaconda3\lib\site-packages\seaborn\categorical.py in plot(self, ax, boxplot_kws)
    526     def plot(self, ax, boxplot_kws):
    527         """Make the plot."""
--> 528         self.draw_boxplot(ax, boxplot_kws)
    529         self.annotate_axes(ax)
    530         if self.orient == "h":

C:\Program Files\Anaconda3\lib\site-packages\seaborn\categorical.py in draw_boxplot(self, ax, kws)
    463                                          positions=[i],
    464                                          widths=self.width,
--> 465                                          **kws)
    466                 color = self.colors[i]
    467                 self.restyle_boxplot(artist_dict, color, props)

C:\Program Files\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, *args, **kwargs)
   1816                     warnings.warn(msg % (label_namer, func.__name__),
   1817                                   RuntimeWarning, stacklevel=2)
-> 1818             return func(ax, *args, **kwargs)
   1819         pre_doc = inner.__doc__
   1820         if pre_doc is None:

C:\Program Files\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_xticks, autorange)
   3172             bootstrap = rcParams['boxplot.bootstrap']
   3173         bxpstats = cbook.boxplot_stats(x, whis=whis, bootstrap=bootstrap,
-> 3174                                        labels=labels, autorange=autorange)
   3175         if notch is None:
   3176             notch = rcParams['boxplot.notch']

C:\Program Files\Anaconda3\lib\site-packages\matplotlib\cbook.py in boxplot_stats(X, whis, bootstrap, labels, autorange)
   2036
   2037         # arithmetic mean
-> 2038         stats['mean'] = np.mean(x)
   2039
   2040         # medians and quartiles

C:\Program Files\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py in mean(a, axis, dtype, out, keepdims)
   2883
   2884     return _methods._mean(a, axis=axis, dtype=dtype,
-> 2885                           out=out, keepdims=keepdims)
   2886
   2887

C:\Program Files\Anaconda3\lib\site-packages\numpy\core\_methods.py in _mean(a, axis, dtype, out, keepdims)
     70         ret = ret.dtype.type(ret / rcount)
     71     else:
---> 72         ret = ret / rcount
     73
     74     return ret

TypeError: unsupported operand type(s) for /: 'str' and 'int'

推荐答案

对于 seaborn 的箱线图,在水平和垂直对齐方式之间切换时,注意 x 轴和 y 轴分配很重要:

For seaborn's boxplots it is important to keep an eye on the x-axis and y-axis assignments, when switching between horizontal and vertical alignment:

%matplotlib inline
import pandas as pd
import seaborn as sns

df = pd.DataFrame({'a' : ['a', 'b' , 'b', 'a'], 'b' : [5, 6, 4, 3] })

# horizontal boxplots
sns.boxplot(x="b", y="a", data=df, orient='h')

# vertical boxplots
sns.boxplot(x="a", y="b", data=df, orient='v')

混淆列会导致seaborn尝试计算分类数据上的框的汇总统计量,这肯定会失败.

Mixing up the columns will cause seaborn to try to calculate the summary statistics of the boxes on categorial data, which is bound to fail.

这篇关于Seaborn boxplot:TypeError:不支持的操作数类型/:'str'和'int'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 18:51