Python 如何在 Seaborn lmplot facetgrid 中设置一些 xlim 和 ylim
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25212986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set some xlim and ylim in Seaborn lmplot facetgrid
提问by exp1orer
I'm using Seaborn's lmplot to plot a linear regression, dividing my dataset into two groups with a categorical variable.
我正在使用 Seaborn 的 lmplot 绘制线性回归图,将我的数据集分为具有分类变量的两组。
For both x and y, I'd like to manually set the lower boundon both plots, but leave the upper boundat the Seaborn default. Here's a simple example:
对于 x 和 y,我想手动设置两个图的下限,但将上限保留为 Seaborn 默认值。这是一个简单的例子:
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:

这将输出以下内容:

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:
但在这个例子中,我希望 xlim 和 ylim 是 (0,*) 。我尝试使用 sns.plt.ylim 和 sns.plt.xlim 但这些只影响右手图。例子:
sns.plt.ylim(0,)
sns.plt.xlim(0,)


How can I access the xlim and ylim for each plot in the FacetGrid?
如何访问 FacetGrid 中每个图的 xlim 和 ylim?
采纳答案by DrV
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,)
creates:
创建:


回答by mwaskom
The lmplotfunction returns a FacetGridinstance. This object has a method called set, to which you can pass key=valuepairs and they will be set on each Axes object in the grid.
该lmplot函数返回一个FacetGrid实例。该对象有一个名为 的方法set,您可以key=value向该方法传递对,它们将设置在网格中的每个 Axes 对象上。
Secondly, you can set only one side of an Axes limit in matplotlib by passing Nonefor the value you want to remain as the default.
其次,您可以通过传递None您希望保留为默认值的值,在 matplotlib 中仅设置 Axes 限制的一侧。
Putting these together, we have:
把这些放在一起,我们有:
g = sns.lmplot('X', 'Y', df, col='Z', sharex=False, sharey=False)
g.set(ylim=(0, None))



