Seaborn Factorplot在实际图块下方生成额外的空图 [英] Seaborn Factorplot generates extra empty plots below actual plot

查看:278
本文介绍了Seaborn Factorplot在实际图块下方生成额外的空图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部

我正在尝试使用子图函数和Seaborn库绘制两个因子图.我可以使用下面的代码分别绘制两个图.但是,seaborn在实际图下方生成了额外的图(请参见下图).有没有一种方法可以避免seaborn生成额外的空图?我试过plt.close摆脱积压,但不幸的是它只关闭了1个积压 另外,我正在尝试将图例移出地块,并在地块旁边显示图例.有没有简单的方法可以做到这一点.我尝试使用seaborn软件包中的legend_out,但是没有用.

I am trying to plot two Factorplots using subplots function and Seaborn library. I was able to plot two plots separately using below code. However, seaborn is generating extra plot below the actual plots (see below image) . Is there a way to avoid seaborn to generate extra empty plot ? I tried plt.close to get rid of plots, but unfortunately it just closed 1 plot Also, I am trying to move legend out of plot and display legend next to plots. Is there an easy way to do it. I tried legend_out available in seaborn package but it didn't work.

我的代码:

f,axes=plt.subplots(1,2,figsize=(8,4))

sns.factorplot(x="borough", y="pickups", hue="borough", kind='bar', data=n, size=4, aspect=2,ax=axes[0])

sns.factorplot(x="borough", y="pickups", hue="borough", kind='bar', data=low_pickups, size=4, aspect=2,ax=axes[1])
plt.close(2)
plt.show()

上述代码的输出:

注意:我是python的新手,请在您的代码中提供解释.

Note: I am new to python please provide explanation with your code.

数据帧的投放

#n dataframe
{'borough': {0: 'Bronx', 1: 'Brooklyn', 2: 'EWR', 3: 'Manhattan', 4: 'Queens', 5: 'Staten Island', 6: 'Unknown'}, 'pickups': {0: 50.66705042597283, 1: 534.4312687082662, 2: 0.02417683628827999, 3: 2387.253281142068, 4: 309.35482385447847, 5: 1.6018880957863229, 6: 2.0571804140650674}}
#low_pickups dataframe
{'borough': {2: 'EWR', 5: 'Staten Island', 6: 'Unknown'}, 'pickups': {2: 0.02417683628827999, 5: 1.6018880957863229, 6: 2.0571804140650674}}

推荐答案

请注意,factorplot被称为在seaborn的最新版本中.

Note that factorplot is called 'catplot' in more recent versions of seaborn.

catplotfactorplot是图形级功能.这意味着它们应该在图形级别而不是在轴级别工作.

catplot or factorplot are figure level functions. This means that they are supposed to work on the level of a figure and not on the level of axes.

f,axes=plt.subplots(1,2,figsize=(8,4))

  • 这将创建图1".
  • sns.factorplot(x="borough", y="pickups", hue="borough", kind='bar', data=n, size=4, aspect=2,ax=axes[0])
    

    • 这将创建图2",但是您不是在Figure 2上绘图,而是告诉seaborn从Figure 1axes[0]上绘图,因此Figure 2保持为空.
      • This creates 'Figure 2' but instead of drawing on Figure 2 you tell seaborn to draw on axes[0] from Figure 1, so Figure 2 remains empty.
      • sns.factorplot(x="borough", y="pickups", hue="borough", kind='bar', data=low_pickups, size=4, aspect=2,ax=axes[1])
        

        • 现在,这又创建了一个图形:Figure 3,在这里,您也告诉seaborn从Figure 1axes[1]的轴上绘制.
          • Now this creates yet again a figure: Figure 3 and here, too, you tell seaborn to draw on an axes from Figure 1, axes[1] that is.
          • plt.close(2)
            

            • 在这里关闭seaborn创建的空白Figure 2.
              • Here you close the empty Figure 2 created by seaborn.
              • 因此,现在剩下的是Figure 1,您已经将两个轴注入"到factorplot调用中,并且还有由factorplot的第二次调用创建的仍然为空的Figure 3图,但是从不对.任何内容:(.

                So now you are left with Figure 1 with the two axes you kinda 'injected' into the factorplot calls and with the still empty Figure 3 figure that was created by the 2nd call of factorplot but never sah any content :(.

                plt.show()
                

                • 现在您看到带有两个轴的Figure 1和带有空图的Figure 3.

                  • And now you see Figure 1 with 2 axes and the Figure 3 with an empty plot.

                    这是在终端机中运行时的情况,在笔记本电脑中,您可能只看到两个图形一个在另一个图形之下,另一个图形似乎是带有三个轴的图形.

                    您有2个选择:

                    plt.show()之前简单地关闭Figure 3:

                    f,axes=plt.subplots(1,2,figsize=(8,4))
                    
                    sns.factorplot(x="borough", y="pickups", hue="borough", kind='bar', data=n, size=4, aspect=2,ax=axes[0])
                    
                    sns.factorplot(x="borough", y="pickups", hue="borough", kind='bar', data=low_pickups, size=4, aspect=2,ax=axes[1])
                    plt.close(2)
                    plt.close(3)
                    plt.show()
                    

                    基本上,您正在通过提供Figure 1中的自定义"轴来短路factorplot的一部分,该部分创建了图形和要绘制的轴. 可能不是factorplot的设计目的,但是,嘿,如果它能工作,它就可以工作...而且确实可以.

                    Basically you are short-circuiting the part of factorplot that creates a figure and axes to draw on by providing your "custom" axes from Figure 1. Probably not what factorplot was designed for, but hey, if it works, it works... and it does.

                    让图形级功能执行其工作并创建自己的图形.您需要做的是指定要作为列的变量.

                    Let the figure level function do its job and create its own figures. What you need to do is specify what variables you want as columns.

                    由于似乎您有2个数据帧,分别是nlow_pickups,因此您应该首先从其中创建一个数据帧,并用cat列表示nlow_pickups:

                    Since it seems that you have 2 data frames, n and low_pickups, you should first create a single data frame out of them with the column say cat that is either n or low_pickups:

                    # assuming n and low_pickups are a pandas.DataFrame:
                    # first add the 'cat' column for both
                    n['cat'] = 'n'
                    low_pickups['cat'] = 'low_pickups'
                    # now create a new dataframe that is a combination of both
                    comb_df = n.append(low_pickups)
                    

                    现在,您可以使用变量cat作为列,通过一次调用sns.catplot(在您的情况下为sns.factorplot)来创建图形:

                    Now you can create your figure with a single call to sns.catplot (or sns.factorplot in your case) using the variable cat as column:

                    sns.catplot(x="borough", y="pickups", col='cat', hue="borough", kind='bar', sharey=False, data=comb_df, size=4, aspect=1)
                    plt.legend()
                    plt.show()
                    

                    注意:默认情况下,sharey=False是必需的,因为它是true,并且您基本上看不到第二个面板中的值,因为它们比第一个面板中的值小得多.

                    Note: The sharey=Falseis required as by default it would be true and you woul essentially not see the values in the 2nd panel as they are considerably smaller than the ones in the first panel.

                    版本2,然后给出:

                    您可能仍需要一些样式,但我会留给您;).

                    希望这对您有帮助!

                    这篇关于Seaborn Factorplot在实际图块下方生成额外的空图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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