x 轴上的分组条形图
Grouped bar plot on the x-axis
我有问题。我想打印一个分组条形图(使用 seaborn 或 matplotlib)。当我 运行 下面的代码片段时,我得到了
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
d = {'type': ['House', 'Boat', 'Bus', 'Bus', 'Boat'], 'room': ['private', 'public', 'public', 'private', 'nothing']}
df = pd.DataFrame(data=d)
print(df)
[OUT]
type room
0 House private
1 Boat public
2 Bus public
3 Bus private
4 Boat nothing
g = sns.catplot(
data=df, kind="bar",
x='type', y=df.groupby(['type']).count(), hue="room",
palette="dark", alpha=.6, height=6
)
g.despine(left=True)
g.set_axis_labels("", "Counts")
g.legend.set_title("")
[OUT]
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
我想要的
看起来这样可以解决问题:
pd.crosstab(df['type'],df['room']).plot.bar(stacked=True)
我有问题。我想打印一个分组条形图(使用 seaborn 或 matplotlib)。当我 运行 下面的代码片段时,我得到了
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
d = {'type': ['House', 'Boat', 'Bus', 'Bus', 'Boat'], 'room': ['private', 'public', 'public', 'private', 'nothing']}
df = pd.DataFrame(data=d)
print(df)
[OUT]
type room
0 House private
1 Boat public
2 Bus public
3 Bus private
4 Boat nothing
g = sns.catplot(
data=df, kind="bar",
x='type', y=df.groupby(['type']).count(), hue="room",
palette="dark", alpha=.6, height=6
)
g.despine(left=True)
g.set_axis_labels("", "Counts")
g.legend.set_title("")
[OUT]
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
我想要的
看起来这样可以解决问题:
pd.crosstab(df['type'],df['room']).plot.bar(stacked=True)