我正在尝试进行分类分析 我想将计数值转换为百分比,但出现错误
I am trying to do categorical analysis I want to convert count values to percentage and I am getting an error
我的代码是:
sns.countplot(x='marital', hue='loan', data=df, estimator=lambda x: sum(x==0)*100.0/len(x))
它给出了以下错误:
AttributeError Traceback (most recent call last)
<ipython-input-21-9615e7e7b899> in <module>()
----> 1 sns.countplot(x='marital', hue='loan', data=df, estimator=lambda x: sum(x==0)*100.0/len(x))
8 frames
/usr/local/lib/python3.7/dist-packages/matplotlib/artist.py in _update_property(self, k, v)
1000 if not callable(func):
1001 raise AttributeError('{!r} object has no property {!r}'
-> 1002 .format(type(self).__name__, k))
1003 return func(v)
1004
AttributeError: 'Rectangle' object has no property 'estimator'
sns.countplot
不支持 estimator=
参数。但是,您可以创建条形图来模拟计数图。由于条形图需要数字 y-value,解决方法是使用一个数组。
sns.barplot(x='marital', y=np.ones(len(df)), hue='loan', data=df, estimator=lambda x: len(x)*100.0/len(df), ci=None)
由于没有给出数据和数据类型,下面是一个使用提示数据集的例子:
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
tips = sns.load_dataset('tips')
sns.set()
ax = sns.barplot(x='day', y=np.ones(len(tips)), hue='smoker', data=tips, palette='autumn',
estimator=lambda x: len(x) * 100.0 / len(tips), ci=None)
ax.set_ylabel('Percentage of counts')
plt.show()
另一种方法是创建一个常规计数图并更改刻度:
import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter, MultipleLocator
import seaborn as sns
import numpy as np
tips = sns.load_dataset('tips')
sns.set()
ax = sns.countplot(x='day', hue='smoker', data=tips, palette='winter')
ax.yaxis.set_major_locator(MultipleLocator(len(tips) * 0.05))
ax.yaxis.set_major_formatter(PercentFormatter(len(tips), decimals=0))
plt.show()
我的代码是:
sns.countplot(x='marital', hue='loan', data=df, estimator=lambda x: sum(x==0)*100.0/len(x))
它给出了以下错误:
AttributeError Traceback (most recent call last)
<ipython-input-21-9615e7e7b899> in <module>()
----> 1 sns.countplot(x='marital', hue='loan', data=df, estimator=lambda x: sum(x==0)*100.0/len(x))
8 frames
/usr/local/lib/python3.7/dist-packages/matplotlib/artist.py in _update_property(self, k, v)
1000 if not callable(func):
1001 raise AttributeError('{!r} object has no property {!r}'
-> 1002 .format(type(self).__name__, k))
1003 return func(v)
1004
AttributeError: 'Rectangle' object has no property 'estimator'
sns.countplot
不支持 estimator=
参数。但是,您可以创建条形图来模拟计数图。由于条形图需要数字 y-value,解决方法是使用一个数组。
sns.barplot(x='marital', y=np.ones(len(df)), hue='loan', data=df, estimator=lambda x: len(x)*100.0/len(df), ci=None)
由于没有给出数据和数据类型,下面是一个使用提示数据集的例子:
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
tips = sns.load_dataset('tips')
sns.set()
ax = sns.barplot(x='day', y=np.ones(len(tips)), hue='smoker', data=tips, palette='autumn',
estimator=lambda x: len(x) * 100.0 / len(tips), ci=None)
ax.set_ylabel('Percentage of counts')
plt.show()
另一种方法是创建一个常规计数图并更改刻度:
import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter, MultipleLocator
import seaborn as sns
import numpy as np
tips = sns.load_dataset('tips')
sns.set()
ax = sns.countplot(x='day', hue='smoker', data=tips, palette='winter')
ax.yaxis.set_major_locator(MultipleLocator(len(tips) * 0.05))
ax.yaxis.set_major_formatter(PercentFormatter(len(tips), decimals=0))
plt.show()