如何访问seaborn.pointplot图中的点对象?
How to access the point objects in a seaborn.pointplot figure?
我想提取用 seaborn.pointplot
创建的图形中点的 y 轴位置。我不需要修改对象,它是为了自动化测试。
对于其他图(例如条形图),我已经能够从 axes.patches
.
中提取它
一直在axes.collections
混日子,想知道有没有直接的方法可以得到点的y轴位置
import random
import seaborn as sns
import pandas as pd
data = pd.DataFrame(
{"Col1": ["A", "B", "C"]*10,
"Col2": [random.uniform(1.0, 200.0) for _ in range(30)],
"Col3": ["X"]*10 + ["Y"]*10 + ["Z"]*10
}
)
params = {
"x": "Col1",
"y": "Col2",
"data": data,
"hue": "Col3",
"color": "black",
"dodge": 0.53333,
"join": False
}
ax = sns.pointplot(**params)
在这种情况下,您可以循环遍历ax.collections
。将有 3 个合集,每个合集 hue
。它们的类型为 PathCollection
。 .get_offsets()
给出他们的 xy 位置。
下面是一些示例代码,它在提取的位置周围绘制了一个红色圆圈。
import random
import seaborn as sns
import pandas as pd
data = pd.DataFrame({"Col1": ["A", "B", "C"] * 10,
"Col2": [random.uniform(1.0, 200.0) for _ in range(30)],
"Col3": ["X"] * 10 + ["Y"] * 10 + ["Z"] * 10})
params = {"x": "Col1", "y": "Col2", "data": data, "hue": "Col3", "color": "black", "dodge": 0.53333, "join": False}
ax = sns.pointplot(**params)
for path in ax.collections:
points = path.get_offsets()
print(f"hue: {path.get_label()}; y-values: {points[:, 1]}")
all_x_values = [path.get_offsets()[:, 0] for path in ax.collections]
all_y_values = [path.get_offsets()[:, 1] for path in ax.collections]
ax.scatter(x=all_x_values, y=all_y_values, s=200, fc='none', ec='r')
hue: X; y-values: [126.22035010280258 141.12294652228957 77.11286110373693]
hue: Y; y-values: [99.68997767038964 58.52959793955581 154.63078569762334]
hue: Z; y-values: [90.76316340021354 71.05228738206348 68.13109019572413]
我想提取用
seaborn.pointplot
创建的图形中点的 y 轴位置。我不需要修改对象,它是为了自动化测试。对于其他图(例如条形图),我已经能够从
中提取它axes.patches
.一直在
axes.collections
混日子,想知道有没有直接的方法可以得到点的y轴位置import random import seaborn as sns import pandas as pd data = pd.DataFrame( {"Col1": ["A", "B", "C"]*10, "Col2": [random.uniform(1.0, 200.0) for _ in range(30)], "Col3": ["X"]*10 + ["Y"]*10 + ["Z"]*10 } ) params = { "x": "Col1", "y": "Col2", "data": data, "hue": "Col3", "color": "black", "dodge": 0.53333, "join": False } ax = sns.pointplot(**params)
在这种情况下,您可以循环遍历ax.collections
。将有 3 个合集,每个合集 hue
。它们的类型为 PathCollection
。 .get_offsets()
给出他们的 xy 位置。
下面是一些示例代码,它在提取的位置周围绘制了一个红色圆圈。
import random
import seaborn as sns
import pandas as pd
data = pd.DataFrame({"Col1": ["A", "B", "C"] * 10,
"Col2": [random.uniform(1.0, 200.0) for _ in range(30)],
"Col3": ["X"] * 10 + ["Y"] * 10 + ["Z"] * 10})
params = {"x": "Col1", "y": "Col2", "data": data, "hue": "Col3", "color": "black", "dodge": 0.53333, "join": False}
ax = sns.pointplot(**params)
for path in ax.collections:
points = path.get_offsets()
print(f"hue: {path.get_label()}; y-values: {points[:, 1]}")
all_x_values = [path.get_offsets()[:, 0] for path in ax.collections]
all_y_values = [path.get_offsets()[:, 1] for path in ax.collections]
ax.scatter(x=all_x_values, y=all_y_values, s=200, fc='none', ec='r')
hue: X; y-values: [126.22035010280258 141.12294652228957 77.11286110373693]
hue: Y; y-values: [99.68997767038964 58.52959793955581 154.63078569762334]
hue: Z; y-values: [90.76316340021354 71.05228738206348 68.13109019572413]