seaborn 中的补丁 [英] Patches in seaborn
问题描述
我的目的是在 seaborn 的 lmplot
中的特定坐标处添加一个补丁:
My intention is to add a patch at a specific coordinate in seaborn's lmplot
:
无论如何要向 lmplot
添加矩形/正方形补丁吗?
Is there anyway to add a rectangular/square patch to lmplot
?
我能够通过 sns.lmplot()
打印出绘图.但是当我尝试使用带有相关坐标的 ax.add_patch()
语句添加矩形补丁时,它出错了.
I was able to get the plot printed out through sns.lmplot()
. But when I try to add the rectangular patch using ax.add_patch()
statement with relevant coordinate's, it error's out.
#Sample code to generate lmplot and add patch
ax= sns.lmplot('A', 'B', hue="group", data=res_me,fit_reg=False, \
palette="Set1",size=10, aspect=1, scatter_kws={"s": 100,"linewidths":2,"edgecolor":"black"})
ax.add_patch(patches.Rectangle((0.912, 0.72), 1.02, .802,fill=False,edgecolor='green',lw=3))
我收到以下错误.
AttributeError: 'FacetGrid' object has no attribute 'add_patch'
那么我们可以为 FacetGrid
添加补丁吗?
So can we add patches to FacetGrid
?
推荐答案
lmplot
如您所学,返回一个 FacetGrid
,它将所有轴存储在一个axes
属性作为 2D numpy 数组.
lmplot
as you've learned, returns a FacetGrid
, which stores all of its axes in an axes
property as a 2D numpy array.
所以您只需要执行以下操作:
So you just need to do something like:
fg = sns.lmplot('A', 'B', hue="group", data=res_me,fit_reg=False,
palette="Set1",size=10, aspect=1,
scatter_kws={"s": 100,"linewidths":2,"edgecolor":"black"})
fg.axes[0, 0].add_patch(patches.Rectangle((0.912, 0.72), 1.02,
0.802,fill=False,edgecolor='green',lw=3))
请注意,如果您的 FacetGrid
只有一个 Axes
对象,您可以直接使用 fg.ax
Note that if your FacetGrid
only has one Axes
object in it, you can access it directly with fg.ax
这篇关于seaborn 中的补丁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!