gpt4 book ai didi

python - 从 plt.scatter 获取 PathCollection 的单个元素的边界框

转载 作者:太空宇宙 更新时间:2023-11-03 15:35:36 25 4
gpt4 key购买 nike

有没有办法在 plt.scatter() 的输出中获取单个元素的边界框?我可以获得偏移量(即 x 和 y 坐标——我必须从它们开始,因为我将它们用于绘图)和大小,但大小不是数据单位,所以即使是从面积到半径的 hacky 转换以获得大小的 bboxes 不会工作...

有什么好的方法吗?

tips = sns.load_dataset('tips')[:20]
f, ax = plt.subplots()
sc = ax.scatter(tips["total_bill"], y=tips["tip"], s=(tips["size"]*3)**2)
plt.show()

enter image description here

sc.properties()['offsets']

array([[ 16.99,   1.01],
[ 10.34, 1.66],
[ 21.01, 3.5 ],
[ 23.68, 3.31],
[ 24.59, 3.61],
[ 25.29, 4.71],
[ 8.77, 2. ],
[ 26.88, 3.12],
[ 15.04, 1.96],
[ 14.78, 3.23],
[ 10.27, 1.71],
[ 35.26, 5. ],
[ 15.42, 1.57],
[ 18.43, 3. ],
[ 14.83, 3.02],
[ 21.58, 3.92],
[ 10.33, 1.67],
[ 16.29, 3.71],
[ 16.97, 3.5 ],
[ 20.65, 3.35]])

sc.get_sizes()

array([ 36, 81, 81, 36, 144, 144, 36, 144, 36, 36, 36, 144, 36,
144, 36, 36, 81, 81, 81, 81])

最佳答案

一般来说,这远非简单。 PathCollection 允许不同的转换以及偏移转换。它也可能有一个或多个路径和大小。

幸运的是,有一个内置函数 matplotlib.path.get_path_collection_extents,它提供了一个 PathCollection 的边界框。我们可以使用它来获取每个成员的范围,方法是提供每条路径的单项列表并遍历所有路径。
由于边界框以像素为单位,因此最后需要转换回数据坐标。

下面是一个完成所有这些的完整函数。它需要先绘制图形,以便设置不同的变换。

import numpy as np; np.random.seed(432)
import matplotlib.pyplot as plt
from matplotlib.path import get_path_collection_extents


def getbb(sc, ax):
""" Function to return a list of bounding boxes in data coordinates
for a scatter plot """
ax.figure.canvas.draw() # need to draw before the transforms are set.
transform = sc.get_transform()
transOffset = sc.get_offset_transform()
offsets = sc._offsets
paths = sc.get_paths()
transforms = sc.get_transforms()

if not transform.is_affine:
paths = [transform.transform_path_non_affine(p) for p in paths]
transform = transform.get_affine()
if not transOffset.is_affine:
offsets = transOffset.transform_non_affine(offsets)
transOffset = transOffset.get_affine()

if isinstance(offsets, np.ma.MaskedArray):
offsets = offsets.filled(np.nan)

bboxes = []

if len(paths) and len(offsets):
if len(paths) < len(offsets):
# for usual scatters you have one path, but several offsets
paths = [paths[0]]*len(offsets)
if len(transforms) < len(offsets):
# often you may have a single scatter size, but several offsets
transforms = [transforms[0]]*len(offsets)

for p, o, t in zip(paths, offsets, transforms):
result = get_path_collection_extents(
transform.frozen(), [p], [t],
[o], transOffset.frozen())
bboxes.append(result.inverse_transformed(ax.transData))

return bboxes



fig, ax = plt.subplots()

sc = ax.scatter(*np.random.rand(2,5), s=np.random.rand(5)*150+60)

# a single size needs to work as well. As well as a marker with non-square extent
sc2 = ax.scatter([0.2,0.5],[0.1, 0.7], s=990, marker="$\\rightarrow$")

boxes = getbb(sc, ax)
boxes2 = getbb(sc2, ax)

# Draw little rectangles for boxes:
for box in boxes+boxes2:
rec = plt.Rectangle((box.x0, box.y0), box.width, box.height, fill=False,
edgecolor="crimson")
ax.add_patch(rec)

plt.show()

enter image description here

关于python - 从 plt.scatter 获取 PathCollection 的单个元素的边界框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55005272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com