seaborn 中的 catplot() 不适用于 subplot()
catplot() in seaborn doesn't work with subplot()
我不能对猫情节进行子情节。这是我的代码:
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. You can either use catplot
on a "long form dataframe" (using pd.melt()
),或在每个 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()
虽然这可能是我的软件包的促销,但 patchworklib 允许您将 catplot 图作为 matplotlib 子图处理。
请看下面的代码。
import seaborn as sns
import matplotlib.pyplot as plt
import patchworklib as pw
pw.overwrite_axisgrid()
df = sns.load_dataset('iris')
g1 = sns.catplot(x='species', y='sepal_length', data=df , kind='violin')
g1 = pw.load_seaborngrid(g1)
g2 = sns.catplot(x='species', y='sepal_width', data=df , kind='violin')
g2 = pw.load_seaborngrid(g2)
(g1|g2).savefig()
您也可以垂直对齐它们,如下所示。
(g1/g2).savefig()
以上代码在Google colab.
中可执行
我不能对猫情节进行子情节。这是我的代码:
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. You can either use catplot
on a "long form dataframe" (using pd.melt()
),或在每个 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()
虽然这可能是我的软件包的促销,但 patchworklib 允许您将 catplot 图作为 matplotlib 子图处理。 请看下面的代码。
import seaborn as sns
import matplotlib.pyplot as plt
import patchworklib as pw
pw.overwrite_axisgrid()
df = sns.load_dataset('iris')
g1 = sns.catplot(x='species', y='sepal_length', data=df , kind='violin')
g1 = pw.load_seaborngrid(g1)
g2 = sns.catplot(x='species', y='sepal_width', data=df , kind='violin')
g2 = pw.load_seaborngrid(g2)
(g1|g2).savefig()
您也可以垂直对齐它们,如下所示。
(g1/g2).savefig()
以上代码在Google colab.
中可执行