对数据进行排序以便在 mplot3d 中绘图
我一直在使用 mplot3d (matplotlib 的一部分)进行一些各种 3d 绘图,并且它的工作表现令人钦佩。然而,我遇到了一个新问题。
Mplot3d 期望数据以某种方式排序,以绘制线框。例如,它喜欢这样的东西:
x = array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
y = array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3])
其中 z 是相同维度的数组,数据对应于空间中的每个位置。
不幸的是,我的数据不是这样格式化的 - 每隔一行都是相反的,因为数据是通过以光栅模式扫描来收集的。
所以我有更多类似的东西:
x = array([[1, 2, 3],
[3, 2, 1],
[1, 2, 3]])
我目前的方法是一种非常丑陋的、暴力的“做一个for循环,然后检查你是否在奇数行”,从旧的数组中构建一个新的数组,但我是希望有一种更优雅的方式来做到这一点。棘手的部分是我必须以与 X 和 Y 相同的方式重新排列 Z 数组,以确保保留与每个点对应的数据空间。
理想情况下,我想要一些健壮且专门设计用于对包含任意随机位置点的一组二维数组进行排序的东西,但即使是一种更Pythonic的方式来完成我已经在做的事情也将受到赞赏。如果我能让它变得更加强大,并且不依赖于这种特定的光栅扫描模式,从长远来看,它可能会帮我解决一些麻烦。
I have been using mplot3d (part of matplotlib) for some various 3d plotting, and it has been performing the job admirably. However, I have run into a new problem.
Mplot3d expects data to be sorted in a certain fashion, to plot a wireframe. For example, it likes something like this:
x = array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
y = array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3])
where z is then an array of the same dimensions, with data corresponding to each of those positions in space.
Unfortunately, my data isn't formatted like this - every other row is reversed, because the data is collected by scanning in a raster pattern.
So I have something more like:
x = array([[1, 2, 3],
[3, 2, 1],
[1, 2, 3]])
My current approach is a very ugly, brute-force "do a for loop then check if you're in an odd row or not" that builds a new array out of the old one, but I am hoping there is a more elegant way of doing this. The tricky part is that I have to re-arrange the Z array in the same way I do the X and Y, to ensure that the data corresponding with each point is space is preserved.
Ideally, I'd like something that's robust and specifically designed to sort a set of 2-d arrays that contain arbitrary random position points, but even a more pythonic way of doing what I'm already doing would be appreciated. If I could make it more robust, and not dependent on this specific raster scanning pattern, it would probably save me headaches in the long term.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确的话,您只想这样做:
x[1::2, :] = x[1::2, ::-1]
。有一些问题......如果您不制作
x
的中间副本,由于 numpy 中广播的工作方式,它不会完全达到您的预期。尽管如此,使用基本索引仍然非常简单:
这会将 this (
x
):转换为 this (
x_rev
):如果您不熟悉 python 中的切片,
x[1::2]
将从第二个项目开始选择x
的所有其他项目。 (1
是起始索引,2
是增量)相反,x[::-1]
仅指定增量 < code>-1,从而反转数组。在本例中,我们仅将这些切片应用到特定轴,因此我们可以从第二行开始选择并反转每隔一行。If I understand you correctly, you just want to do this:
x[1::2, :] = x[1::2, ::-1]
.There are a few kinks... If you don't make an intermediate copy of
x
it doesn't quite do what you'd expect due to the way broadcasting works in numpy.Nonetheless, it's still pretty simple to do with basic indexing:
This converts this (
x
):Into this (
x_rev
):In case you're not familiar with slicing in python,
x[1::2]
would select every other item ofx
, starting with the second item. (1
is the the start index,2
is the increment) In contrast,x[::-1]
just specifies an increment of-1
, thus reversing the array. In this case we're only applying these slices to a particular axis, so we can select and reverse every other row, starting with the second row.