简体   繁体   English

在 MatplotLib 3D 散点图中绘制边界框

[英]Drawing Bounding Box in MatplotLib 3D Scatterplot

I'd like to draw a bounding box around my 3D Scatterplot datapoints without having to resort to Microsoft Paint.我想在我的 3D 散点图数据点周围绘制一个边界框,而不必求助于 Microsoft Paint。 Is there an easy way to do this in MatplotLib?在 MatplotLib 中是否有一种简单的方法可以做到这一点?

在此处输入图像描述

You need to define where your vertices are, and which edges need connecting.您需要定义顶点在哪里,以及哪些边需要连接。 Assuming you have defined a set of edges as pairs of vertices in some list AllEdgesVertexPairs , ask matplotlib to make a line for each pair.假设您已在某个列表AllEdgesVertexPairs中将一组边定义为顶点对,请让 matplotlib 为每一对创建一条线。

#Plot a line segment for each edge pair:
for SingleEdgeVertexPair in AllEdgesVertexPairs:
    Vertex1 = SingleEdgeVertexPair[0]
    Vertex2 = SingleEdgeVertexPair[1]

    EdgeXvals = [Vertex1[0], Vertex2[0] ]
    EdgeYvals = [Vertex1[1], Vertex2[1] ]
    EdgeZvals = [Vertex1[2], Vertex2[2] ]

    matplotlib.pyplot.plot(
        EdgeXvals, 
        EdgeYvals,
        EdgeZvals,  
        c='k', 
        marker=None,
        linestyle = '-',
        linewidth = 0.2,
        )

In a particular case for myself, I needed to plot a cube so my AllEdgesVertexPairs looked like:在我自己的一个特殊情况下,我需要 plot 一个立方体,所以我的AllEdgesVertexPairs看起来像:

[[[ 16.  16. 115.]
  [-14.  16. 115.]]
 [[ 16.  16. 115.]
  [ 16. -14. 115.]]
 [[ 16.  16. 115.]
  [ 16.  16.  85.]]
 [[-14.  16. 115.]
  [-14. -14. 115.]]
 [[-14.  16. 115.]
  [-14.  16.  85.]]
 [[ 16. -14. 115.]
  [-14. -14. 115.]]
 [[ 16. -14. 115.]
  [ 16. -14.  85.]]
 [[-14. -14. 115.]
  [-14. -14.  85.]]
 [[ 16.  16.  85.]
  [-14.  16.  85.]]
 [[ 16.  16.  85.]
  [ 16. -14.  85.]]
 [[-14.  16.  85.]
  [-14. -14.  85.]]
 [[ 16. -14.  85.]
  [-14. -14.  85.]]]

In your case you will also need 8 vertices, and 12 edges.在您的情况下,您还需要 8 个顶点和 12 个边。 However, its not super obvious how you will choose them.但是,您将如何选择它们并不是很明显。

If you need an algorithm to choose the bounding vertices yourself based on your image, I would suggest the following:如果您需要一种算法来根据您的图像自己选择边界顶点,我会建议以下内容:

  • Calculate principle components from eigenvalues / eigenvectors of a covariance matrix.从协方差矩阵的特征值/特征向量计算主成分。
  • Use a rectangular prism centered upon the mean of your data使用以数据平均值为中心的矩形棱镜
  • side lengths = 2*StdDev on each principle axis每个主轴上的边长 = 2*StdDev
  • Use the eigenvectors to find the vertex locations from the mean使用特征向量从平均值中找到顶点位置

By doing it with PCA and rectangular prism, you can make a mathematical statement about how many points will be inside your "bounding box".通过使用 PCA 和直角棱镜,您可以对“边界框”内的点数做出数学陈述。 (you are basically approximating an ellipsoid derived from a multivariate Gaussian with a rectangular box). (您基本上是在用一个矩形框逼近从多元高斯派生的椭圆体)。 There are likely better or more clever algorithms out there.那里可能有更好或更聪明的算法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM