使用func使用自定义DF(或列表)注释每个FacetGrid子图

发布于 2025-01-29 12:01:11 字数 1628 浏览 2 评论 0原文

考虑以下数据和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)

”示例faceTgrid“

使用另一个dataFrame

d = {'SITE':['A', 'B', 'C'], 'N':[10, 5, 7]} 

ann_df = pd.DataFrame(data=d) 

其中site匹配原始new_df ['site'] ,new_df ['site']的维度不同,但在columns中的相应长度 facetgrid < /代码>。

您如何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)

Example FacetGrid

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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东风软 2025-02-05 12:01:11
  • 建议使用seaborn v0.11.0使用图形函数,例如 seaborn.relplot 而不是 seaborn.facetgrid
  • col =使用的值将按字母顺序绘制,默认情况下,否则用col_order = 当然ann_df ['site']按相同的顺序排序。
  • seaborn.axisgrid.facetgrid弄平 sns.relplot,通过matplotlib.axes iTed,并添加 .text .text 通过使用<代码> i 来自枚举 with .iloc 'n'的正确值索引。
  • 类似于此答案,但是从次级dataframe而不是dict dict中获取数据
  • Python 3.10PANDAS 1.4.2matplotlib 3.5.1seaborn 0.11.2中测试。
import seaborn as sns
import pandas as pd

# DataFrame 1
d1 = {'SITE':['A', 'B', 'C', 'C', 'A'],
      'VF':[0.00, 0.78, 0.99, 1.00, 0.50],
      'TYPE':['typeA', 'typeA', 'typeB', 'typeC', 'typeD']} 
df = pd.DataFrame(data=d1)

# DataFrame 2
d2 = {'SITE':['A', 'B', 'C'], 'N':[10, 5, 7]} 
ann_df = pd.DataFrame(data=d2) 

# plot
g = sns.relplot(kind='scatter', data=df, x='VF', y='TYPE', col='SITE',
                col_wrap=3, height=7, aspect=0.5, hue='TYPE', s=100)

# flatten axes into a 1-d array
axes = g.axes.flatten()

# iterate through the axes
for i, ax in enumerate(axes):
    ax.text(0, 3, f"N = {ann_df.iloc[i, 1]}")

“在此处输入图像描述”

  • It is recommended from seaborn v0.11.0 to use figure-level functions like seaborn.relplot instead of seaborn.FacetGrid
  • The values used for col= will be plotted alphabetically by default, otherwise specify an order with col_order=, and then make sure ann_df['SITE'] is sorted in the same order.
  • Flatten the seaborn.axisgrid.FacetGrid returned by sns.relplot, iterate through the matplotlib.axes, and add .text to each plot by using i from enumerate with .iloc to index the correct value for 'N'.
  • Similar to this answer, but getting data from a secondary DataFrame instead of a dict.
  • Tested in python 3.10, pandas 1.4.2, matplotlib 3.5.1, seaborn 0.11.2
import seaborn as sns
import pandas as pd

# DataFrame 1
d1 = {'SITE':['A', 'B', 'C', 'C', 'A'],
      'VF':[0.00, 0.78, 0.99, 1.00, 0.50],
      'TYPE':['typeA', 'typeA', 'typeB', 'typeC', 'typeD']} 
df = pd.DataFrame(data=d1)

# DataFrame 2
d2 = {'SITE':['A', 'B', 'C'], 'N':[10, 5, 7]} 
ann_df = pd.DataFrame(data=d2) 

# plot
g = sns.relplot(kind='scatter', data=df, x='VF', y='TYPE', col='SITE',
                col_wrap=3, height=7, aspect=0.5, hue='TYPE', s=100)

# flatten axes into a 1-d array
axes = g.axes.flatten()

# iterate through the axes
for i, ax in enumerate(axes):
    ax.text(0, 3, f"N = {ann_df.iloc[i, 1]}")

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文