首页 > 解决方案 > ValueError:无法将输入数组从形状(244,2)广播到形状(244,)

问题描述

我正在尝试绘制一个简单的 Seaborn 情节。

import seaborn as sns
from matplotlib import pyplot as plt

tips = sns.load_dataset('tips')

#Plotting our subplots dividing with smoker
sns.relplot(x = "total_bill", y = "tip", hue = "smoker", col = "size", data = tips)

#Showing our plots in a proper format
plt.show()

但它显示一个错误

ValueError: could not broadcast input array from shape (244,2) into shape (244,)

我该怎么办?

标签: pythonpython-3.xnumpyseaborn

解决方案


'size' 列名似乎有一个错误。这很奇怪,因为tips它是 seaborn 的典型示例数据集之一。

当该列被重命名时,它按预期工作:

import seaborn as sns
from matplotlib import pyplot as plt

tips = sns.load_dataset('tips')
tips.rename(columns={'size': 'size_renamed'}, inplace=True)
sns.relplot(x='total_bill', y='tip', hue='smoker', col='size_renamed', data=tips)
plt.tight_layout()
plt.show()

结果图


推荐阅读