简体   繁体   English

Seaborn FacetGrid 上的熊猫图

[英]pandas plots on Seaborn FacetGrid

I have a problem in a Qt application when I attempt to plot my dataframe as an area plot with a time index using pandas plotting function in combination with Seaborn's FacetGrids.当我尝试使用 Pandas 绘图功能结合 Seaborn 的 FacetGrids 将我的数据框绘制为带有时间索引的区域图时,我在 Qt 应用程序中遇到了问题。 What happens is that a grid layout is correctly created, but the plots do not appear in these grids.发生的情况是正确创建了网格布局,但这些图没有出现在这些网格中。 Using a Seaborn plotting function works as expected, though.不过,使用 Seaborn 绘图功能按预期工作。

I tried to figure out what's going on by isolating the drawing routines from the rest of my code, and I've found a rather unexpected behaviour as shown below (using ipython notebook):我试图通过将绘图例程与我的其余代码隔离来弄清楚发生了什么,我发现了一个相当意外的行为,如下所示(使用 ipython notebook):

%matplotlib inline

import pandas as pd
import seaborn as sns

df = pd.DataFrame({
        "Home": [76, 64, 38, 78, 63,    45, 32, 46, 13, 40],
        "Away": [55, 67, 70, 56, 59,    69, 72, 24, 45, 21],
        "Team": ["T1"] * 5 +            ["T2"] * 5,
        "Year": ["1991", "1992", "1993", "1994", "1995"] * 2})

Now, what I want to do is to draw two facets, one for each team.现在,我想要做的是绘制两个方面,每个团队一个。 Each facet should show the 'Away' and 'Home' columns as two separate time series.每个方面都应将“离开”和“回家”列显示为两个单独的时间序列。 In line with the suggestion in another question ( Plotting time series using Seaborn FacetGrid ), I wrote a function that calls the pandas plotting function for the subset passed to it by map_dataframe():根据另一个问题( 使用 Seaborn FacetGrid 绘制时间序列)中的建议,我编写了一个函数,该函数为 map_dataframe() 传递给它的子集调用 pandas 绘图函数:

def plot_area(data, color):
    data[["Home", "Away"]].index = pd.to_datetime(data["Year"])
    data[["Home", "Away"]].plot(kind="area")

However, when using this function, the result is rather unexpected: the FacetGrid is created and initialized correctly, but the two calls to the pandas method do not use this grid as their plotting region, and they appear elsewhere.但是,在使用该函数时,结果却出人意料:FacetGrid 被正确创建和初始化,但是对pandas 方法的两次调用都没有使用该网格作为它们的绘图区域,而是出现在其他地方。

g = sns.FacetGrid(df, col="Team")
g.map_dataframe(plot_area)

<seaborn.axisgrid.FacetGrid at 0x1a25110>

Screenshot of output:输出截图:

In the post I linked above, @mwaskom notes that methods called in this way在我上面链接的帖子中,@mwaskom 指出以这种方式调用的方法

must draw a plot on the "currently active" matplotlib Axes.必须在“当前活动”的 matplotlib 轴上绘制一个图。

Perhaps that is the problem here?也许这就是问题所在? The code as such appears to be correct, because with a different plotting function, everything works as expected, eg with a sns.heatmap():这样的代码似乎是正确的,因为使用不同的绘图功能,一切都按预期工作,例如使用 sns.heatmap():

def plot_heatmap(data, color):
    sns.heatmap(data[["Home", "Away"]])    

g = sns.FacetGrid(df, col="Team")
g.map_dataframe(plot_heatmap)

<seaborn.axisgrid.FacetGrid at 0x4a6d110>

Screenshot of output:输出截图:

So, my question boils down to this: how do I have to change the function plot_area() so that the axes produced by the pandas plotting function appear on the subplots created by Seaborn's FacetGrid?所以,我的问题归结为:如何更改函数 plot_area() 以便熊猫绘图函数生成的轴出现在 Seaborn 的 FacetGrid 创建的子图中?

(pandas version 0.16.0, Seaborn version 0.6.0, ipython 3.2.1, Python 2.7) (熊猫版本 0.16.0、Seaborn 版本 0.6.0、ipython 3.2.1、Python 2.7)

The comment by mwaskom set me on the right track: I have to provide the current axes to the plot function (now this seems so obvious...). mwaskom 的评论让我走上了正确的轨道:我必须为 plot 函数提供当前轴(现在这看起来很明显......)。 For future reference, this is a working solution to my problem:为了将来参考,这是我的问题的有效解决方案:

def plot_area(data, color):
    data.index = pd.to_datetime(data["Year"])
    data[["Home", "Away"]].plot(kind="area", ax=plt.gca())

g = sns.FacetGrid(df, col="Team")
g.map_dataframe(plot_area)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM