我已经为我定义了一个函数,用于使用箱线图分析我的列
fig, ax = plt.subplots((len(list_of_columns)),1,figsize= datafigsize)
fig.suptitle(suptitle,fontsize=30)
ax = ax.ravel() # Ravel turns a matrix into a vector, which is easier to iterate
plt.tight_layout(h_pad = 3,pad=10);
for i, column in enumerate(list_of_columns):
nobs = dataframe[column].value_counts().values
nobs = [str(y) for y in nobs.tolist()]
nobs = ["n: " + j for j in nobs]
pos = range(len(nobs))
medians = dataframe.groupby([column])['saleprice'].median().values
for tick,label in zip(pos,ax[i].get_xticklabels()):
ax[i].text(pos[tick], medians[tick] + 0.03, nobs[tick],
horizontalalignment='center', size='small', color='k', weight='semibold')
sns.boxplot(data = dataframe,
x= dataframe[column],
y='saleprice',
ax=ax[i])
ax[i].set_title(list_of_titles[i],fontdict={'fontsize': 15})
ax[i].xaxis.set_visible(True);
子地块运行良好。 我的观察数字也被绘制出来
然而,观测数量只能绘制在6个类别上。以下是一个例子:
仅显示6个类别的n=#。 仅显示6个类别的n=#。
最有可能的情况是,您的环境中存在导致问题的其他对象。您还将sns.boxplot放置在错误的for循环中
如果我使用示例数据集进行设置:
您可以在下面看到,我稍微修改了脚本,计算了data.frame中的中值和观察数。还要确保打印顺序和计数顺序相同(我在下面使用groupby数据框的索引作为参考):
相关问题 更多 >
编程相关推荐