使用func使用自定义DF(或列表)注释每个FacetGrid子图
考虑以下数据和faceTgrid:
d = {'SITE':['A', 'B', 'C', 'C', 'A'], 'VF':[0.00, 0.78, 0.99, 1.00, 0.50],'TYPE':['typeA', 'typeA', 'typeB', 'typeC', 'typeD']}
new_df = pd.DataFrame(data=d)
with sns.axes_style("white"):
g = sns.FacetGrid(data=new_df, col='SITE', col_wrap= 3, height=7, aspect=0.25,
hue='TYPE', palette=['#1E88E5', '#FFC107', '#D81B60'])
g.map(sns.scatterplot, 'VF', 'TYPE', s=100)
使用另一个dataFrame
:
d = {'SITE':['A', 'B', 'C'], 'N':[10, 5, 7]}
ann_df = pd.DataFrame(data=d)
其中site
匹配原始new_df ['site'] ,和与
facetgrid < /代码>。new_df ['site']
的维度不同,但在columns
中的相应长度
您如何Annotate
每个subplot
使用custom func
使用 not scatterplot new_df
new_df ,但是ann_df
或自定义 list ,如果它与原始new_df ['site']
匹配,并添加ann_df ['n ']
到每个子图,如下所示:
所以,沿着这些行或更好的东西:
def annotate(data, **kws):
n = data # should be the int for each matching SITE
ax = plt.gca()
ax.text(.1, .2, f"N = {n}", transform=ax.transAxes)
g.map_dataframe(annotate(ann_df))
Consider the following data and FacetGrid:
d = {'SITE':['A', 'B', 'C', 'C', 'A'], 'VF':[0.00, 0.78, 0.99, 1.00, 0.50],'TYPE':['typeA', 'typeA', 'typeB', 'typeC', 'typeD']}
new_df = pd.DataFrame(data=d)
with sns.axes_style("white"):
g = sns.FacetGrid(data=new_df, col='SITE', col_wrap= 3, height=7, aspect=0.25,
hue='TYPE', palette=['#1E88E5', '#FFC107', '#D81B60'])
g.map(sns.scatterplot, 'VF', 'TYPE', s=100)
Using another dataframe
:
d = {'SITE':['A', 'B', 'C'], 'N':[10, 5, 7]}
ann_df = pd.DataFrame(data=d)
Where the SITE
matches the original new_df['SITE']
, and is not the same dimensions as new_df['SITE']
, but has the corresponding length of columns
in the FacetGrid
.
How do you annotate
each subplot
using a custom func
using not the scatterplot new_df
, but the ann_df
or custom list
, if it matches the original new_df['SITE']
and adds the ann_df['N']
to each subplot as shown below:
So, something along these lines or better:
def annotate(data, **kws):
n = data # should be the int for each matching SITE
ax = plt.gca()
ax.text(.1, .2, f"N = {n}", transform=ax.transAxes)
g.map_dataframe(annotate(ann_df))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
seaborn v0.11.0
使用图形函数,例如seaborn.relplot
而不是 seaborn.facetgridcol =
使用的值将按字母顺序绘制,默认情况下,否则用col_order =
当然ann_df ['site']
按相同的顺序排序。seaborn.axisgrid.facetgrid
弄平sns.relplot
,通过matplotlib.axes
iTed,并添加.text
.text 通过使用<代码> i 来自枚举
with.iloc
为'n'
的正确值索引。dataframe
而不是dict dict中获取数据
。Python 3.10
,PANDAS 1.4.2
,matplotlib 3.5.1
,seaborn 0.11.2
中测试。seaborn v0.11.0
to use figure-level functions likeseaborn.relplot
instead ofseaborn.FacetGrid
col=
will be plotted alphabetically by default, otherwise specify an order withcol_order=
, and then make sureann_df['SITE']
is sorted in the same order.seaborn.axisgrid.FacetGrid
returned bysns.relplot
, iterate through thematplotlib.axes
, and add.text
to each plot by usingi
fromenumerate
with.iloc
to index the correct value for'N'
.DataFrame
instead of adict
.python 3.10
,pandas 1.4.2
,matplotlib 3.5.1
,seaborn 0.11.2