如何在Seaborn Lmplot FacetGrid中设置一些xlim和ylim [英] How to set some xlim and ylim in Seaborn lmplot facetgrid
问题描述
我正在使用Seaborn的lmplot绘制线性回归,将我的数据集分为两个类别变量.
I'm using Seaborn's lmplot to plot a linear regression, dividing my dataset into two groups with a categorical variable.
对于x和y,我想在两个图上手动设置下界,但将上界保留为Seaborn默认值. 这是一个简单的示例:
For both x and y, I'd like to manually set the lower bound on both plots, but leave the upper bound at the Seaborn default. Here's a simple example:
import pandas as pd
import seaborn as sns
import random
n = 200
random.seed(2014)
base_x = [random.random() for i in range(n)]
base_y = [2*i for i in base_x]
errors = [random.uniform(0,1) for i in range(n)]
y = [i+j for i,j in zip(base_y,errors)]
df = pd.DataFrame({'X': base_x,
'Y': y,
'Z': ['A','B']*(n/2)})
mask_for_b = df.Z == 'B'
df.loc[mask_for_b,['X','Y']] = df.loc[mask_for_b,] *2
sns.lmplot('X','Y',df,col='Z',sharex=False,sharey=False)
这将输出以下内容:
This outputs the following:
但是在此示例中,我希望xlim和ylim为(0,*).我尝试使用sns.plt.ylim和sns.plt.xlim,但它们仅影响右侧图. 示例:
But in this example, I'd like the xlim and the ylim to be (0,*) . I tried using sns.plt.ylim and sns.plt.xlim but those only affect the right-hand plot. Example:
sns.plt.ylim(0,)
sns.plt.xlim(0,)
如何在FacetGrid中为每个图访问xlim和ylim?
How can I access the xlim and ylim for each plot in the FacetGrid?
推荐答案
您需要掌握轴本身.也许最干净的方法是更改最后一行:
You need to get hold of the axes themselves. Probably the cleanest way is to change your last row:
lm = sns.lmplot('X','Y',df,col='Z',sharex=False,sharey=False)
然后,您可以控制轴对象(轴数组):
Then you can get hold of the axes objects (an array of axes):
axes = lm.axes
之后,您可以调整轴属性
After that you can tweak the axes properties
axes[0,0].set_ylim(0,)
axes[0,1].set_ylim(0,)
创建:
这篇关于如何在Seaborn Lmplot FacetGrid中设置一些xlim和ylim的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!