当需要条件时,如何将plt.fill_between()映射到Seaborn中的facetgrid图?

时间:2018-06-27 00:53:43

标签: python matplotlib seaborn

我发现solution在没有条件的情况下将fill_between映射到facetgrid图。如果我需要一个怎么办。示例代码:

# Create a dataset
my_count = [
    "France", "Australia", "Japan", "USA", "Germany", "Congo", "China",
    "England", "Spain", "Greece", "Marocco", "South Africa", "Indonesia",
    "Peru", "Chili", "Brazil"
]
grp = ["Group 1", "Group 2"]
df = pd.DataFrame({
    "country": np.repeat(my_count, 20),
    "years": range(2000, 2010) * 32,
    "value": np.arange(320),
    "lower bound": np.arange(320) - 10,
    "upper bound": np.arange(320) + 10,
    "group": list(np.repeat(grp, 10)) * 16,
})

# Create a grid : initialize it
g = sns.FacetGrid(df, col="country", col_wrap=4, sharey=False)

# Add the line over the area with the plot function
g = g.map_dataframe(
    sns.tsplot,
    time="years",
    unit="group",
    value="value",
    condition="group",
    color="deep")
# Fill the area with fill_between
g = g.map(
    plt.fill_between,
    "years",
    "lower bound",
    "upper bound",
# This line gives error. If commented out, code runs but gives meaningless plots.
    df["group"] == "Group 1",  
    alpha=0.2)

plt.show()

我可以iterate over g.axes.flat一次一个地填充fill_between(),但这很麻烦。

0 个答案:

没有答案