Seborn中的catlot()不能与subploy()一起使用 [英] catplot() in seaborn doesn't work with subplot()

查看:14
本文介绍了Seborn中的catlot()不能与subploy()一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能细分CatPlot。以下是我的代码:

import seaborn as sns
import matplotlib.pyplot as plt

fig, axes = plt.subplots(1,2)
sns.catplot(x='species', y='sepal_length', data=df , kind='violin')
sns.catplot(x='species', y='sepal_width', data=df , kind='violin')

下面是输出: 我怎么才能把它修好呢? 谢谢您。

推荐答案

catplot创建其own new figure。您可以在"long form dataframe"(使用pd.melt())上使用catplot,也可以在每个ax上创建单独的sns.violinplot

将子图与violinplot一起使用:

import seaborn as sns
import matplotlib.pyplot as plt

df = sns.load_dataset('iris')
fig, axes = plt.subplots(1, 2)
sns.violinplot(x='species', y='sepal_length', data=df, ax=axes[0])
sns.violinplot(x='species', y='sepal_width', data=df, ax=axes[1])
plt.tight_layout()
plt.show()

catplot与长格式数据帧配合使用:

import seaborn as sns
import matplotlib.pyplot as plt

df = sns.load_dataset('iris')
df_long = df.melt(id_vars='species', value_vars=['sepal_length', 'sepal_width'])

sns.catplot(x='species', y='value', col='variable', data=df_long, kind='violin')
plt.tight_layout()
plt.show()

这篇关于Seborn中的catlot()不能与subploy()一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆