使用Seaborn的FacetGrid时如何在所有图上添加比较线 [英] How to add a comparison line to all plots when using Seaborn's FacetGrid

查看:42
本文介绍了使用Seaborn的FacetGrid时如何在所有图上添加比较线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用FacetGrid将相同的比较线添加到多个绘图中.这是我卡住的地方:

I'm trying to add the same comparison line to multiple plots using FacetGrid. Here is where I get stuck:

# Import the dataset
tips = sns.load_dataset("tips")

# Plot using FaceGrid, separated by smoke
g = sns.FacetGrid(tips, col="smoker", size=5, aspect=1.5)
g.map(plt.scatter, "tip", "total_bill")
x = np.arange(0, 50, .5)
y = 0.2*x
plt.plot(y, x, C='k')
plt.show()

这是结果

如您所见,这条线出现在最后一个图上,但不是第一个.我如何在两者上都得到它?

As you can see, the line shows up on the last plot, but not the first. How do I get it on both?

推荐答案

您可以映射相同的功能到 FacetGrid .

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

# Import the dataset
tips = sns.load_dataset("tips")

# Plot using FaceGrid, separated by smoke
g = sns.FacetGrid(tips, col="smoker", height=5, aspect=1.5)
g.map(plt.scatter, "tip", "total_bill")

def const_line(*args, **kwargs):
    x = np.arange(0, 50, .5)
    y = 0.2*x
    plt.plot(y, x, C='k')

g.map(const_line)

plt.show()

这篇关于使用Seaborn的FacetGrid时如何在所有图上添加比较线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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