Python - 在散点处绘制已知大小的矩形

标签 python scatter-plot

我有一组点:

a = ([126, 237, 116, 15, 136, 348, 227, 247, 106, 5, -96, 25, 146], [117, 127, 228, 107, 6, 137, 238, 16, 339, 218, 97, -4, -105])

然后我像这样绘制它们的散点图:

    fig = plt.figure(figsize = (15,6))
    ax = fig.add_subplot(111)
    ax.scatter(a[0], a[1], color = 'red', s=binradius)

这使得这个情节:

enter image description here

--

我用一张图片覆盖它,其中每个散点都有一个球形 Blob 。我想拟合这个 Blob ,所以我在散点周围定义了一个矩形区域,以便执行拟合。

我想在图上看到这个矩形,以直观地查看它们是否足够大以包围 blob,如下所示:

enter image description here

我可以用 scatter 来做吗?或者有什么其他方法可以做到吗?

最佳答案

虽然@ralf-htp 的回答很好很干净并且使用了 scatter,但据我所知,标记的比例是用 points 表示的(参见例如 here) .此外,如果放大,自定义标记不会改变大小。

也许这正是您正在寻找的。如果不是,使用单独的 Rectangle 对象也能很好地达到目的。这允许您以数据单位而不是点指定宽度和高度,并且您可以放大。如有必要,通过设置 angle 属性也可以轻松应用旋转:

from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle

# Your data
a = ([126, 237, 116, 15, 136, 348, 227, 247, 106, 5, -96, 25, 146],
     [117, 127, 228, 107, 6, 137, 238, 16, 339, 218, 97, -4, -105])

# Your scatter plot
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(a[0], a[1], color = 'red', s=10)

# Add rectangles
width = 30
height = 20
for a_x, a_y in zip(*a):
    ax.add_patch(Rectangle(
        xy=(a_x-width/2, a_y-height/2) ,width=width, height=height,
        linewidth=1, color='blue', fill=False))
ax.axis('equal')
plt.show()

结果: scatter plot with blue rectangles

注意:如有必要,您可以通过 ax.get_children() 获取 Rectangle 实例。

关于Python - 在散点处绘制已知大小的矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52056475/

相关文章:

python-3.x - 在 Python 3.0 中为类动态添加方法

python - wxPython:GridBagSizer 的问题

python - 使用 xpath 的 Scrapy 格式面包屑

python - 如何制作一个迭代超过 1500 万条记录的 for 循环,节省空间?

r - 在 R 中添加对线

r - 散点图回归线 lty 命令未被识别

r - 防止 geom_points 及其对应的标签重叠

python - 如何在 pandas 数据框中通过两列和第三列求和来从组中获取最大值?

highcharts - 在分散 HighCharts 中显示具有完全相同值的多个点

numpy - 用不同的标记 python 绘制类