本文介绍了如何在不生成第二个图的情况下(错误地)调整 Seaborn 中种类“群"的猫图的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了调整(大多数)Seaborn 图的大小,我一直遵循这个约定:

fig, ax = plt.subplots(figsize=(10,5))sns.catplot(x='xdata', y='ydata', data=df, kind='swarm', ax=ax)

这也适用于猫图,但它会生成第二个完全空白的图.这是 Seaborn 中的错误(还是我做错了什么)?有什么方法可以在不获得第二个空白图的情况下正确调整该图的大小(当我说空白图时,我的意思是没有数据的图与第一个图的轴标签相同)?

请记住,我是 Seaborn 的新手.提前致谢.

解决方案

考虑不调用 subplots 并使用 heightaspect 参数作为这个seaborn

For resizing (most) Seaborn plots, i've been following this convention:

fig, ax = plt.subplots(figsize=(10,5))
sns.catplot(x='xdata', y='ydata', data=df, kind='swarm', ax=ax)

This works for the catplot as well, however it generates a second, completely blank plot. Is this a bug in Seaborn (or am i doing something wrong)? Is there any way to properly resize this plot without getting a second blank plot (when i say blank plot i mean a plot with no data just same axis labels as the first plot)?

Please keep in mind I'm very new to Seaborn. Thanks in advance.

解决方案

Consider not calling subplots and use height and aspect arguments as this seaborn factorplot solution shows where aspect is the width multiple of height, likely to keep dimensions consistent:

sns.catplot(x='xdata', y='ydata', data=df, kind='swarm', height=5, aspect=2)

From help(sns.catplot) output:

height : scalar, optional
        Height (in inches) of each facet. See also: ``aspect``.

aspect : scalar, optional
    Aspect ratio of each facet, so that ``aspect * height`` gives the width
    of each facet in inches.

To demonstrate with random data:

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

np.random.sample(71318)
df = pd.DataFrame({'xdata': np.random.choice(['pandas', 'r', 'julia', 'sas', 'spss', 'stata'], 100),
                   'ydata': np.random.choice(range(1,6), 100)})

sns.catplot(x='xdata', y='ydata', data=df, kind='swarm', height=5, aspect=2)

plt.show()
plt.clf()
plt.close()

这篇关于如何在不生成第二个图的情况下(错误地)调整 Seaborn 中种类“群"的猫图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 09:30